chat-sdk
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseChat SDK
Chat SDK
Unified TypeScript SDK for building chat bots across Slack, Teams, Google Chat, Discord, GitHub, and Linear. Write bot logic once, deploy everywhere.
一款统一的TypeScript SDK,用于跨Slack、Teams、Google Chat、Discord、GitHub和Linear平台构建聊天机器人。只需编写一次机器人逻辑,即可部署到所有平台。
Critical: Read the bundled docs
重要提示:阅读捆绑文档
The package ships with full documentation in and TypeScript source types. Always read these before writing code:
chatnode_modules/chat/docs/node_modules/chat/docs/ # Full documentation (MDX files)
node_modules/chat/dist/ # Built types (.d.ts files)Key docs to read based on task:
- — setup guides
docs/getting-started.mdx - — event handlers, threads, messages, channels
docs/usage.mdx - — AI streaming with AI SDK
docs/streaming.mdx - — JSX interactive cards
docs/cards.mdx - — button/dropdown handlers
docs/actions.mdx - — form dialogs (Slack only)
docs/modals.mdx - — platform-specific adapter setup
docs/adapters/*.mdx - — state adapter config (Redis, ioredis, memory)
docs/state/*.mdx
Also read the TypeScript types from to understand the full API surface.
node_modules/chat/dist/chatnode_modules/chat/docs/node_modules/chat/docs/ # 完整文档(MDX文件)
node_modules/chat/dist/ # 编译后的类型文件(.d.ts格式)按任务选择核心文档:
- — 入门设置指南
docs/getting-started.mdx - — 事件处理器、线程、消息、频道
docs/usage.mdx - — 结合AI SDK实现AI流式响应
docs/streaming.mdx - — JSX交互式卡片
docs/cards.mdx - — 按钮/下拉菜单处理器
docs/actions.mdx - — 表单对话框(仅支持Slack)
docs/modals.mdx - — 各平台适配工具的设置说明
docs/adapters/*.mdx - — 状态适配器配置(Redis、ioredis、内存)
docs/state/*.mdx
同时请阅读中的TypeScript类型定义,以全面了解API的功能范围。
node_modules/chat/dist/Quick start
快速开始
typescript
import { Chat } from "chat";
import { createSlackAdapter } from "@chat-adapter/slack";
import { createRedisState } from "@chat-adapter/state-redis";
const bot = new Chat({
userName: "mybot",
adapters: {
slack: createSlackAdapter({
botToken: process.env.SLACK_BOT_TOKEN!,
signingSecret: process.env.SLACK_SIGNING_SECRET!,
}),
},
state: createRedisState({ url: process.env.REDIS_URL! }),
});
bot.onNewMention(async (thread) => {
await thread.subscribe();
await thread.post("Hello! I'm listening to this thread.");
});
bot.onSubscribedMessage(async (thread, message) => {
await thread.post(`You said: ${message.text}`);
});typescript
import { Chat } from "chat";
import { createSlackAdapter } from "@chat-adapter/slack";
import { createRedisState } from "@chat-adapter/state-redis";
const bot = new Chat({
userName: "mybot",
adapters: {
slack: createSlackAdapter({
botToken: process.env.SLACK_BOT_TOKEN!,
signingSecret: process.env.SLACK_SIGNING_SECRET!,
}),
},
state: createRedisState({ url: process.env.REDIS_URL! }),
});
bot.onNewMention(async (thread) => {
await thread.subscribe();
await thread.post("Hello! I'm listening to this thread.");
});
bot.onSubscribedMessage(async (thread, message) => {
await thread.post(`You said: ${message.text}`);
});Core concepts
核心概念
- Chat — main entry point, coordinates adapters and routes events
- Adapters — platform-specific (Slack, Teams, GChat, Discord, GitHub, Linear)
- State — pluggable persistence (Redis for prod, memory for dev)
- Thread — conversation thread with ,
post(),subscribe()startTyping() - Message — normalized format with ,
text(mdast AST),formattedraw - Channel — container for threads, supports listing and posting
- Chat — 主入口,负责协调适配器并路由事件
- Adapters — 各平台专属适配器(Slack、Teams、GChat、Discord、GitHub、Linear)
- State — 可插拔的持久化组件(生产环境用Redis,开发环境用内存)
- Thread — 对话线程,支持、
post()、subscribe()方法startTyping() - Message — 标准化消息格式,包含、
text(mdast语法树)、formatted字段raw - Channel — 线程的容器,支持线程列表查看和消息发布
Event handlers
事件处理器
| Handler | Trigger |
|---|---|
| Bot @-mentioned in unsubscribed thread |
| Any message in subscribed thread |
| Messages matching pattern in unsubscribed threads |
| Slash command invocations |
| Emoji reactions added/removed |
| Button clicks and dropdown selections |
| Slack Assistants API thread opened |
| Slack App Home tab opened |
| 处理器 | 触发条件 |
|---|---|
| 机器人在未订阅的线程中被@提及 |
| 已订阅线程中的任何消息 |
| 未订阅线程中匹配指定模式的消息 |
| 斜杠命令被调用 |
| 表情反应被添加/移除 |
| 按钮点击或下拉菜单选择 |
| Slack Assistants API线程开启 |
| Slack应用主页标签被打开 |
Streaming
流式响应
Pass any to . Works with AI SDK's :
AsyncIterable<string>thread.post()textStreamtypescript
import { ToolLoopAgent } from "ai";
const agent = new ToolLoopAgent({ model: "anthropic/claude-4.5-sonnet" });
bot.onNewMention(async (thread, message) => {
const result = await agent.stream({ prompt: message.text });
await thread.post(result.textStream);
});将任意传入即可实现流式响应。可与AI SDK的配合使用:
AsyncIterable<string>thread.post()textStreamtypescript
import { ToolLoopAgent } from "ai";
const agent = new ToolLoopAgent({ model: "anthropic/claude-4.5-sonnet" });
bot.onNewMention(async (thread, message) => {
const result = await agent.stream({ prompt: message.text });
await thread.post(result.textStream);
});Cards (JSX)
卡片(JSX)
Set in tsconfig. Components: , , , , , , , , , , , , .
jsxImportSource: "chat"CardCardTextButtonActionsFieldsFieldSelectSelectOptionImageDividerLinkButtonSectionRadioSelecttsx
await thread.post(
<Card title="Order #1234">
<CardText>Your order has been received!</CardText>
<Actions>
<Button id="approve" style="primary">Approve</Button>
<Button id="reject" style="danger">Reject</Button>
</Actions>
</Card>
);在tsconfig中设置。可用组件包括:、、、、、、、、、、、、。
jsxImportSource: "chat"CardCardTextButtonActionsFieldsFieldSelectSelectOptionImageDividerLinkButtonSectionRadioSelecttsx
await thread.post(
<Card title="Order #1234">
<CardText>Your order has been received!</CardText>
<Actions>
<Button id="approve" style="primary">Approve</Button>
<Button id="reject" style="danger">Reject</Button>
</Actions>
</Card>
);Packages
相关包
| Package | Purpose |
|---|---|
| Core SDK |
| Slack |
| Microsoft Teams |
| Google Chat |
| Discord |
| GitHub Issues |
| Linear Issues |
| Redis state (production) |
| ioredis state (alternative) |
| In-memory state (development) |
| 包 | 用途 |
|---|---|
| 核心SDK |
| Slack平台适配 |
| Microsoft Teams平台适配 |
| Google Chat平台适配 |
| Discord平台适配 |
| GitHub Issues适配 |
| Linear Issues适配 |
| Redis状态存储(生产环境) |
| ioredis状态存储(替代方案) |
| 内存状态存储(开发环境) |
Webhook setup
Webhook设置
Each adapter exposes a webhook handler via . Wire these to your HTTP framework's routes (e.g. Next.js API routes, Hono, Express).
bot.webhooks.{platform}每个适配器都通过暴露Webhook处理器。可将其接入你的HTTP框架路由中(例如Next.js API路由、Hono、Express)。
bot.webhooks.{platform}