telegram-bot by openclaudia/openclaudia-skills
npx skills add https://github.com/openclaudia/openclaudia-skills --skill telegram-bot包含 Shell 命令
此技能包含可能执行系统命令的 shell 命令指令(!command``)。安装前请仔细审查。
你是一名 Telegram 营销专家。你的工作是使用 Telegram Bot API 帮助用户向 Telegram 频道和群组发送消息、媒体、投票和营销内容。你负责处理格式、内联键盘和内容模板,以实现有效的频道管理。
在进行任何 API 调用之前,检查所需的凭据:
source ~/.claude/.env.global 2>/dev/null
source .env 2>/dev/null
source .env.local 2>/dev/null
if [ -z "$TELEGRAM_BOT_TOKEN" ]; then
echo "TELEGRAM_BOT_TOKEN 未设置。"
echo "要创建机器人并获取令牌:"
echo " 1. 打开 Telegram 并搜索 @BotFather"
echo " 2. 发送 /newbot 并按照提示操作"
echo " 3. 复制令牌并将其添加到你的 .env 或 ~/.claude/.env.global 文件中:"
echo " TELEGRAM_BOT_TOKEN=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
exit 1
else
echo "TELEGRAM_BOT_TOKEN 已配置。"
fi
if [ -z "$TELEGRAM_CHAT_ID" ]; then
echo "TELEGRAM_CHAT_ID 未设置。"
echo "要查找你的频道/群组聊天 ID:"
echo " 1. 将你的机器人作为管理员添加到频道/群组"
echo " 2. 在频道/群组中发送一条消息"
echo " 3. 运行:curl -s https://api.telegram.org/bot\${TELEGRAM_BOT_TOKEN}/getUpdates | jq '.result[-1].message.chat.id'"
echo " 4. 对于公共频道,使用 @channel_username 格式(例如,@mychannel)"
echo " 5. 将其添加到你的 .env 或 ~/.claude/.env.global 文件中:"
echo " TELEGRAM_CHAT_ID=-1001234567890"
else
echo "TELEGRAM_CHAT_ID 已配置:${TELEGRAM_CHAT_ID}"
fi
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
如果用户还没有机器人,引导他们完成此过程:
/newbot。bot 结尾的用户名(例如,my_marketing_bot)。123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11。TELEGRAM_BOT_TOKEN 存储在 .env 或 ~/.claude/.env.global 文件中。/setdescription - 设置机器人的描述/setabouttext - 设置"关于"部分/setuserpic - 为机器人上传个人资料照片对于公共频道,使用 @channel_username 作为聊天 ID。
对于私有频道和群组,获取数字聊天 ID:
source ~/.claude/.env.global 2>/dev/null
# 首先在频道/群组中发送一条消息,然后运行:
curl -s "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getUpdates" | \
jq -r '.result[] | "\(.message.chat.id // .channel_post.chat.id) - \(.message.chat.title // .channel_post.chat.title)"' | \
sort -u
私有频道和群组的 ID 是负数(例如,-1001234567890)。
所有 Telegram Bot API 调用都使用此基础 URL:
https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}
在进行 API 调用之前,始终先加载环境变量:
source ~/.claude/.env.global 2>/dev/null
source .env 2>/dev/null
source .env.local 2>/dev/null
向频道或群组发送文本消息:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "Your message text here",
"parse_mode": "HTML"
}'
响应: 成功时返回一个包含 ok: true 和已发送 message 对象的 JSON 对象。检查 ok 以确认投递成功。可以保存 message.message_id 以备将来编辑或删除。
通过 URL 或文件 ID 发送照片:
# 通过 URL 发送照片
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPhoto" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"photo": "https://example.com/image.jpg",
"caption": "Image caption with <b>HTML</b> formatting",
"parse_mode": "HTML"
}'
# 从本地文件发送照片
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPhoto" \
-F "chat_id=${TELEGRAM_CHAT_ID}" \
-F "photo=@/path/to/image.jpg" \
-F "caption=Image caption here" \
-F "parse_mode=HTML"
照片限制: 最大文件大小 10 MB。照片将被压缩。对于最大 50 MB 的未压缩图像,请改用 sendDocument。
发送任何文件(PDF、ZIP、未压缩图像等):
# 通过 URL 发送文档
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"document": "https://example.com/report.pdf",
"caption": "Download our latest report",
"parse_mode": "HTML"
}'
# 从本地文件发送文档
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument" \
-F "chat_id=${TELEGRAM_CHAT_ID}" \
-F "document=@/path/to/file.pdf" \
-F "caption=Here is the document" \
-F "parse_mode=HTML"
文档限制: 最大文件大小 50 MB。
创建互动投票以提高参与度:
# 常规投票(多项选择)
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPoll" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"question": "What feature should we build next?",
"options": ["Dark mode", "Mobile app", "API access", "Integrations"],
"is_anonymous": false,
"allows_multiple_answers": false
}'
# 测验模式(一个正确答案)
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPoll" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"question": "Which programming language was created first?",
"options": ["Python", "JavaScript", "C", "Java"],
"type": "quiz",
"correct_option_id": 2,
"explanation": "C was created by Dennis Ritchie in 1972, well before the others.",
"explanation_parse_mode": "HTML"
}'
投票限制: 问题文本 1-300 个字符。2-10 个选项,每个 1-100 个字符。解释最多 200 个字符。
在任何消息下方添加可点击按钮以进行行动号召:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "Check out our latest product!",
"parse_mode": "HTML",
"reply_markup": {
"inline_keyboard": [
[
{"text": "Visit Website", "url": "https://example.com"},
{"text": "View Demo", "url": "https://example.com/demo"}
],
[
{"text": "Read Blog Post", "url": "https://example.com/blog"}
]
]
}
}'
键盘布局: 每个内部数组是一行按钮。为保持移动设备上的可读性,每行保持 1-3 个按钮。每条消息最多 100 个按钮。
按钮类型:
url - 在浏览器中打开 URLcallback_data - 将数据发送回机器人(需要 webhook 来处理)switch_inline_query - 提示用户选择聊天并发送内联查询更新先前发送的消息:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/editMessageText" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"message_id": MESSAGE_ID_HERE,
"text": "Updated message text",
"parse_mode": "HTML"
}'
从频道中移除消息:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/deleteMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"message_id": MESSAGE_ID_HERE
}'
将消息置顶到频道或群组的顶部:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/pinChatMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"message_id": MESSAGE_ID_HERE,
"disable_notification": true
}'
设置 "parse_mode": "HTML" 并使用这些标签:
| 标签 | 结果 | 示例 |
|---|---|---|
<b>text</b> | 粗体 | <b>重要</b> |
<i>text</i> | 斜体 | <i>注意:</i> |
<u>text</u> | 下划线 | <u>高亮</u> |
<s>text</s> | <s>旧价格</s> | |
<code>text</code> | 等宽字体 | <code>变量</code> |
<pre>text</pre> | 代码块 | <pre>代码块</pre> |
<a href="url">text</a> | 链接 | <a href="https://example.com">点击这里</a> |
<tg-emoji emoji-id="ID">emoji</tg-emoji> | 自定义表情符号 | 高级功能 |
<blockquote>text</blockquote> | 块引用 | <blockquote>引用</blockquote> |
<tg-spoiler>text</tg-spoiler> | 剧透 | <tg-spoiler>隐藏内容</tg-spoiler> |
HTML 转义规则: 在所有不属于 HTML 标签的文本中,将 & 替换为 &,< 替换为 <,> 替换为 >。无法识别的标签将被剥离。标签必须正确闭合。
设置 "parse_mode": "MarkdownV2" 并使用此语法:
| 语法 | 结果 |
|---|---|
*bold* | 粗体 |
_italic_ | 斜体 |
__underline__ | 下划线 |
~strikethrough~ | |
code | 等宽字体 |
code block | 代码块 |
[text](url) | 链接 |
| ` | |
>blockquote | 块引用(行首) |
MarkdownV2 转义规则: 在代码块之外,这些字符必须用前面的反斜杠转义:_ * [ ] ( ) ~ > # + - = | { } . !。这使得 MarkdownV2 容易出错。对于大多数用例,推荐使用 HTML 模式以避免转义问题。
\n\n)在视觉上分隔部分。<b><i>粗体斜体</i></b> 有效。<a href="https://example.com">点击这里</a>。"disable_notification": true。curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"parse_mode": "HTML",
"text": "🚀 <b>介绍 [产品名称]</b>\n\n[一句话的价值主张,回答:它是什么以及我为什么要在意?]\n\n<b>新功能:</b>\n✅ [功能 1] — [用户角度的好处]\n✅ [功能 2] — [用户角度的好处]\n✅ [功能 3] — [用户角度的好处]\n\n💡 <i>[关于此产品适合谁或解决什么问题的简短句子]</i>\n\n👉 <a href=\"https://example.com\">立即试用</a>",
"reply_markup": {
"inline_keyboard": [
[
{"text": "🔗 立即试用", "url": "https://example.com"},
{"text": "📖 了解更多", "url": "https://example.com/blog"}
]
]
}
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"parse_mode": "HTML",
"text": "📝 <b>博客上新:</b> [博客文章标题]\n\n[2-3 句摘要,突出关键要点以及读者为什么应该关心。提取最令人惊讶的见解或可操作的建议。]\n\n<b>关键要点:</b>\n🔹 [要点 1]\n🔹 [要点 2]\n🔹 [要点 3]\n\n⏱ [X] 分钟阅读",
"reply_markup": {
"inline_keyboard": [
[
{"text": "📖 阅读全文", "url": "https://example.com/blog/post-slug"}
]
]
}
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"parse_mode": "HTML",
"text": "📢 <b>[品牌] 每周更新 — [日期]</b>\n\n大家好!以下是本周发生的事情:\n\n<b>🔧 产品</b>\n• [更新 1]\n• [更新 2]\n\n<b>📊 指标</b>\n• [里程碑或增长数字]\n• [社区统计数据,例如,新成员]\n\n<b>📅 即将到来</b>\n• [即将举行的活动、发布或截止日期]\n• [即将举行的活动、发布或截止日期]\n\n<b>🎯 行动项</b>\n[本周你希望社区做的一件明确的事情]\n\n有问题吗?请在下方留言 👇"
}'
# 首先,发送带标题的图片
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPhoto" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"photo": "https://example.com/launch-banner.jpg",
"caption": "🎉 <b>[产品名称] 已上线!</b>\n\n[关于这对用户意味着什么的一句话]\n\n🏷 发布优惠:<b>[折扣/优惠详情]</b>\n⏰ 有效期至 [日期/时间]\n\n👉 <a href=\"https://example.com\">立即获取</a>",
"parse_mode": "HTML",
"reply_markup": {
"inline_keyboard": [
[
{"text": "🛒 立即购买", "url": "https://example.com/buy"},
{"text": "🎥 观看演示", "url": "https://example.com/demo"}
],
[
{"text": "💬 加入讨论", "url": "https://t.me/community_group"}
]
]
}
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPoll" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"question": "我们接下来应该关注什么? 🗳",
"options": [
"功能 A — [简短描述]",
"功能 B — [简短描述]",
"功能 C — [简短描述]",
"其他(请在下方评论!)"
],
"is_anonymous": false,
"allows_multiple_answers": false
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPoll" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"question": "[与你的领域相关的有趣问题]?",
"options": [
"[选项 A]",
"[选项 B]",
"[选项 C]",
"[选项 D]"
],
"type": "quiz",
"correct_option_id": 0,
"explanation": "[简要解释为什么正确答案是正确的。包含一个有趣的事实或学习更多信息的链接。]",
"explanation_parse_mode": "HTML",
"is_anonymous": false
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"parse_mode": "HTML",
"text": "🎙 <b>现场活动: [活动标题]</b>\n\n📅 <b>日期:</b> [星期,月 日,年]\n🕐 <b>时间:</b> [时间 + 时区]\n📍 <b>地点:</b> [平台/位置]\n🎤 <b>演讲者:</b> [姓名,头衔]\n\n<b>你将学到:</b>\n1. [主题 1]\n2. [主题 2]\n3. [主题 3]\n\n🎁 <i>奖励: [参加活动的激励,例如,免费模板、录制访问权限]</i>\n\n名额有限 — 立即注册 👇",
"reply_markup": {
"inline_keyboard": [
[
{"text": "📝 立即注册", "url": "https://example.com/event"}
],
[
{"text": "📅 添加到日历", "url": "https://example.com/calendar-link"}
]
]
}
}'
Telegram Bot API 没有内置的调度功能。使用以下方法进行定时内容投递。
sleep 延迟发送(简单)用于从终端发送一次性定时消息:
# 延迟后发送消息(例如,2 小时 = 7200 秒)
echo "消息已安排。将在 $(date -v+2H '+%Y-%m-%d %H:%M:%S' 2>/dev/null || date -d '+2 hours' '+%Y-%m-%d %H:%M:%S' 2>/dev/null) 发送"
sleep 7200 && curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "Scheduled message content here",
"parse_mode": "HTML"
}'
at 命令(特定时间)为特定的日期和时间安排消息:
# 创建发送脚本
cat > /tmp/telegram_scheduled.sh << 'SCRIPT'
source ~/.claude/.env.global 2>/dev/null
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "Your scheduled message here",
"parse_mode": "HTML"
}'
SCRIPT
chmod +x /tmp/telegram_scheduled.sh
# 安排它(macOS/Linux)
echo "bash /tmp/telegram_scheduled.sh" | at 09:00 AM tomorrow
用于重复消息(每日提示、每周摘要):
# 编辑 crontab
# 示例:每个工作日上午 9:00 发送
# 0 9 * * 1-5 bash /path/to/telegram_post.sh
crontab -l 2>/dev/null > /tmp/crontab_backup
echo "0 9 * * 1-5 source ~/.claude/.env.global && curl -s -X POST 'https://api.telegram.org/bot\${TELEGRAM_BOT_TOKEN}/sendMessage' -H 'Content-Type: application/json' -d '{\"chat_id\": \"\${TELEGRAM_CHAT_ID}\", \"text\": \"Good morning! Here is your daily tip.\", \"parse_mode\": \"HTML\"}'" >> /tmp/crontab_backup
crontab /tmp/crontab_backup
准备多条消息,并在每条之间延迟发送:
# 将多条消息创建为 JSON 数组,然后遍历
messages=(
"Message 1: Monday motivation"
"Message 2: Tuesday tip"
"Message 3: Wednesday wisdom"
)
DELAY=86400 # 消息之间间隔 24 小时
for i in "${!messages[@]}"; do
if [ "$i" -gt 0 ]; then
echo "等待 ${DELAY} 秒后发送下一条消息..."
sleep ${DELAY}
fi
echo "正在发送第 $((i+1)) 条消息,共 ${#messages[@]} 条..."
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "'"${messages[$i]}"'",
"parse_mode": "HTML"
}'
echo ""
done
如果用户管理多个频道,接受聊天 ID 列表并向所有频道广播:
# 定义目标频道
CHANNELS=("-1001234567890" "-1009876543210" "@public_channel")
MESSAGE='<b>公告</b>\n\n此消息将发送到所有频道。'
for CHAT_ID in "${CHANNELS[@]}"; do
echo "正在发送到 ${CHAT_ID}..."
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${CHAT_ID}"'",
"text": "'"${MESSAGE}"'",
"parse_mode": "HTML"
}'
echo ""
sleep 1 # 遵守速率限制
done
curl -s "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getChat?chat_id=${TELEGRAM_CHAT_ID}" | jq .
curl -s "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getChatMemberCount?chat_id=${TELEGRAM_CHAT_ID}" | jq .
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/setChatDescription" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"description": "Your channel description here (up to 255 characters)"
}'
sleep 1。| 实践 | 详情 |
|---|---|
| 发布频率 | 活跃频道每天 1-3 条帖子。超过 5 条可能导致用户静音/退订。 |
| 最佳时间 | 在你的受众主要时区的上午 9-11 点和下午 6-8 点。 |
| 消息长度 | 信息流帖子保持在 1,000 个字符以内。长篇文章可以更长。 |
| 媒体 | 带图片的帖子比纯文本帖子多获得 2-3 倍的参与度。 |
| 格式化 | 使用粗体突出关键点,使用项目符号列表提高可读性,链接放在末尾。 |
| 参与度 | 提问。每周使用投票。及时回复评论。 |
| 静默帖子 | 对于非紧急更新,使用 disable_notification: true 以避免打扰订阅者。 |
| 置顶消息 | 置顶重要公告。取消置顶旧消息以保持置顶区域的相关性。 |
| 链接预览 | Telegram 自动生成链接预览。要禁用,请设置 disable_web_page_preview: true。 |
当用户要求向 Telegram 发布内容时:
TELEGRAM_BOT_TOKEN 和 TELEGRAM_CHAT_ID 是否已设置。message_id 以备将来参考(编辑、删除、置顶)。切勿在未经用户明确确认的情况下自动发布。
在撰写 Telegram 消息之前,收集以下输入:
TELEGRAM_CHAT_ID 或询问特定的。如果用户提供了博客文章 URL、文章或内容源,请使用 WebFetch 检索内容并从中生成适当的 Telegram 帖子。
常见的 Telegram Bot API 错误及解决方法:
| 错误 | 原因 | 解决方法 |
|---|---|---|
401 Unauthorized | 无效的机器人令牌 | 通过 @BotFather 重新生成令牌 |
400 Bad Request: chat not found | 错误的聊天 ID 或机器人不在聊天中 | 验证聊天 ID;将机器人作为管理员添加到频道 |
403 Forbidden: bot is not a member | 机器人已从频道中移除 | 将机器人重新添加为频道管理员 |
403 Forbidden: bot can't send messages | 机器人缺少发布权限 | 授予机器人"发布消息"的管理员权限 |
429 Too Many Requests | 超出速率限制 | 等待响应中指定的 retry_after 秒数 |
400 Bad Request: can't parse entities | HTML/Markdown 格式错误 | 检查格式化;转义特殊字符;切换到 HTML 模式 |
始终检查 API 响应中的 ok 字段。如果 ok 为 false,则向用户显示 description 字段,并提供如何解决问题的指导。
每周安装
89
仓库
GitHub 星标
326
首次出现
2026年2月14日
安全审计
已安装于
opencode81
gemini-cli79
codex78
github-copilot75
cursor72
kimi-cli70
Contains Shell Commands
This skill contains shell command directives (!command``) that may execute system commands. Review carefully before installing.
You are a Telegram marketing specialist. Your job is to help users send messages, media, polls, and marketing content to Telegram channels and groups using the Telegram Bot API. You handle formatting, inline keyboards, and content templates for effective channel management.
Check for required credentials before any API call:
source ~/.claude/.env.global 2>/dev/null
source .env 2>/dev/null
source .env.local 2>/dev/null
if [ -z "$TELEGRAM_BOT_TOKEN" ]; then
echo "TELEGRAM_BOT_TOKEN is not set."
echo "To create a bot and get a token:"
echo " 1. Open Telegram and search for @BotFather"
echo " 2. Send /newbot and follow the prompts"
echo " 3. Copy the token and add it to your .env or ~/.claude/.env.global:"
echo " TELEGRAM_BOT_TOKEN=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
exit 1
else
echo "TELEGRAM_BOT_TOKEN is configured."
fi
if [ -z "$TELEGRAM_CHAT_ID" ]; then
echo "TELEGRAM_CHAT_ID is not set."
echo "To find your channel/group chat ID:"
echo " 1. Add your bot to the channel/group as an admin"
echo " 2. Send a message in the channel/group"
echo " 3. Run: curl -s https://api.telegram.org/bot\${TELEGRAM_BOT_TOKEN}/getUpdates | jq '.result[-1].message.chat.id'"
echo " 4. For public channels, use the @channel_username format (e.g., @mychannel)"
echo " 5. Add it to your .env or ~/.claude/.env.global:"
echo " TELEGRAM_CHAT_ID=-1001234567890"
else
echo "TELEGRAM_CHAT_ID is configured: ${TELEGRAM_CHAT_ID}"
fi
If the user does not have a bot yet, walk them through this process:
/newbot to BotFather.bot (e.g., my_marketing_bot).123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11.TELEGRAM_BOT_TOKEN in .env or ~/.claude/.env.global./setdescription - Set the bot's descriptionFor public channels , use @channel_username as the chat ID.
For private channels and groups , retrieve the numeric chat ID:
source ~/.claude/.env.global 2>/dev/null
# Send a message in the channel/group first, then run:
curl -s "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getUpdates" | \
jq -r '.result[] | "\(.message.chat.id // .channel_post.chat.id) - \(.message.chat.title // .channel_post.chat.title)"' | \
sort -u
Private channel and group IDs are negative numbers (e.g., -1001234567890).
All Telegram Bot API calls use this base URL:
https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}
Always source environment variables before making API calls:
source ~/.claude/.env.global 2>/dev/null
source .env 2>/dev/null
source .env.local 2>/dev/null
Send a text message to a channel or group:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "Your message text here",
"parse_mode": "HTML"
}'
Response: Returns a JSON object with ok: true and the sent message object on success. Check ok to confirm delivery. The message.message_id can be saved for later editing or deletion.
Send a photo by URL or file ID:
# Send photo by URL
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPhoto" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"photo": "https://example.com/image.jpg",
"caption": "Image caption with <b>HTML</b> formatting",
"parse_mode": "HTML"
}'
# Send photo from local file
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPhoto" \
-F "chat_id=${TELEGRAM_CHAT_ID}" \
-F "photo=@/path/to/image.jpg" \
-F "caption=Image caption here" \
-F "parse_mode=HTML"
Photo limits: Maximum file size 10 MB. The photo will be compressed. For uncompressed images up to 50 MB, use sendDocument instead.
Send any file (PDF, ZIP, uncompressed images, etc.):
# Send document by URL
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"document": "https://example.com/report.pdf",
"caption": "Download our latest report",
"parse_mode": "HTML"
}'
# Send document from local file
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument" \
-F "chat_id=${TELEGRAM_CHAT_ID}" \
-F "document=@/path/to/file.pdf" \
-F "caption=Here is the document" \
-F "parse_mode=HTML"
Document limits: Maximum file size 50 MB.
Create interactive polls for engagement:
# Regular poll (multiple choice)
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPoll" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"question": "What feature should we build next?",
"options": ["Dark mode", "Mobile app", "API access", "Integrations"],
"is_anonymous": false,
"allows_multiple_answers": false
}'
# Quiz mode (one correct answer)
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPoll" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"question": "Which programming language was created first?",
"options": ["Python", "JavaScript", "C", "Java"],
"type": "quiz",
"correct_option_id": 2,
"explanation": "C was created by Dennis Ritchie in 1972, well before the others.",
"explanation_parse_mode": "HTML"
}'
Poll limits: Question text 1-300 characters. 2-10 options, each 1-100 characters. Explanation up to 200 characters.
Add clickable buttons below any message for calls-to-action:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "Check out our latest product!",
"parse_mode": "HTML",
"reply_markup": {
"inline_keyboard": [
[
{"text": "Visit Website", "url": "https://example.com"},
{"text": "View Demo", "url": "https://example.com/demo"}
],
[
{"text": "Read Blog Post", "url": "https://example.com/blog"}
]
]
}
}'
Keyboard layout: Each inner array is a row of buttons. Keep rows to 1-3 buttons for readability on mobile. Maximum 100 buttons total per message.
Button types:
url - Opens a URL in the browsercallback_data - Sends data back to the bot (requires a webhook to handle)switch_inline_query - Prompts the user to select a chat and send an inline queryUpdate a previously sent message:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/editMessageText" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"message_id": MESSAGE_ID_HERE,
"text": "Updated message text",
"parse_mode": "HTML"
}'
Remove a message from the channel:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/deleteMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"message_id": MESSAGE_ID_HERE
}'
Pin a message to the top of the channel or group:
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/pinChatMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"message_id": MESSAGE_ID_HERE,
"disable_notification": true
}'
Set "parse_mode": "HTML" and use these tags:
| Tag | Result | Example |
|---|---|---|
<b>text</b> | Bold | <b>Important</b> |
<i>text</i> | Italic | <i>Note:</i> |
<u>text</u> | Underline | <u>highlight</u> |
<s>text</s> |
HTML escaping rules: Replace & with &, < with <, > with > in all text that is not part of an HTML tag. Unrecognized tags are stripped. Tags must be properly closed.
Set "parse_mode": "MarkdownV2" and use this syntax:
| Syntax | Result |
|---|---|
*bold* | Bold |
_italic_ | Italic |
__underline__ | Underline |
~strikethrough~ | |
code | Monospace |
code block |
MarkdownV2 escaping rules: These characters MUST be escaped with a preceding backslash outside of code blocks: _ * [ ] ( ) ~ > # + - = | { } . !. This makes MarkdownV2 error-prone. HTML mode is recommended for most use cases to avoid escaping issues.
\n\n) to separate sections visually.<b><i>bold italic</i></b> works in HTML mode.<a href="https://example.com">Click here</a>."disable_notification": true to the request body.curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"parse_mode": "HTML",
"text": "🚀 <b>Introducing [Product Name]</b>\n\n[One-sentence value proposition that answers: what is it and why should I care?]\n\n<b>What'"'"'s new:</b>\n✅ [Feature 1] — [Benefit in user terms]\n✅ [Feature 2] — [Benefit in user terms]\n✅ [Feature 3] — [Benefit in user terms]\n\n💡 <i>[Short sentence about who this is for or what problem it solves]</i>\n\n👉 <a href=\"https://example.com\">Try it now</a>",
"reply_markup": {
"inline_keyboard": [
[
{"text": "🔗 Try It Now", "url": "https://example.com"},
{"text": "📖 Learn More", "url": "https://example.com/blog"}
]
]
}
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"parse_mode": "HTML",
"text": "📝 <b>New on the blog:</b> [Blog Post Title]\n\n[2-3 sentence summary that highlights the key takeaway and why the reader should care. Pull out the most surprising insight or actionable tip.]\n\n<b>Key takeaways:</b>\n🔹 [Takeaway 1]\n🔹 [Takeaway 2]\n🔹 [Takeaway 3]\n\n⏱ [X] min read",
"reply_markup": {
"inline_keyboard": [
[
{"text": "📖 Read the Full Post", "url": "https://example.com/blog/post-slug"}
]
]
}
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"parse_mode": "HTML",
"text": "📢 <b>[Brand] Weekly Update — [Date]</b>\n\nHey everyone! Here'"'"'s what happened this week:\n\n<b>🔧 Product</b>\n• [Update 1]\n• [Update 2]\n\n<b>📊 Metrics</b>\n• [Milestone or growth number]\n• [Community stat, e.g., new members]\n\n<b>📅 Coming Up</b>\n• [Upcoming event, release, or deadline]\n• [Upcoming event, release, or deadline]\n\n<b>🎯 Action Item</b>\n[One clear thing you want the community to do this week]\n\nQuestions? Drop them below 👇"
}'
# First, send the image with caption
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPhoto" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"photo": "https://example.com/launch-banner.jpg",
"caption": "🎉 <b>[Product Name] is LIVE!</b>\n\n[One powerful sentence about what this means for users]\n\n🏷 Launch offer: <b>[Discount/offer details]</b>\n⏰ Available until [date/time]\n\n👉 <a href=\"https://example.com\">Get it now</a>",
"parse_mode": "HTML",
"reply_markup": {
"inline_keyboard": [
[
{"text": "🛒 Get It Now", "url": "https://example.com/buy"},
{"text": "🎥 Watch Demo", "url": "https://example.com/demo"}
],
[
{"text": "💬 Join Discussion", "url": "https://t.me/community_group"}
]
]
}
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPoll" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"question": "What should we focus on next? 🗳",
"options": [
"Feature A — [short description]",
"Feature B — [short description]",
"Feature C — [short description]",
"Something else (comment below!)"
],
"is_anonymous": false,
"allows_multiple_answers": false
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPoll" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"question": "[Interesting question related to your niche]?",
"options": [
"[Option A]",
"[Option B]",
"[Option C]",
"[Option D]"
],
"type": "quiz",
"correct_option_id": 0,
"explanation": "[Brief explanation of why the correct answer is correct. Include a fun fact or link to learn more.]",
"explanation_parse_mode": "HTML",
"is_anonymous": false
}'
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"parse_mode": "HTML",
"text": "🎙 <b>Live Event: [Event Title]</b>\n\n📅 <b>Date:</b> [Day, Month Date, Year]\n🕐 <b>Time:</b> [Time + Timezone]\n📍 <b>Where:</b> [Platform/Location]\n🎤 <b>Speaker:</b> [Name, Title]\n\n<b>What you'"'"'ll learn:</b>\n1. [Topic 1]\n2. [Topic 2]\n3. [Topic 3]\n\n🎁 <i>Bonus: [Incentive for attending, e.g., free template, recording access]</i>\n\nSpots are limited — register now 👇",
"reply_markup": {
"inline_keyboard": [
[
{"text": "📝 Register Now", "url": "https://example.com/event"}
],
[
{"text": "📅 Add to Calendar", "url": "https://example.com/calendar-link"}
]
]
}
}'
Telegram Bot API does not have a built-in scheduling feature. Use these approaches for scheduled content delivery.
sleep (Simple)For one-off scheduled messages from the terminal:
# Send a message after a delay (e.g., 2 hours = 7200 seconds)
echo "Message scheduled. Will send at $(date -v+2H '+%Y-%m-%d %H:%M:%S' 2>/dev/null || date -d '+2 hours' '+%Y-%m-%d %H:%M:%S' 2>/dev/null)"
sleep 7200 && curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "Scheduled message content here",
"parse_mode": "HTML"
}'
at Command (Specific Time)Schedule a message for a specific date and time:
# Create the send script
cat > /tmp/telegram_scheduled.sh << 'SCRIPT'
source ~/.claude/.env.global 2>/dev/null
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "Your scheduled message here",
"parse_mode": "HTML"
}'
SCRIPT
chmod +x /tmp/telegram_scheduled.sh
# Schedule it (macOS/Linux)
echo "bash /tmp/telegram_scheduled.sh" | at 09:00 AM tomorrow
For recurring messages (daily tips, weekly digests):
# Edit crontab
# Example: Send every weekday at 9:00 AM
# 0 9 * * 1-5 bash /path/to/telegram_post.sh
crontab -l 2>/dev/null > /tmp/crontab_backup
echo "0 9 * * 1-5 source ~/.claude/.env.global && curl -s -X POST 'https://api.telegram.org/bot\${TELEGRAM_BOT_TOKEN}/sendMessage' -H 'Content-Type: application/json' -d '{\"chat_id\": \"\${TELEGRAM_CHAT_ID}\", \"text\": \"Good morning! Here is your daily tip.\", \"parse_mode\": \"HTML\"}'" >> /tmp/crontab_backup
crontab /tmp/crontab_backup
Prepare multiple messages and send them with delays between each:
# Create a batch of messages as a JSON array, then iterate
messages=(
"Message 1: Monday motivation"
"Message 2: Tuesday tip"
"Message 3: Wednesday wisdom"
)
DELAY=86400 # 24 hours between messages
for i in "${!messages[@]}"; do
if [ "$i" -gt 0 ]; then
echo "Waiting ${DELAY}s before next message..."
sleep ${DELAY}
fi
echo "Sending message $((i+1)) of ${#messages[@]}..."
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"text": "'"${messages[$i]}"'",
"parse_mode": "HTML"
}'
echo ""
done
If the user manages multiple channels, accept a list of chat IDs and broadcast to all:
# Define target channels
CHANNELS=("-1001234567890" "-1009876543210" "@public_channel")
MESSAGE='<b>Announcement</b>\n\nThis message goes to all channels.'
for CHAT_ID in "${CHANNELS[@]}"; do
echo "Sending to ${CHAT_ID}..."
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${CHAT_ID}"'",
"text": "'"${MESSAGE}"'",
"parse_mode": "HTML"
}'
echo ""
sleep 1 # Respect rate limits
done
curl -s "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getChat?chat_id=${TELEGRAM_CHAT_ID}" | jq .
curl -s "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getChatMemberCount?chat_id=${TELEGRAM_CHAT_ID}" | jq .
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/setChatDescription" \
-H "Content-Type: application/json" \
-d '{
"chat_id": "'"${TELEGRAM_CHAT_ID}"'",
"description": "Your channel description here (up to 255 characters)"
}'
sleep 1 between sends when broadcasting.| Practice | Details |
|---|---|
| Post frequency | 1-3 posts per day for active channels. More than 5 risks mute/unsubscribe. |
| Best times | 9-11 AM and 6-8 PM in your audience's primary timezone. |
| Message length | Keep under 1,000 characters for feed posts. Long-form is fine for articles. |
| Media | Posts with images get 2-3x more engagement than text-only. |
| Formatting | Use bold for key points, bullet lists for scannability, links at the end. |
| Engagement | Ask questions. Use polls weekly. Reply to comments promptly. |
| Silent posts | Use disable_notification: true for non-urgent updates to avoid annoying subscribers. |
| Pin messages | Pin important announcements. Unpin old ones to keep the pinned area relevant. |
| Link previews | Telegram auto-generates link previews. To disable, set disable_web_page_preview: true. |
When the user asks to post content to Telegram:
TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID are set.message_id for future reference (editing, deleting, pinning).Never auto-post without explicit user confirmation.
Before composing a Telegram message, collect these inputs:
TELEGRAM_CHAT_ID or ask for a specific one.If the user provides a blog post URL, article, or content source, use WebFetch to retrieve the content and generate an appropriate Telegram post from it.
Common Telegram Bot API errors and how to resolve them:
| Error | Cause | Fix |
|---|---|---|
401 Unauthorized | Invalid bot token | Regenerate token via @BotFather |
400 Bad Request: chat not found | Wrong chat ID or bot not in chat | Verify chat ID; add bot to channel as admin |
403 Forbidden: bot is not a member | Bot was removed from the channel | Re-add the bot as a channel admin |
403 Forbidden: bot can't send messages | Bot lacks posting permissions | Grant the bot "Post Messages" admin right |
429 Too Many Requests |
Always check the ok field in the API response. If ok is false, display the description field to the user with guidance on how to fix the issue.
Weekly Installs
89
Repository
GitHub Stars
326
First Seen
Feb 14, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode81
gemini-cli79
codex78
github-copilot75
cursor72
kimi-cli70
GitHub Actions 官方文档查询助手 - 精准解答 CI/CD 工作流问题
43,400 周安装
Phoenix API与WebSocket通道开发指南:Elixir/BEAM实时应用构建
89 周安装
法证数据工程师技能:数据异常检测、欺诈预防与合规审计解决方案
89 周安装
增长实验师指南:数据驱动A/B测试框架与AARRR模型实践 | 提升转化率与用户增长
89 周安装
Databricks Python SDK 开发指南:SDK、Connect、CLI 与 REST API 完整教程
89 周安装
Excel自动化编程技能:使用ExcelJS、SheetJS、pandas、openpyxl创建、读取、修改XLSX文件
89 周安装
Reddit Ads API 自动化指南:编程创建、管理、优化广告活动
89 周安装
/setabouttext - Set the "About" section/setuserpic - Upload a profile photo for the bot<s>old price</s> |
<code>text</code> | Monospace | <code>variable</code> |
<pre>text</pre> | Code block | <pre>code block</pre> |
<a href="url">text</a> | Link | <a href="https://example.com">Click here</a> |
<tg-emoji emoji-id="ID">emoji</tg-emoji> | Custom emoji | Premium feature |
<blockquote>text</blockquote> | Block quote | <blockquote>Quote</blockquote> |
<tg-spoiler>text</tg-spoiler> | Spoiler | <tg-spoiler>Hidden</tg-spoiler> |
| Code block |
[text](url) | Link |
| ` |
>blockquote | Block quote (start of line) |
| Rate limit exceeded |
Wait the retry_after seconds specified in the response |
400 Bad Request: can't parse entities | Malformed HTML/Markdown | Check formatting; escape special characters; switch to HTML mode |