telegram-bot
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCall the official Telegram Bot API with . The bot token is
injected as ; an optional default target chat is
(e.g. or a numeric id). Base URL:
.
curl + jq$TELEGRAMBOT_BOT_TOKEN$TELEGRAMBOT_CHAT_ID@your_channelhttps://api.telegram.org/bot$TELEGRAMBOT_BOT_TOKEN/<METHOD>Every response is JSON on success, or
on failure — always check
and show verbatim. Common failures: = bad/
revoked token (reconnect the connector), = wrong
chat_id, / = add the bot to the channel as an admin with post rights,
with = rate-limited, wait that many seconds.
{"ok":true,"result":...}{"ok":false,"error_code":<n>,"description":"<msg>"}.okdescription401 Unauthorized400 Bad Request: chat not found403 Forbidden: bot is not a member of the channel chat... need administrator rights429parameters.retry_afterchat_id@channelusername-1001234567890getChat使用调用官方Telegram Bot API。机器人令牌会被注入为;可选的默认目标聊天为(例如或数字ID)。基础URL:。
curl + jq$TELEGRAMBOT_BOT_TOKEN$TELEGRAMBOT_CHAT_ID@your_channelhttps://api.telegram.org/bot$TELEGRAMBOT_BOT_TOKEN/<METHOD>成功时,每个响应都是JSON格式的;失败时则为——务必检查字段,并原样显示内容。常见失败情况: = 令牌无效/已撤销(重新连接连接器), = chat_id错误, / = 将机器人添加为频道管理员并赋予发帖权限, 伴随 = 触发速率限制,等待指定秒数后重试。
{"ok":true,"result":...}{"ok":false,"error_code":<n>,"description":"<msg>"}.okdescription401 Unauthorized400 Bad Request: chat not found403 Forbidden: bot is not a member of the channel chat... need administrator rights429parameters.retry_afterchat_id@channelusername-1001234567890getChatStep 1 — verify the bot + target
步骤1 — 验证机器人与目标聊天
bash
BOT="https://api.telegram.org/bot$TELEGRAMBOT_BOT_TOKEN"
curl -sS "$BOT/getMe" | jq '{ok, bot: .result.username, name: .result.first_name}'bash
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}'
| jq '{ok, type: .result.type, title: .result.title, error: .description}'
undefinedCHAT="${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}'
| jq '{ok, type: .result.type, title: .result.title, error: .description}'
undefinedSend a text post
发送文本帖子
Confirm the text and target chat with the user before posting publicly. Text
is 1-4096 chars. Use (simplest) or .
parse_mode=HTMLMarkdownV2bash
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}'The public post URL for a channel is .
https://t.me/<channelusername>/<message_id>- HTML mode supports . Escape literal
<b> <i> <u> <s> <a href> <code> <pre> <blockquote>as< > &.< > & - MarkdownV2 requires escaping > # + - = | { } . !
_ * [ ] ( ) ~ \`.with - Always send via
textso newlines/Cyrillic/emoji aren't mangled.--data-urlencode
公开发帖前请与用户确认文本内容和目标聊天。 文本长度为1-4096字符。可使用(最简单)或。
parse_mode=HTMLMarkdownV2bash
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>转义为< > &。< > & - MarkdownV2模式需要使用转义
\> # + - = | { } . !`这些字符。_ * [ ] ( ) ~ \ - 务必通过发送
--data-urlencode参数,避免换行符、西里尔字母或表情符号出现乱码。text
Send a photo with caption
发送带说明文字的图片
bash
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}'photofile_idsendMediaGroupbash
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}'photofile_idsendMediaGroupEdit / delete a message
编辑/删除消息
bash
undefinedbash
undefinededit text (same chat_id + message_id)
编辑文本(需相同的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}'
--data-urlencode "chat_id=$CHAT" --data-urlencode "message_id=123"
--data-urlencode "text=Updated text" --data-urlencode "parse_mode=HTML"
| jq '{ok, error: .description}'
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}'
--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}'
--data-urlencode "chat_id=$CHAT" --data-urlencode "message_id=123"
| jq '{ok, error: .description}'
`deleteMessage` works for the bot's own channel posts within 48 hours (and needs
`can_delete_messages`/post rights).curl -sS -G "$BOT/deleteMessage"
--data-urlencode "chat_id=$CHAT" --data-urlencode "message_id=123"
| jq '{ok, error: .description}'
--data-urlencode "chat_id=$CHAT" --data-urlencode "message_id=123"
| jq '{ok, error: .description}'
`deleteMessage`方法仅能在48小时内删除机器人自身发布的频道帖子(且需要`can_delete_messages`权限或发帖权限)。Gotchas
注意事项
- The bot must be added to the channel/group as an admin with post rights, or
sends return .
403first to confirm access.getChat - Channel numeric ids are large negative numbers (); prefer
-100...for public channels.@username - Free broadcast limit is ~30 msg/s; space out bulk sends or you'll hit (respect
429).parameters.retry_after - This is the bot speaking (not your personal account) — for reading your own
DMs / groups as yourself, use the (MTProto) connector instead.
telegram
- 必须将机器人添加为频道/群组的管理员并赋予发帖权限,否则发送请求会返回错误。请先使用
403方法确认访问权限。getChat - 频道的数字ID为大负数();公开频道优先使用
-100...。@username - 免费广播限制约为30条/秒;批量发送时请间隔一段时间,否则会触发错误(请遵循
429指定的等待时间)。parameters.retry_after - 此操作是机器人执行的(而非你的个人账号)——如果需要以个人身份读取自己的私信/群组消息,请使用(MTProto)连接器。
telegram