Telegram
SwB 借助你在 BotFather 中创建的机器人与 Telegram 通信。SwB 主动向 Telegram 查询新消息,因此 SwB 服务器使用内网地址即可。
开始之前
使用一个 Telegram 账号。要连接群组,这个账号需要能添加成员。以所有者身份登录 SwB。
复制设置提示词
Read the setup guide at https://swb.one/docs/setup/telegram. Help me connect Telegram to SwB. SwB carries messages between my agents and channels. If the SwB tools are missing, ask me to add SwB as a custom MCP server. Follow https://swb.one/docs/connect to add the client. For Claude, give me the path `Customize > Connectors > Add > Custom > Web`. For ChatGPT, ask me to open `https://chatgpt.com/plugins`. Give me the path `+ > Add custom MCP server`. For Grok, give me the path `grok.com > Connectors > New Connector > Custom`. For other apps, ask me to open the MCP server settings. Wait for me to confirm. Use a SwB client with owner access. Call `connect(what="telegram")`. Do each step marked "you" yourself. Give me each step marked "person" one at a time with the exact click path and links. Wait for me to say done after each step. Ask me to open https://t.me/BotFather and send `/newbot`. Ask me to choose the bot's name and username. BotFather replies with the token. Ask me to enter it only through the claim link from `connect`. Then ask me to start a chat with the bot and send one message. For a group, ask me to open `setup.add_to_group_url` from `telegram_status`. Ask me to select the group. Ask me to send `/swb@<bot username>` there. To let the bot read all group messages, ask me to send `/setprivacy` to BotFather. Ask me to choose the bot and select **Disable**. Ask me to remove the bot from the group. Ask me to add it again. Use `telegram_status` to get the chat IDs from `setup.seen_chats` and my user ID from the direct message. Add a space under `blocks.telegram.spaces` in `config/switchboard.yaml`. Put the chat IDs in `chats` and my user ID in `owner_ids`. Grant the test agent access in `blocks.telegram.agents`. If the file needs a manual edit, give me the YAML. Restart SwB after an edit. Call `connect(what="telegram")` again to check the connection. When it reports `connected`, ask me to choose a test chat. Call `telegram_post` with the space label, chat ID and test text. Show me the result. Subscribe to switchboard.message.created if this app supports MCP events. Otherwise, call wait or inbox to collect new messages. If you keep your own memory, set up memory sync as described at https://swb.one/docs/memory.
把提示词粘贴到你的 SwB 所有者客户端中。
设置 Telegram 机器人
打开 BotFather
在 Telegram 中打开 https://t.me/BotFather。如果是第一次与 BotFather 聊天,点按 Start。
创建机器人
发送 /newbot,然后回复一个显示名称。
选择用户名
回复一个以 bot 结尾的用户名。BotFather 会回复令牌。
保存令牌
打开智能体给出的 SwB 认领链接,把令牌粘贴到字段中。
发起私聊
在与机器人的私聊中点按 Start。发送一条消息,让 SwB 能找到你的用户 ID。
把机器人添加到群组
打开智能体给出的群组链接,选择群组。
发送设置命令
在群组中发送 /swb@<bot username>。SwB 由此得知群组 ID。
读取群组的全部消息
如果机器人需要读取某些群组的每条消息,向 BotFather 发送 /setprivacy。选择你的机器人,再选择 Disable。
重新添加机器人
把机器人从每个群组中移除,再重新添加到群组。
智能体会做什么
智能体调用 connect(what="telegram"),必要时启用 telegram 模块。模块启用后,connect 或 telegram_provision 返回用来保存 Bot 令牌的认领链接。
你保存令牌后,智能体调用 telegram_status,检查机器人和隐私模式。群组邀请链接在 setup.add_to_group_url 中,最近的聊天 ID 在 setup.seen_chats 中。
智能体在 config/switchboard.yaml 的 blocks.telegram.spaces 下配置一个空间。空间把一组聊天指向一个 SwB 项目。在 chats 中填群组 ID,在 owner_ids 中填你的用户 ID。编辑文件后重启 SwB。
凭据
| 令牌 | Telegram 中的显示位置 | 格式 | 去向 |
|---|---|---|---|
| Bot 令牌 | 发送 /newbot 或 /token 后 BotFather 的回复 | 数字、一个冒号,再加字母和数字 | SwB 认领链接 |
检查连接
让智能体再次调用 connect。设置完成后,它会报告 connected。
使用 owner_ids 中列出的 Telegram 账号。在私聊中向机器人发送 /swb <target> hello,其中的 <target> 换成 targets 中的一个目标名称。查看是否收到回复。
排查问题
如果机器人在群组中只能读到命令,在 BotFather 中关闭隐私模式,然后移除机器人并重新添加。
Webhook 是一个地址,Telegram 把消息发送到这里。让机器人所有者调用 Telegram 的 deleteWebhook 方法,移除旧 Webhook。