mcpField
T
telegram-bot

telegram-bot

github.com/acedatacloud营销与增长1763 次安装2 天前更新
代码仓库

关于 telegram-bot

telegram-bot 是使用 curl 和 jq 调用官方 Telegram Bot API 的技能,面向需要通过机器人管理频道或群组消息的工作流。它涵盖发文、图片消息及消息编辑与删除,并要求公开发布前确认内容和目标。

主要功能

  1. 01调用官方 Telegram Bot API
  2. 02验证机器人与目标聊天
  3. 03发送文本和图片消息
  4. 04编辑或删除消息
  5. 05解析 API 错误响应

适用场景

  1. 01向频道发布文本动态
  2. 02发送带说明的图片
  3. 03修改或删除机器人发布的消息

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/acedatacloud/skills --skill telegram-bot

安装后在 Claude Code 中输入/telegram-bot确认已加载。

详细介绍

使用 curl + jq 调用 官方 Telegram Bot API。机器人令牌会注入为 $TELEGRAMBOT_BOT_TOKEN;可选的默认目标聊天为 $TELEGRAMBOT_CHAT_ID(例如 @your_channel 或数字 ID)。基础 URL: https://api.telegram.org/bot$TELEGRAMBOT_BOT_TOKEN/<METHOD>。

成功时,每个响应都是 JSON:{"ok":true,"result":...};失败时则为 {"ok":false,"error_code":<n>,"description":"<msg>"}——务必检查 .ok,并原样显示 description。常见错误:401 Unauthorized = 令牌错误或已撤销(重新连接此连接器);400 Bad Request: chat not found = chat_id 错误;403 Forbidden: bot is not a member of the channel chat / ... need administrator rights = 将机器人添加到频道,并授予可发帖的管理员权限;429 且带有 parameters.retry_after = 请求受限,等待指定秒数。

chat_id 可以是 @channelusername(公开频道),也可以是数字 ID(私有频道/群组,通常为负数,例如 -1001234567890)。使用 getChat 解析/验证。

步骤 1 — 验证机器人和目标

BOT="https://api.telegram.org/bot$TELEGRAMBOT_BOT_TOKEN"
curl -sS "$BOT/getMe" | jq '{ok, bot: .result.username, name: .result.first_name}'
# confirm the bot can post to the target chat (must be admin in the channel):
CHAT="${TELEGRAMBOT_CHAT_ID:-@your_channel}"
curl -sS -G "$BOT/getChat" --data-urlencode "chat_id=$CHAT" \
  | jq '{ok, type: .result.type, title: .result.title, error: .description}'

发送文本帖子

公开发布前,请先与用户确认文本和目标聊天。 文本长度为 1-4096 个字符。使用 parse_mode=HTML(最简单)或 MarkdownV2。

CHAT="${TELEGRAMBOT_CHAT_ID:-@your_channel}"
curl -sS -G "$BOT/sendMessage" \
  --data-urlencode "chat_id=$CHAT" \
  --data-urlencode "text=<b>Hello</b> from the bot 👋  #ai" \
  --data-urlencode "parse_mode=HTML" \
  --data-urlencode "disable_web_page_preview=false" \
  | jq '{ok, message_id: .result.message_id, error: .description}'

频道公开帖子的 URL 为 https://t.me/<channelusername>/<message_id>。

  • HTML 模式支持 <b> <i> <u> <s> <a href> <code> <pre> <blockquote>。 将字面量 < > & 转义为 &lt; &gt; &amp;。
  • MarkdownV2 要求使用 \ 转义 _ * [ ] ( ) ~ \ > # + - = | { } . !`。
  • 始终通过 --data-urlencode 发送 text,避免换行符、西里尔字母或表情符号被破坏。

发送带说明文字的照片

CHAT="${TELEGRAMBOT_CHAT_ID:-@your_channel}"
curl -sS -G "$BOT/sendPhoto" \
  --data-urlencode "chat_id=$CHAT" \
  --data-urlencode "photo=https://cdn.example.com/pic.jpg" \
  --data-urlencode "caption=<b>Caption</b> text" \
  --data-urlencode "parse_mode=HTML" \
  | jq '{ok, message_id: .result.message_id, error: .description}'

photo 接受 HTTPS URL(≤5 MB,Telegram 会获取该文件)、Telegram file_id 或上传的文件。说明文字长度为 0-1024 个字符。发送相册时使用 sendMediaGroup。

编辑 / 删除消息

# edit text (same chat_id + message_id)
curl -sS -G "$BOT/editMessageText" \
  --data-urlencode "chat_id=$CHAT" --data-urlencode "message_id=123" \
  --data-urlencode "text=Updated text" --data-urlencode "parse_mode=HTML" \
  | jq '{ok, error: .description}'

# delete a message
curl -sS -G "$BOT/deleteMessage" \
  --data-urlencode "chat_id=$CHAT" --data-urlencode "message_id=123" \
  | jq '{ok, error: .description}'

deleteMessage 可删除机器人在 48 小时内发送的频道帖子(并且需要 can_delete_messages / 发帖权限)。

注意事项

  • 必须将机器人添加到频道/群组并设为管理员,且授予发帖权限,否则发送会返回 403。先调用 getChat 确认访问权限。
  • 频道数字 ID 是较大的负数(-100...);公开频道优先使用 @username。
  • 免费广播限制约为 30 条消息/秒;批量发送时请控制间隔,否则会触发 429(遵循 parameters.retry_after)。
  • 发言者是机器人(不是你的个人账号)——若要以你本人身份读取私信/群组,请改用 telegram(MTProto)连接器。

评论

支持 Markdown