zhin-adapter-development
Original:🇺🇸 English
Translated
Guides development of custom platform adapters in Zhin. Covers extending the Adapter abstract class, creating Bot instances, handling message events, registering tools, and lifecycle management. Use when building adapters for new chat platforms.
4installs
Sourcezhinjs/ai-skills
Added on
NPX Install
npx skill4agent add zhinjs/ai-skills zhin-adapter-developmentTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Zhin Adapter Development Guide
Use this skill to create custom platform adapters that connect Zhin to chat platforms (QQ, Discord, Telegram, etc.).
Adapter Architecture
An adapter manages multiple Bot instances for a single platform. Each Bot connects to the platform and handles message sending/receiving.
Adapter (platform)
├── Bot 1 (account A)
├── Bot 2 (account B)
└── Tools (platform-specific capabilities)Minimal Adapter
ts
import { Adapter, Bot, Plugin, Message } from '@zhin.js/core'
// 1. Define Bot config type
interface MyBotConfig {
token: string
name: string
}
// 2. Define platform message type
interface MyMessage {
id: string
content: string
channelId: string
senderId: string
}
// 3. Create Bot class
class MyBot extends Bot<MyBotConfig, MyMessage> {
$connected = false
async $connect() {
// Connect to platform API
this.$connected = true
}
async $disconnect() {
// Disconnect from platform
this.$connected = false
}
async $sendMessage(options: { id: string; type: string; content: string }) {
// Send message via platform API
return 'message-id'
}
async $recallMessage(messageId: string) {
// Recall/delete a message
}
}
// 4. Create Adapter class
class MyAdapter extends Adapter<MyBot> {
constructor(plugin: Plugin, config: Adapter.BotConfig<MyBot>[]) {
super(plugin, 'my-platform' as any, config)
}
createBot(config: MyBotConfig): MyBot {
return new MyBot(config)
}
}
// 5. Register the adapter
Adapter.register('my-platform', MyAdapter as any)Using the Adapter in a Plugin
ts
import { usePlugin, Adapter } from 'zhin.js'
const plugin = usePlugin()
// Create and start adapter
const adapter = new MyAdapter(plugin, [
{ token: 'bot-token-1', name: 'bot1' },
])
plugin.onMounted(async () => {
await adapter.start()
})
plugin.onDispose(async () => {
await adapter.stop()
})Message Handling
When a message arrives from the platform, emit it on the adapter:
ts
class MyAdapter extends Adapter<MyBot> {
private handleIncomingMessage(rawMsg: MyMessage) {
const message = new Message({
$adapter: 'my-platform',
$bot: rawMsg.botId,
$channel: { id: rawMsg.channelId, type: 'group' },
$sender: { id: rawMsg.senderId, name: rawMsg.senderName },
$content: rawMsg.content,
$raw: rawMsg.content,
$reply: async (content) => {
// Reply implementation
return 'reply-id'
},
})
// This triggers the middleware pipeline
this.emit('message.receive', message)
}
}Message Lifecycle
- Platform receives raw message.
- Adapter converts to object.
Message - event triggers the root plugin middleware pipeline.
message.receive - Middleware chain processes the message (logging, command matching, etc.).
Registering Adapter Tools
Adapters can provide tools for the AI service:
ts
class MyAdapter extends Adapter<MyBot> {
constructor(plugin: Plugin, config: any[]) {
super(plugin, 'my-platform' as any, config)
this.registerDefaultTools() // Adds send_message and list_bots
// Add custom platform tools
this.addTool({
name: 'my_platform_get_user',
description: 'Get user info from my-platform',
parameters: {
type: 'object',
properties: {
userId: { type: 'string', description: 'User ID' },
},
required: ['userId'],
},
execute: async (args) => {
return await this.getUserInfo(args.userId)
},
})
}
}Default Tools (via registerDefaultTools
)
registerDefaultTools- — Send message to a target
{platform}_send_message - — List connected bots
{platform}_list_bots
Before-Send Hook
Adapters support hooks for message transformation:
before.sendMessagets
plugin.root.on('before.sendMessage', async (options) => {
// Modify options.content before sending
return options
})Adapter Lifecycle Events
| Event | Description |
|---|---|
| A message was received |
| A private message received |
| A group message received |
| Request to recall a message |
Configuration in zhin.config.yml
zhin.config.ymlyaml
bots:
- name: my-platform
context: my-platform
token: bot-token-here
plugins:
- my-adapter-pluginChecklist
- Extend abstract class with your platform name.
Adapter - Create a subclass implementing
Bot,$connect,$disconnect,$sendMessage.$recallMessage - Use to register the adapter factory.
Adapter.register(name, factory) - Emit with a
message.receiveobject when messages arrive.Message - Call in the constructor for AI tool support.
registerDefaultTools() - Implement and
start()lifecycle in the plugin viastop()/onMounted.onDispose