Telegram Bot Builder
它能做什么
这项技能指导你的智能体创建能与用户聊天的 Telegram 机器人。涵盖通过 BotFather 设置、消息处理、带按钮的菜单,以及让机器人上线运行。
适用场景
- ✓当你想为客户或社群创建 Telegram 机器人或聊天机器人时。
- ✓当你想为现有 Telegram 机器人添加按钮、菜单或媒体时。
- ✓当你需要使用 Webhook 或长轮询部署机器人的时候。
技能链接
https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/development/telegram-bot-builder/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请为我构建一个 Telegram 机器人,回答客户问题,并显示带按钮的菜单。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体使用你从 BotFather 获得的机器人令牌与 Telegram 的 Bot API 通信,为消息和按钮点击构建处理程序,并设置机器人接收更新的方式。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Telegram Bot Builder
Comprehensive guidance for building Telegram bots using the Bot API (v9.4). Covers both Node.js and Python ecosystems with production-ready patterns for authentication, messaging, keyboards, media handling, payments, inline mode, webhooks, and deployment.
When to Use This Skill
Use this skill when:
- Building a new Telegram bot from scratch
- Integrating Telegram messaging into an existing application
- Setting up webhooks or long polling for bot updates
- Creating interactive menus with inline keyboards and callback queries
- Handling media (photos, videos, documents, stickers)
- Implementing Telegram Payments or Telegram Stars
- Building inline mode functionality
- Managing groups, channels, or forum topics via bot
- Deploying bots to production (Docker, PM2, serverless)
Core Concepts
Authentication
Every bot has a unique token obtained from @BotFather. Token format: 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11.
All API calls go to: https://api.telegram.org/bot<TOKEN>/METHOD_NAME
# .env file
BOT_TOKEN=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
Store the token in environment variables. Never commit it to source code.
Receiving Updates: Polling vs Webhook
Long Polling (getUpdates) - Simpler, no HTTPS required, ideal for development:
// Node.js with node-telegram-bot-api
const bot = new TelegramBot(process.env.BOT_TOKEN, { polling: true });
# Python with python-telegram-bot
app = Application.builder().token(os.getenv("BOT_TOKEN")).build()
app.run_polling()
Webhook (setWebhook) - Better for production, lower latency, requires HTTPS (ports 443, 80, 88, or 8443):
bot.setWebHook('https://yourdomain.com/webhook', { secret_token: SECRET });
Choose polling for development and small bots. Choose webhooks for production deployments handling high traffic.
Message Types & Formatting
Send text with sendMessage. Supported parse modes:
- HTML:
<b>bold</b>,<i>italic</i>,<code>code</code>,<pre>block</pre>,<a href="url">link</a>,<tg-spoiler>spoiler</tg-spoiler> - MarkdownV2:
*bold*,_italic_,`code`,```block```,[link](url),||spoiler||. Requires escaping:_*[]()~>#+-=|{}.!
Prefer HTML for easier escaping. Use MarkdownV2 when simpler formatting suffices.
Keyboards & Interactive Elements
Inline Keyboard - Buttons attached to messages:
bot.sendMessage(chatId, 'Choose:', {
reply_markup: {
inline_keyboard: [
[{ text: 'Option A', callback_data: 'a' }, { text: 'Option B', callback_data: 'b' }],
[{ text: 'Visit Site', url: 'https://example.com' }]
]
}
});
Reply Keyboard - Custom keyboard below input field:
bot.sendMessage(chatId, 'Choose:', {
reply_markup: {
keyboard: [[{ text: '📊 Stats' }, { text: '⚙️ Settings' }]],
resize_keyboard: true,
one_time_keyboard: true
}
});
Handle inline button presses with callback_query. The callback_data field is limited to 64 bytes. Always call answerCallbackQuery to dismiss the loading indicator.
Sending Media
// Photo (file_id, URL, or upload)
bot.sendPhoto(chatId, 'https://example.com/photo.jpg', { caption: 'A photo' });
// Document
bot.sendDocument(chatId, fs.createReadStream('./file.pdf'), { caption: 'Report' });
// Album (2-10 items)
bot.sendMediaGroup(chatId, [
{ type: 'photo', media: 'https://example.com/1.jpg', caption: 'First' },
{ type: 'photo', media: 'https://example.com/2.jpg' }
]);
Three ways to specify files: file_id (reuse previously uploaded), HTTP URL (Telegram downloads it), or multipart upload. File limits: 50MB upload, 20MB download via Bot API.
Conversation State
For multi-step interactions (registration, forms, wizards), maintain conversation state per chat:
- Node.js: Use a
Mapor Redis to track{ step, data }perchatId - Python: Use
ConversationHandlerfrompython-telegram-bot(built-in state machine)
See reference/patterns_and_examples.md for complete conversation flow implementations.
Error Handling
Handle common error scenarios:
- 429 Too Many Requests: Read
retry_afterfrom response, wait, then retry - 403 Forbidden: Bot was blocked by user or removed from chat
- 400 Bad Request: Invalid parameters (check
descriptionfield) - 409 Conflict: Another bot instance using same token with polling
常见问题
在哪里获得机器人令牌?
你通过 Telegram 上的 BotFather 创建机器人,它会给你一个令牌,你需要将其作为机密设置保存。
轮询和 Webhook 有什么区别?
轮询是机器人主动向 Telegram 询问新消息,开发时更简单。Webhook 是 Telegram 通过 HTTPS 把更新发送到你的服务器。
机器人能收款吗?
这项技能涵盖 Telegram Payments 和 Telegram Stars,因此指南中包括这些内容。
详情
作者的原始描述
This skill should be used when the user asks to "create a Telegram bot", "build a Telegram chatbot", "set up a Telegram webhook", "add inline keyboards to a bot", "handle Telegram callback queries", "implement Telegram payments", "send media via Telegram bot", "configure Telegram bot commands", "deploy a Telegram bot", or mentions the Telegram Bot API, telegram bot tokens, getUpdates, setWebhook, or bot frameworks like node-telegram-bot-api, grammy, python-telegram-bot, or aiogram. Provides comprehensive guidance for building production-ready Telegram bots with Node.js and Python.
热度
- GitHub 星标
- 33k
- 排名
- #8805
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
owasp-security
33k教你的智能体掌握面向网页应用、API、移动端、容器和 AI 智能体的 OWASP 安全指南,以便发现常见弱点并提出修复建议。
agent-management
33k让你的智能体使用 AI Maestro 命令行工具创建、列出、休眠、唤醒、重命名和删除 AI 智能体,管理每个智能体的生命周期。
agent-messaging
33k让你的智能体使用智能体消息协议,在 AI 智能体之间发送、接收和回复带签名的消息,大部分在你自己的电脑上完成。
docs-search
33k让你的智能体搜索自动生成的代码文档,查找函数签名、类定义和注释,从而在修改代码之前核对真实的 API。