消息通道:Telegram 与 Discord
Telegram Bot 申请与配置
在 OpenClaw 中使用 Telegram 通道,首先需要在 Telegram 上创建一个 Bot 并获取 Token。
创建 Telegram Bot 的步骤:
- 在 Telegram 中搜索
@BotFather,这是 Telegram 官方提供的 Bot 管理机器人 - 向 BotFather 发送
/newbot命令,按照提示设置 Bot 的名称和用户名 - 创建成功后,BotFather 会返回一个 API Token,格式如
7234567890:AAHxxxxxxxxxxxxxxxxxxxxxxxxxx - 将 Token 保存到项目的
.env文件中
# .env 文件
TELEGRAM_BOT_TOKEN=7234567890:AAHxxxxxxxxxxxxxxxxxxxxxxxxxx
配置 Telegram 通道:
// openclaw.config.ts
import { defineConfig } from 'openclaw';
export default defineConfig({
model: {
provider: 'anthropic',
model: 'claude-sonnet-4-20250514',
apiKey: process.env.ANTHROPIC_API_KEY,
},
bots: [
{
name: 'tg-bot',
platform: 'telegram',
token: process.env.TELEGRAM_BOT_TOKEN,
systemPrompt: '你是一个 Telegram 上的中文 AI 助手。',
allowGroups: true, // 允许在群组中使用
allowPrivateChats: true, // 允许私聊
adminIds: ['987654321'], // 管理员用户 ID
},
],
});
Discord Bot 创建与权限配置
创建 Discord Bot 的步骤:
- 访问 Discord Developer Portal 并登录
- 点击 "New Application",输入应用名称
- 进入 "Bot" 页面,点击 "Add Bot" 创建机器人
- 在 "Token" 区域点击 "Reset Token" 并复制生成的 Token
- 在 "Privileged Gateway Intents" 中勾选
MESSAGE CONTENT INTENT(必须启用)
邀请 Bot 加入服务器:
在 OAuth2 > URL Generator 页面,选择 bot 和 applications.commands scopes,然后选择以下权限:
- Send Messages
- Read Message History
- Use Slash Commands
- Embed Links
- Attach Files
配置 Discord 通道:
// openclaw.config.ts
export default defineConfig({
model: {
provider: 'anthropic',
model: 'claude-sonnet-4-20250514',
apiKey: process.env.ANTHROPIC_API_KEY,
},
bots: [
{
name: 'discord-bot',
platform: 'discord',
token: process.env.DISCORD_BOT_TOKEN,
systemPrompt: '你是一个 Discord 服务器上的 AI 助手。',
intents: ['guilds', 'guildMessages', 'messageContent'],
allowDm: true,
commandPrefix: '!ai', // 命令前缀
},
],
});
通道配置参数详解
Telegram 和 Discord 通道共享以下通用配置参数:
| 参数 | 类型 | 说明 | Telegram | Discord |
|---|---|---|---|---|
| token | string | Bot 身份令牌 | 必填 | 必填 |
| webhookUrl | string | Webhook 回调地址 | 可选 | 不支持 |
| polling | boolean | 是否使用轮询模式 | 可选,默认 false | 强制 |
| commandPrefix | string | 命令触发前缀 | 可选 | 可选 |
| allowGroups | boolean | 是否响应群组消息 | 可选 | 对应 guilds |
| rateLimit | number | 每分钟最大消息数 | 可选 | 内置限制 |
Webhook vs Polling 模式
Telegram 通道支持 Webhook 和 Polling 两种消息接收模式,两者的区别如下:
| 对比维度 | Webhook 模式 | Polling 模式 |
|---|---|---|
| 原理 | 由 Telegram 服务器主动推送消息 | 由 Bot 定时主动拉取消息 |
| 需要公网 IP | 是,需暴露可访问的 URL | 否 |
| 需要 HTTPS | 是,必须配置 SSL 证书 | 否 |
| 实时性 | 高,消息即时推送 | 较低,有轮询间隔延迟 |
| 资源消耗 | 低,被动接收 | 较高,持续轮询 |
| 配置复杂度 | 较高,需配置 Webhook URL 和证书 | 低,开箱即用 |
| 适用场景 | 生产环境 | 开发调试、内网环境 |
在开发阶段推荐使用 Polling 模式。切换到生产环境时,使用 Webhook 模式可以获得更好的实时性和更低的资源消耗。
// 开发环境使用 Polling
{
platform: 'telegram',
token: process.env.TELEGRAM_BOT_TOKEN,
polling: true, // 启用轮询
}
// 生产环境使用 Webhook
{
platform: 'telegram',
token: process.env.TELEGRAM_BOT_TOKEN,
webhookUrl: 'https://your-domain.com/webhook/telegram',
}
消息格式适配
不同平台的消息格式存在差异,OpenClaw 通过统一的 Message 接口进行适配。以下是一个消息在不同平台间的格式转换示意:
// OpenClaw 统一消息格式
interface Message {
id: string;
platform: 'telegram' | 'discord';
channelId: string;
userId: string;
text: string;
attachments?: Attachment[];
metadata?: Record<string, unknown>;
}
// 平台适配器负责转换
class TelegramAdapter implements ChannelAdapter {
async toPlatform(msg: Message): Promise<TelegramMessage> {
return {
chat_id: msg.channelId,
text: msg.text,
parse_mode: 'MarkdownV2',
// Telegram 特有的格式适配
};
}
}
class DiscordAdapter implements ChannelAdapter {
async toPlatform(msg: Message): Promise<DiscordMessage> {
return {
content: msg.text,
// Discord 使用不同的消息格式
embeds: msg.attachments?.map(attachmentToEmbed),
};
}
}
实战:同时部署 Telegram 和 Discord 机器人
以下配置展示了如何用一个 OpenClaw 实例同时为 Telegram 和 Discord 提供服务:
import { defineConfig } from 'openclaw';
export default defineConfig({
model: {
provider: 'anthropic',
model: 'claude-sonnet-4-20250514',
apiKey: process.env.ANTHROPIC_API_KEY,
maxTokens: 2048,
},
bots: [
{
name: 'telegram-support',
platform: 'telegram',
token: process.env.TELEGRAM_BOT_TOKEN,
systemPrompt: '你是 Telegram 上的客服助手,回复简洁专业。',
polling: true,
},
{
name: 'discord-community',
platform: 'discord',
token: process.env.DISCORD_BOT_TOKEN,
systemPrompt: '你是 Discord 社区的 AI 伙伴,回复亲切活泼。',
commandPrefix: '!ai',
},
],
session: {
strategy: 'memory',
ttl: 3600,
},
});
启动后,两个机器人会共享同一个 AI 模型,但各自维护独立的会话和角色设定。用户无论在 Telegram 还是 Discord 上发送消息,都能获得一致的服务体验。