Loading...
Loading...
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.
npx skill4agent add zhinjs/ai-skills zhin-adapter-developmentAdapter (platform)
├── Bot 1 (account A)
├── Bot 2 (account B)
└── Tools (platform-specific capabilities)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)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()
})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)
}
}Messagemessage.receiveclass 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)
},
})
}
}registerDefaultTools{platform}_send_message{platform}_list_botsbefore.sendMessageplugin.root.on('before.sendMessage', async (options) => {
// Modify options.content before sending
return options
})| Event | Description |
|---|---|
| A message was received |
| A private message received |
| A group message received |
| Request to recall a message |
zhin.config.ymlbots:
- name: my-platform
context: my-platform
token: bot-token-here
plugins:
- my-adapter-pluginAdapterBot$connect$disconnect$sendMessage$recallMessageAdapter.register(name, factory)message.receiveMessageregisterDefaultTools()start()stop()onMountedonDispose