The Channel module is a high-level abstraction built on top of WSClient, EventDispatcher, and Client. It is designed for building conversational bots that require features like message normalization, security policies, outbound sending, streaming replies, media uploads, and card interactions.
When to use Channel:
- Building AI chat bots.
- Implementing streaming replies (typewriter effect).
- Handling card button interactions.
- Managing media uploads and
@all strategies.
When to use lower-level components:
- If you only need to process a small number of events with simple logic, the
WSClient + EventDispatcher combination is sufficient.
import { createLarkChannel, LoggerLevel } from '@larksuiteoapi/node-sdk';
const channel = createLarkChannel({
appId: process.env.FEISHU_APP_ID!,
appSecret: process.env.FEISHU_APP_SECRET!,
loggerLevel: LoggerLevel.info,
policy: { requireMention: true, dmMode: 'open' },
});
channel.on('message', async (msg) => {
await channel.send(
msg.chatId,
{ markdown: `收到:${msg.content}` },
{ replyTo: msg.messageId },
);
});
await channel.connect();
console.log(`connected as ${channel.botIdentity!.name}`);
process.on('SIGINT', async () => {
await channel.disconnect();
process.exit(0);
});