OpenClaw Telegram & Discord Bot 完整搭建指南
世界上有一种孤独叫做"写了个AI Agent,但没人跟它说话"。于是我开始搭建Bot——Telegram的、Discord的、飞书的。凌晨1点,3个Bot同时在线,我看着消息通知,突然觉得自己像一个数字世界的夜班值班员。
为什么用OpenClaw搭Bot
传统Bot开发需要:选框架 → 写Webhook → 处理消息格式 → 管理会话状态 → 对接AI。而OpenClaw已经把这些全包了。
- 开箱即用的消息处理:飞书、Discord、Telegram、企业微信、QQ,一行配置搞定
- 内置AI能力:Bot天然具备LLM推理、工具调用、记忆管理
- 多渠道统一:同一个Agent同时服务多个平台
- 自动化集成:cron定时任务、webhook、审批流程无缝接入
Part 1: Telegram Bot 搭建
Step 1: 创建 Bot
# 1. 在 Telegram 中找到 @BotFather
# 2. 发送 /newbot
# 3. 输入Bot名称和用户名
# 4. 获得 Bot Token
# 记录你的 Token
BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrsTUVwxyz
Step 2: 配置 OpenClaw Telegram 集成
# openclaw.config.yaml
channels:
telegram:
enabled: true
token: ${TELEGRAM_BOT_TOKEN}
# 可选配置
allowed_chats: # 白名单(仅响应特定聊天)
- chat_id: -1001234567890
name: "我的群组"
webhook_url: https://your-domain.com/telegram/webhook
command_prefix: "/" # 命令前缀
Step 3: 设置 Webhook(生产环境)
# 通过API设置Webhook
curl "https://api.telegram.org/bot${BOT_TOKEN}/setWebhook" \
-d "url=https://your-domain.com/webhook/telegram" \
-d "secret_token=your_secret_token"
Step 4: 命令注册
# 注册Bot命令菜单
curl "https://api.telegram.org/bot${BOT_TOKEN}/setMyCommands" \
-H "Content-Type: application/json" \
-d '{
"commands": [
{"command": "start", "description": "开始对话"},
{"command": "help", "description": "查看帮助"},
{"command": "status", "description": "查看系统状态"},
{"command": "news", "description": "最新AI新闻"},
{"command": "tools", "description": "推荐AI工具"}
]
}'
Step 5: 处理 Telegram 特有消息类型
// Telegram 支持丰富的消息类型
// OpenClaw message 工具已封装常用操作
// 发送文本消息
message({ action: "send", channel: "telegram", message: "你好!" });
// 发送图片
message({ action: "send", channel: "telegram", filePath: "/path/to/image.png" });
// 发送文档(避免压缩)
message({ action: "send", channel: "telegram", filePath: "report.pdf", forceDocument: true });
// 回复特定消息
message({ action: "send", channel: "telegram", replyTo: "msg_123", message: "这是回复" });
Part 2: Discord Bot 搭建
Step 1: 创建 Discord Application
# 1. 访问 https://discord.com/developers/applications
# 2. 点击 "New Application"
# 3. 创建后进入 Bot 页面
# 4. 复制 Bot Token
# 5. 开启 MESSAGE CONTENT INTENT(重要!)
DISCORD_BOT_TOKEN=MTIzNDU2Nzg5MDEyMzQ1Njc4OQ.XXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Step 2: 邀请 Bot 到服务器
# 在 OAuth2 页面生成邀请链接
# 勾选权限:bot, Send Messages, Read Message History, Embed Links
# 或直接构造URL:
https://discord.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&permissions=68608&scope=bot
Step 3: 配置 OpenClaw Discord 集成
# openclaw.config.yaml
channels:
discord:
enabled: true
token: ${DISCORD_BOT_TOKEN}
# 可选配置
allowed_guilds: # 服务器白名单
- "123456789012345678"
command_prefix: "!" # 命令前缀
activity:
type: "watching"
name: "AI新闻 | 妙趣AI"
Step 4: Discord 高级功能
// Discord 特有功能
// 设置Bot状态
message({ action: "send", channel: "discord", status: "online" });
// 发送带Emoji效果的消息
message({ action: "send", channel: "discord", effect: "balloons", message: "🎉 好消息!" });
// 创建线程
message({ action: "send", channel: "discord", threadName: "讨论: AI趋势" });
// 添加反应
message({ action: "react", channel: "discord", emoji: "🔥" });
// 发送 Embed 消息(结构化卡片)
message({
action: "send",
channel: "discord",
card: {
title: "AI日报",
description: "2026年4月24日AI行业热点",
color: 0x5865F2,
fields: [
{ name: "热点1", value: "Vercel Labs发布Skills", inline: true },
{ name: "热点2", value: "Claude更新上下文窗口", inline: true }
]
}
});
Part 3: 多渠道统一管理
跨平台消息转发
// 同一内容发送到多个平台
const platforms = ['telegram', 'discord', 'feishu'];
for (const channel of platforms) {
message({
action: "send",
channel: channel,
message: "🔥 今日AI热点:OpenClaw新版本发布!"
});
}
平台适配策略
- Telegram:适合快速问答、命令式交互、文件传输
- Discord:适合社区讨论、线程对话、丰富格式消息
- 飞书:适合团队协作、审批流程、知识库联动
- 企业微信:适合企业内部、合规要求高的场景
安全最佳实践
- 使用环境变量存储Token,绝不要硬编码
- 设置聊天白名单,防止Bot被滥用
- 限制命令频率,防止消息风暴
- 记录所有Bot交互日志
- 定期轮换Bot Token