Skip to content

第四章:聊天平台集成

把 AI 助手接入你最常用的聊天工具。

本章概览

在这一章中,你将学习:

  • 如何将 OpenClaw 接入 Telegram(最简单的入门方式)
  • 如何将 OpenClaw 接入 WhatsApp
  • Slack 和 Discord 的接入概览
  • 如何同时运行多个平台
  • 安全最佳实践

推荐顺序

如果你是第一次接入聊天平台,建议按这个顺序尝试:

  1. Telegram -- 最简单,5 分钟搞定
  2. Slack -- 如果你用 Slack 办公的话
  3. WhatsApp -- 需要更多配置步骤
  4. Discord -- 和 Telegram 类似

4.1 Telegram 集成(推荐入门)

Telegram 是接入 OpenClaw 最简单的平台,因为 Telegram 的 Bot API 非常开放友好。

为什么从 Telegram 开始?

  • 创建 Bot 最简单 -- 直接和 @BotFather 聊天就行
  • 不需要企业账号 -- 个人账号就能用
  • 不需要审核 -- 即时生效
  • 免费 -- Telegram Bot API 完全免费
  • 功能丰富 -- 支持 Markdown、按钮、文件等

第一步:创建 Telegram Bot

  1. 打开 Telegram,搜索 @BotFather(这是 Telegram 官方的 Bot 管理机器人)

  2. 发送 /newbot 命令

  3. BotFather 会问你两个问题:

BotFather: Alright, a new bot. How are we going to call it?
           Please choose a name for your bot.

你: My OpenClaw Assistant

BotFather: Good. Now let's choose a username for your bot.
           It must end in 'bot'. Like this, for example:
           TetrisBot or tetris_bot.

你: my_openclaw_bot
  1. BotFather 会返回你的 Bot Token
BotFather: Done! Congratulations on your new bot.
           You will find it at t.me/my_openclaw_bot.

           Use this token to access the HTTP API:
           123456789:ABCdefGHIjklMNOpqrsTUVwxyz

           For a description of the Bot API, see:
           https://core.telegram.org/bots/api

保管好 Bot Token

Bot Token 就像你的 Bot 的密码,谁有了这个 Token 就能控制你的 Bot。请:

  • 不要分享给别人
  • 不要发布到公开场所
  • 如果泄露了,立刻在 BotFather 中使用 /revoke 重新生成

第二步:配置 OpenClaw

使用命令行添加 Telegram 平台:

bash
openclaw gateway add telegram

交互式向导会引导你完成配置:

🐾 Adding Telegram platform

? Enter your Telegram Bot Token:
  123456789:ABCdefGHIjklMNOpqrsTUVwxyz

? Enter your Telegram User ID (for access control):
  (You can find it by messaging @userinfobot on Telegram)
  87654321

? Webhook URL (or press Enter to use polling mode):
  (press Enter for polling)

✅ Telegram platform added successfully!

Your bot is now listening at: @my_openclaw_bot
Send it a message to test!
什么是 Polling 模式和 Webhook 模式?

Polling 模式(轮询):OpenClaw 每隔几秒主动去 Telegram 服务器问"有新消息吗?"

  • 优点:简单,不需要公网 IP
  • 缺点:有轻微延迟(1-3 秒)

Webhook 模式:Telegram 服务器在有新消息时主动推送给 OpenClaw

  • 优点:实时,无延迟
  • 缺点:需要公网可访问的 URL(可用 ngrok 解决)

对于个人使用,Polling 模式完全够用。

或者,你也可以直接编辑配置文件:

yaml
# ~/.openclaw/gateway/platforms/telegram.yaml
platform: telegram
enabled: true

# Bot Token(从 BotFather 获取)
bot_token: "123456789:ABCdefGHIjklMNOpqrsTUVwxyz"

# 连接模式
mode: polling              # polling 或 webhook
polling_interval: 1000     # 轮询间隔(毫秒)

# Webhook 配置(如果使用 webhook 模式)
# webhook_url: "https://your-domain.com/webhook/telegram"

# 访问控制
allowed_users:
  - "87654321"             # 你的 Telegram User ID

# 消息格式
message_format:
  parse_mode: "Markdown"   # 支持 Markdown 格式
  max_length: 4096         # 单条消息最大长度

第三步:获取你的 Telegram User ID

你需要知道自己的 Telegram User ID 来设置访问控制。

方法一:使用 @userinfobot

  1. 在 Telegram 中搜索 @userinfobot
  2. 发送任意消息
  3. 它会回复你的 User ID
@userinfobot:
Your user ID: 87654321
Your language: zh-hans

方法二:使用 @RawDataBot

  1. 搜索 @RawDataBot
  2. 发送任意消息
  3. 在回复中找到 "id" 字段

第四步:测试

  1. 在 Telegram 中搜索你创建的 Bot(比如 @my_openclaw_bot
  2. 点击 "Start" 开始对话
  3. 发送一条消息,比如 "你好"
  4. 等待回复
你: 你好!

my_openclaw_bot: 你好!我是你的 AI 助手。
                 有什么我可以帮你的吗?😊

第一次回复可能比较慢

第一条消息可能需要 5-10 秒才能收到回复,因为 OpenClaw 需要初始化连接。后续消息通常在 2-5 秒内回复。

进阶:自定义 Bot 信息

通过 BotFather,你可以自定义 Bot 的各种信息:

/setdescription   - 设置 Bot 描述
/setabouttext     - 设置 "关于" 文本
/setuserpic       - 设置 Bot 头像
/setcommands      - 设置命令菜单

设置命令菜单的示例:

/setcommands

BotFather: Choose a bot to change the list of commands.

(选择你的 bot)

BotFather: OK. Send me a list of commands for your bot.

help - 查看帮助信息
memo - 记录备忘
tasks - 查看待办事项
clear - 清除当前对话

4.2 WhatsApp 集成

WhatsApp 的集成相对复杂一些,因为它需要通过 WhatsApp Business API

前置要求

  • 一个 Meta(Facebook)开发者账号
  • 一个 WhatsApp Business 账号
  • 一个用于接收 Webhook 的公网 URL

个人使用 vs 企业使用

WhatsApp Business API 主要面向企业用户,但个人也可以使用开发者测试号码。 如果你只是想体验,可以先用 Telegram,效果是一样的。

第一步:创建 Meta 应用

  1. 访问 developers.facebook.com
  2. 登录或创建账号
  3. 进入 "My Apps"
  4. 点击 "Create App"
  5. 选择 "Business" 类型
  6. 填写应用名称,点击 "Create App"

第二步:添加 WhatsApp 产品

  1. 在应用面板中,点击 "Add Product"
  2. 找到 "WhatsApp" 并点击 "Set up"
  3. 按照提示完成 WhatsApp Business 账号关联

你会获得以下信息:

Phone Number ID:  1234567890
WhatsApp Business Account ID: 0987654321
Temporary Access Token: EAAG...(临时 Token,24 小时过期)

关于 Access Token

Meta 提供的临时 Token 只有 24 小时有效期。对于长期使用,你需要:

  1. 在 Meta 应用设置中生成永久 Token
  2. 或者使用 System User Token(推荐)

具体步骤请参考 Meta 官方文档。

第三步:配置 Webhook

WhatsApp Business API 使用 Webhook 来推送消息。你需要一个公网可访问的 URL。

本地开发可以使用 ngrok:

bash
# 安装 ngrok
npm install -g ngrok

# 启动隧道(OpenClaw 默认端口 3000)
ngrok http 3000

ngrok 会给你一个公网 URL,类似:

https://abc123.ngrok-free.app → http://localhost:3000

第四步:配置 OpenClaw

bash
openclaw gateway add whatsapp
🐾 Adding WhatsApp platform

? Phone Number ID:
  1234567890

? Access Token:
  EAAG...

? Verify Token (create a custom string):
  my-secret-verify-token

? Webhook URL:
  https://abc123.ngrok-free.app/webhook/whatsapp

✅ WhatsApp platform configured!

或手动编辑配置文件:

yaml
# ~/.openclaw/gateway/platforms/whatsapp.yaml
platform: whatsapp
enabled: true

# WhatsApp Business API 配置
phone_number_id: "1234567890"
access_token: "EAAG..."
verify_token: "my-secret-verify-token"

# Webhook 配置
webhook_path: "/webhook/whatsapp"

# 访问控制
allowed_numbers:
  - "+8613800138000"       # 你的手机号
  - "+8613900139000"       # 其他允许的号码

# 消息配置
message_format:
  max_length: 4096

第五步:在 Meta 开发者后台配置 Webhook

  1. 进入你的 Meta 应用 → WhatsApp → Configuration
  2. 在 Webhook 部分:
    • Callback URL: https://abc123.ngrok-free.app/webhook/whatsapp
    • Verify Token: my-secret-verify-token(和配置中一致)
  3. 点击 "Verify and save"
  4. 在 "Webhook fields" 中订阅 messages 事件

第六步:测试

用你的 WhatsApp 给 Business 号码发消息,就能收到 AI 的回复了。

4.3 Slack 集成

Slack 适合团队使用场景 -- 在工作群组中加入 AI 助手。

创建 Slack App

  1. 访问 api.slack.com/apps
  2. 点击 "Create New App"
  3. 选择 "From scratch"
  4. 填写 App Name 和选择 Workspace

配置权限(OAuth Scopes)

在 "OAuth & Permissions" 页面,添加以下 Bot Token Scopes:

chat:write        - 发送消息
app_mentions:read - 读取 @提及
channels:history  - 读取频道消息历史
im:history        - 读取私聊消息历史
im:read           - 读取私聊列表
im:write          - 发起私聊

安装到 Workspace

  1. 在 "OAuth & Permissions" 页面点击 "Install to Workspace"
  2. 授权
  3. 获取 Bot User OAuth Token(以 xoxb- 开头)

配置 OpenClaw

bash
openclaw gateway add slack
yaml
# ~/.openclaw/gateway/platforms/slack.yaml
platform: slack
enabled: true

bot_token: "xoxb-xxxxxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx"
signing_secret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
app_token: "xapp-x-xxxxxxxxxx-xxxxxxxxxxxxx-xxxxxxxxxxxxxxxx"

# 触发方式
triggers:
  - mention          # @机器人 时触发
  - direct_message   # 私聊时触发

# 访问控制
allowed_channels:
  - "C01234ABCDE"    # 允许在哪些频道使用
allowed_users:
  - "U01234ABCDE"    # 允许哪些用户使用

使用方式

在 Slack 中:

# 在频道中 @提及
@openclaw 帮我总结一下今天频道里的讨论要点

# 或者直接私聊
帮我写一个会议邀请邮件

4.4 Discord 集成

Discord 的接入方式和 Telegram 比较类似。

创建 Discord Bot

  1. 访问 discord.com/developers/applications
  2. 点击 "New Application"
  3. 填写名称,创建
  4. 在 "Bot" 页面点击 "Add Bot"
  5. 复制 Bot Token

配置权限

在 "OAuth2 → URL Generator" 中:

  • Scopes: bot
  • Bot Permissions: Send Messages, Read Message History

生成邀请链接,用这个链接把 Bot 邀请到你的 Discord 服务器。

配置 OpenClaw

yaml
# ~/.openclaw/gateway/platforms/discord.yaml
platform: discord
enabled: true

bot_token: "MTIzNDU2Nzg5MDEyMzQ1Njc4OQ.XXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXX"

# 触发方式
triggers:
  - mention           # @机器人 时触发
  - direct_message    # 私聊时触发
  - prefix: "!ask"    # 以 !ask 开头的消息触发

# 访问控制
allowed_servers:
  - "123456789012345678"
allowed_channels:
  - "987654321098765432"

4.5 同时运行多个平台

OpenClaw 的一大优势是可以同时连接多个平台。配置好各个平台后,它们会自动并行运行。

查看已连接的平台

bash
openclaw gateway list

输出示例:

🐾 Connected Platforms

  Platform    Status      Users     Messages Today
  ─────────   ─────────   ───────   ──────────────
  Telegram    Connected   1         24
  WhatsApp    Connected   1         12
  Slack       Connected   3         8
  Discord     Disabled    -         -

  Total messages today: 44

启用/禁用平台

bash
# 禁用某个平台(不删除配置)
openclaw gateway disable discord

# 重新启用
openclaw gateway enable discord

# 完全移除
openclaw gateway remove discord

平台优先级

如果你在多个平台上同时和 AI 对话,Gateway 会确保上下文的连贯性。但有时你可能想设置平台优先级:

yaml
# ~/.openclaw/config.yaml
gateway:
  priority:
    - telegram       # 优先响应 Telegram
    - whatsapp       # 其次 WhatsApp
    - slack          # 然后 Slack
    - discord        # 最后 Discord

优先级的含义

优先级影响的是消息处理顺序。当多个平台同时有消息到达时,高优先级平台的消息会先被处理。对于普通个人使用,通常不需要设置优先级。

4.6 安全最佳实践

将 AI 助手接入聊天平台后,安全是必须认真对待的问题。

实践一:开启白名单

最重要的安全措施:只允许指定的用户/号码使用你的 AI 助手。

yaml
# 每个平台都应设置允许列表
# Telegram
allowed_users:
  - "87654321"         # 你的 User ID

# WhatsApp
allowed_numbers:
  - "+8613800138000"   # 你的手机号

# Slack
allowed_users:
  - "U01234ABCDE"      # 你的 Slack User ID

不设白名单的后果

如果你的 Telegram Bot 没有设置白名单,任何人找到你的 Bot 都可以和它对话。这意味着:

  • 别人可以消耗你的 API 额度
  • 别人可能看到你的 Skills 列表
  • 如果启用了 Shell 等高权限 Skills,可能造成安全风险

实践二:使用专用号码

对于 WhatsApp 集成:

推荐:使用一个专门的手机号注册 WhatsApp Business
不推荐:使用你的个人手机号

原因:

  • 隔离个人和 AI 助手的通信
  • 更容易管理和监控
  • 出问题时影响范围更小

实践三:限制 Skills 权限

不是所有 Skills 都需要在所有平台上启用:

yaml
# 可以按平台限制可用的 Skills
platforms:
  telegram:
    allowed_skills:
      - web_browse
      - memo
      - calendar
    blocked_skills:
      - shell          # 不允许通过 Telegram 执行命令

  slack:
    allowed_skills:
      - all            # Slack 内网环境,允许所有 Skills

实践四:设置速率限制

防止 API 额度被意外消耗完:

yaml
security:
  rate_limit:
    messages_per_minute: 20    # 每分钟最多 20 条消息
    messages_per_hour: 200     # 每小时最多 200 条
    messages_per_day: 1000     # 每天最多 1000 条
    alert_threshold: 0.8       # 达到 80% 时发出警告

实践五:定期审查日志

bash
# 查看最近的异常活动
openclaw logs --level warn

# 查看消息统计
openclaw stats --period week

# 输出示例
🐾 Weekly Statistics (2025-01-13 to 2025-01-19)

  Platform    Messages    Unique Users    Blocked
  ─────────   ────────    ────────────    ───────
  Telegram    168         1               0
  WhatsApp    95          1               2
  Slack       42          3               0

  ⚠️ 2 blocked messages from unknown WhatsApp numbers

安全检查清单

在正式使用前,请确认以下安全设置:

  • [ ] 所有平台都设置了用户白名单
  • [ ] API Key 没有暴露在公开位置
  • [ ] 高危 Skills(Shell、文件系统)有适当的权限限制
  • [ ] 设置了消息速率限制
  • [ ] Webhook URL 使用 HTTPS
  • [ ] Bot Token 安全保存,没有泄露
  • [ ] 定期查看运行日志

4.7 使用 ngrok 进行本地开发

如果你在本地运行 OpenClaw,但需要接收 Webhook 回调(WhatsApp 等),可以使用 ngrok。

安装和使用

bash
# 安装
npm install -g ngrok

# 注册账号并获取 Auth Token
# 访问 ngrok.com 注册,然后:
ngrok authtoken YOUR_AUTH_TOKEN

# 启动隧道
ngrok http 3000

ngrok 输出

Session Status                online
Account                       your-email@example.com
Version                       3.x.x
Region                        Asia Pacific (ap)
Forwarding                    https://abc123.ngrok-free.app → http://localhost:3000

Connections    ttl     opn     rt1     rt5     p50     p90
               12      0       0.00    0.00    0.15    0.30

https://abc123.ngrok-free.app 作为你的 Webhook 基础 URL。

ngrok 免费版限制

免费版 ngrok 每次启动会生成不同的 URL,这意味着每次重启都需要更新 Webhook 配置。 如果你需要固定 URL,可以考虑:

  1. ngrok 付费版(固定域名)
  2. 使用 Cloudflare Tunnel(免费)
  3. 部署到有公网 IP 的服务器

本章小结

在这一章中,我们学习了:

  • Telegram 是最容易接入的平台,通过 BotFather 创建 Bot 即可
  • WhatsApp 需要通过 Meta Business API,配置较复杂
  • Slack 和 Discord 各有自己的 App/Bot 创建流程
  • 多平台并行 是 OpenClaw 的核心优势
  • 安全很重要 -- 白名单、速率限制、日志审查缺一不可

下一步

平台接好了,接下来让我们看看 AI 助手能做些什么 -- 第五章:Skills 与 Tools