消息通道:Telegram 与 Discord

Telegram Bot 申请与配置

在 OpenClaw 中使用 Telegram 通道,首先需要在 Telegram 上创建一个 Bot 并获取 Token。

创建 Telegram Bot 的步骤:

  1. 在 Telegram 中搜索 @BotFather,这是 Telegram 官方提供的 Bot 管理机器人
  2. 向 BotFather 发送 /newbot 命令,按照提示设置 Bot 的名称和用户名
  3. 创建成功后,BotFather 会返回一个 API Token,格式如 7234567890:AAHxxxxxxxxxxxxxxxxxxxxxxxxxx
  4. 将 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 的步骤:

  1. 访问 Discord Developer Portal 并登录
  2. 点击 "New Application",输入应用名称
  3. 进入 "Bot" 页面,点击 "Add Bot" 创建机器人
  4. 在 "Token" 区域点击 "Reset Token" 并复制生成的 Token
  5. 在 "Privileged Gateway Intents" 中勾选 MESSAGE CONTENT INTENT(必须启用)

邀请 Bot 加入服务器:

在 OAuth2 > URL Generator 页面,选择 botapplications.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 通道共享以下通用配置参数:

参数类型说明TelegramDiscord
tokenstringBot 身份令牌必填必填
webhookUrlstringWebhook 回调地址可选不支持
pollingboolean是否使用轮询模式可选,默认 false强制
commandPrefixstring命令触发前缀可选可选
allowGroupsboolean是否响应群组消息可选对应 guilds
rateLimitnumber每分钟最大消息数可选内置限制

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 上发送消息,都能获得一致的服务体验。