Telegram Bot API Skill
Use this skill to run Telegram Bot API operations through uxc + OpenAPI.
Reuse the uxc skill for shared execution, auth, and error-handling guidance.
Prerequisites
- -
uxc is installed and available in PATH. - Network access to
https://api.telegram.org. - Access to the curated OpenAPI schema URL:
-
https://raw.githubusercontent.com/holon-run/uxc/main/skills/telegram-openapi-skill/references/telegram-bot.openapi.json
- - A Telegram bot token from BotFather.
Scope
This skill covers a lean bot core surface:
- - bot identity and chat lookup
- text sends
- media sends by
file_id, HTTP URL, or local multipart upload - polling via INLINECODE7
- webhook setup/status/delete operations
This skill does not cover:
- - multipart media groups with
attach:// file arrays - generic webhook ingestion/runtime hosting
- the full Telegram Bot API surface
Authentication
Telegram Bot API requires the bot token in the request path: https://api.telegram.org/bot<TOKEN>/METHOD_NAME.
Configure the credential with a request path prefix template:
CODEBLOCK0
Validate the local mapping when auth looks wrong:
CODEBLOCK1
Core Workflow
- 1. Use the fixed link command by default:
-
command -v telegram-openapi-cli
- If missing, create it:
uxc link telegram-openapi-cli https://api.telegram.org --schema-url https://raw.githubusercontent.com/holon-run/uxc/main/skills/telegram-openapi-skill/references/telegram-bot.openapi.json
- INLINECODE12
- 2. Inspect operation schema first:
-
telegram-openapi-cli get:/getMe -h
-
telegram-openapi-cli post:/sendMessage -h
-
telegram-openapi-cli post:/sendPhoto -h
-
telegram-openapi-cli post:/sendDocument -h
- INLINECODE17
- 3. Prefer read/setup validation before writes:
-
telegram-openapi-cli get:/getMe
-
telegram-openapi-cli get:/getWebhookInfo
- INLINECODE20
- 4. Execute operations with key/value or positional JSON:
- key/value:
telegram-openapi-cli post:/sendMessage chat_id=CHAT_ID text="Hello from uxc"
- multipart upload:
telegram-openapi-cli post:/sendPhoto chat_id=CHAT_ID photo=/tmp/photo.jpg caption="Uploaded by uxc"
- positional JSON:
telegram-openapi-cli post:/sendMessage '{"chat_id":"CHAT_ID","text":"Hello from uxc"}'
- daemon-backed polling subscribe:
INLINECODE24
Runtime Validation
The following Telegram polling flow has been validated against the real Bot API through uxc:
- - INLINECODE26
- INLINECODE27
- daemon-backed
uxc subscribe --mode poll on INLINECODE29 - item-derived offset progression from INLINECODE30
- dedupe/checkpoint behavior for repeated polls
Observed runtime behavior:
- -
data events are emitted for real Telegram updates - INLINECODE32 events record fetched/emitted/skipped counts
- INLINECODE33 events are emitted after new updates are seen
- repeated polls skip already-consumed updates after checkpoint advancement
Operation Groups
Read / Lookup
- - INLINECODE34
- INLINECODE35
- INLINECODE36
- INLINECODE37
Messaging
- - INLINECODE38
- INLINECODE39
- INLINECODE40
- INLINECODE41
Update Delivery
- - INLINECODE42
- INLINECODE43
- INLINECODE44
Guardrails
- - Keep automation on the JSON output envelope; do not use
--text. - Parse stable fields first:
ok, kind, protocol, data, error. - INLINECODE51 and webhook delivery are mutually exclusive:
- if a webhook is configured, call
post:/deleteWebhook before polling with
post:/getUpdates
- if polling is active, do not treat webhook operations as background subscription support
- - Telegram allows only one active
getUpdates consumer per bot token:
- if another bot process or script is polling at the same time, Telegram returns HTTP 409
- stop the other consumer before relying on daemon-backed polling subscribe
- - For daemon-backed polling subscribe, prefer item-derived offset progression:
-
extract_items_pointer should be
/result
-
request_cursor_arg should be
offset
-
cursor_from_item_pointer should be
/update_id
-
cursor_transform should be
increment
-
checkpoint_strategy.type should usually be
item_key with
item_key_pointer=/update_id
- -
uxc auth binding match should be checked against a concrete Telegram method URL such as https://api.telegram.org/getMe, because auth is applied through a path-prefix template that expands to /bot<TOKEN>/.... - INLINECODE69 ,
sendDocument, and sendMediaGroup in this skill accept existing file_id values or HTTP URLs only; they do not upload new local files. - INLINECODE73 and
sendDocument also support multipart/form-data local file uploads. File fields must be local path strings. - INLINECODE76 still stays JSON-only in this skill because current multipart v1 does not model the
media array plus attach:// file set cleanly. - INLINECODE79 supports multipart certificate upload for self-signed certs through the
certificate file field. - Treat
post:/sendMessage, all send* operations, and webhook-changing operations as write/high-risk actions; require explicit user confirmation before execution. - INLINECODE83 is equivalent to
uxc https://api.telegram.org --schema-url <telegram_openapi_schema> <operation> ....
References
- - Usage patterns: INLINECODE85
- Curated OpenAPI schema: INLINECODE86
- Telegram Bot API docs: https://core.telegram.org/bots/api
- Local Bot API server: https://github.com/tdlib/telegram-bot-api
Telegram Bot API 技能
使用此技能通过 uxc + OpenAPI 运行 Telegram Bot API 操作。
复用 uxc 技能以获取共享执行、认证和错误处理指导。
前提条件
- - uxc 已安装并可在 PATH 中使用。
- 可访问 https://api.telegram.org 的网络连接。
- 可访问精选的 OpenAPI 模式 URL:
- https://raw.githubusercontent.com/holon-run/uxc/main/skills/telegram-openapi-skill/references/telegram-bot.openapi.json
- - 从 BotFather 获取的 Telegram 机器人令牌。
范围
此技能涵盖精简的机器人核心功能:
- - 机器人身份和聊天查找
- 文本发送
- 通过 file_id、HTTP URL 或本地 multipart 上传的媒体发送
- 通过 getUpdates 轮询
- Webhook 设置/状态/删除操作
此技能不涵盖:
- - 使用 attach:// 文件数组的 multipart 媒体组
- 通用 webhook 接收/运行时托管
- 完整的 Telegram Bot API 功能
认证
Telegram Bot API 需要在请求路径中包含机器人令牌:https://api.telegram.org/bot/METHOD_NAME。
使用请求路径前缀模板配置凭据:
bash
uxc auth credential set telegram-bot \
--auth-type api_key \
--secret-env TELEGRAMBOTTOKEN \
--path-prefix-template /bot{{secret}}
uxc auth binding add \
--id telegram-bot \
--host api.telegram.org \
--scheme https \
--credential telegram-bot \
--priority 100
当认证出现问题时,验证本地映射:
bash
uxc auth binding match https://api.telegram.org/getMe
核心工作流程
- 1. 默认使用固定链接命令:
- command -v telegram-openapi-cli
- 如果缺失,创建它:
uxc link telegram-openapi-cli https://api.telegram.org --schema-url https://raw.githubusercontent.com/holon-run/uxc/main/skills/telegram-openapi-skill/references/telegram-bot.openapi.json
- telegram-openapi-cli -h
- 2. 首先检查操作模式:
- telegram-openapi-cli get:/getMe -h
- telegram-openapi-cli post:/sendMessage -h
- telegram-openapi-cli post:/sendPhoto -h
- telegram-openapi-cli post:/sendDocument -h
- telegram-openapi-cli post:/getUpdates -h
- 3. 在写入操作前优先进行读取/设置验证:
- telegram-openapi-cli get:/getMe
- telegram-openapi-cli get:/getWebhookInfo
- telegram-openapi-cli get:/getChat chat
id=@channelor
chatid
- 4. 使用键/值或位置 JSON 执行操作:
- 键/值:
telegram-openapi-cli post:/sendMessage chat
id=CHATID text=来自 uxc 的问候
- multipart 上传:
telegram-openapi-cli post:/sendPhoto chat
id=CHATID photo=/tmp/photo.jpg caption=由 uxc 上传
- 位置 JSON:
telegram-openapi-cli post:/sendMessage {chat
id:CHATID,text:来自 uxc 的问候}
- 守护进程支持的轮询订阅:
uxc subscribe start https://api.telegram.org post:/getUpdates {timeout:5,allowed
updates:[message,callbackquery]} --mode poll --poll-config {interval
secs:2,extractitems
pointer:/result,requestcursor
arg:offset,cursorfrom
itempointer:/update
id,cursortransform:increment,checkpoint
strategy:{type:itemkey,item
keypointer:/update_id}} --sink file:/tmp/telegram-updates.ndjson
运行时验证
以下 Telegram 轮询流程已通过 uxc 针对真实 Bot API 验证:
- - get:/getMe
- get:/getWebhookInfo
- 守护进程支持的 uxc subscribe --mode poll 在 post:/getUpdates 上
- 从 update_id + 1 派生的基于项目的偏移量推进
- 重复轮询的去重/检查点行为
观察到的运行时行为:
- - 为真实的 Telegram 更新发出 data 事件
- poll 事件记录获取/发出/跳过的计数
- 在发现新更新后发出 checkpoint 事件
- 重复轮询在检查点推进后跳过已消费的更新
操作组
读取/查找
- - get:/getMe
- get:/getChat
- get:/getChatMember
- get:/getWebhookInfo
消息发送
- - post:/sendMessage
- post:/sendPhoto
- post:/sendDocument
- post:/sendMediaGroup
更新投递
- - post:/getUpdates
- post:/setWebhook
- post:/deleteWebhook
防护措施
- - 保持自动化在 JSON 输出封装上;不要使用 --text。
- 首先解析稳定字段:ok、kind、protocol、data、error。
- getUpdates 和 webhook 投递互斥:
- 如果配置了 webhook,在通过 post:/getUpdates 轮询前调用 post:/deleteWebhook
- 如果轮询处于活动状态,不要将 webhook 操作视为后台订阅支持
- - Telegram 每个机器人令牌只允许一个活动的 getUpdates 消费者:
- 如果另一个机器人进程或脚本同时轮询,Telegram 返回 HTTP 409
- 在依赖守护进程支持的轮询订阅前停止其他消费者
- - 对于守护进程支持的轮询订阅,优先使用基于项目的偏移量推进:
- extract
itemspointer 应为 /result
- request
cursorarg 应为 offset
- cursor
fromitem
pointer 应为 /updateid
- cursor_transform 应为 increment
- checkpoint
strategy.type 通常应为 itemkey,item
keypointer=/update_id
- - uxc auth binding match 应针对具体的 Telegram 方法 URL(如 https://api.telegram.org/getMe)进行检查,因为认证通过路径前缀模板应用,该模板扩展为 /bot/...。
- 此技能中的 sendPhoto、sendDocument 和 sendMediaGroup 仅接受现有的 fileid 值或 HTTP URL;它们不上传新的本地文件。
- sendPhoto 和 sendDocument 也支持 multipart/form-data 本地文件上传。文件字段必须是本地路径字符串。
- 此技能中的 sendMediaGroup 仍保持仅 JSON,因为当前的 multipart v1 无法干净地建模 media 数组加 attach:// 文件集。
- setWebhook 支持通过 certificate 文件字段进行自签名证书的 multipart 证书上传。
- 将 post:/sendMessage、所有 send* 操作和更改 webhook 的操作视为写入/高风险操作;在执行前需要明确的用户确认。
- telegram-openapi-cli ... 等同于 uxc https://api.telegram.org --schema-url openapi_schema> ...。
参考资料
- - 使用模式:references/usage-patterns.md
- 精选的 OpenAPI 模式:references/telegram-bot.openapi.json
- Telegram Bot API 文档:https://core.telegram.org/bots/api
- 本地 Bot API 服务器:https://github.com/tdlib/telegram-bot-api