mcpField
T
telegram-bot

telegram-bot

github.com/openclaudia营销与增长722498 次安装1 天前更新
代码仓库

关于 telegram-bot

面向 Telegram 频道和群组运营的营销技能,通过 Telegram Bot API 处理消息发布及常见管理操作。

主要功能

  1. 01发送文本、媒体和文件
  2. 02创建投票与问答
  3. 03添加内联键盘按钮
  4. 04编辑、删除和置顶消息
  5. 05使用 HTML 或 MarkdownV2 格式
  6. 06套用营销内容模板

适用场景

  1. 01在频道发布产品上线消息
  2. 02分享带行动号召的博客文章
  3. 03在群组发起互动投票
  4. 04发布定期社群动态

安装

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

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

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

详细介绍

Telegram 机器人技能

你是一名 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 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
通过 @BotFather 创建机器人

如果用户还没有机器人,请引导他们完成以下流程:

  1. 打开 Telegram 并搜索 @BotFather(官方机器人创建工具)。
  2. 向 BotFather 发送 /newbot。
  3. 为机器人选择一个显示名称(例如,“我的营销机器人”)。
  4. 选择一个以 bot 结尾的用户名(例如,my_marketing_bot)。
  5. BotFather 会回复一个类似 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11 的 API 令牌。
  6. 将令牌保存为 .env 或 ~/.claude/.env.global 中的 TELEGRAM_BOT_TOKEN。
  7. 将机器人添加为目标频道或群组的管理员。
  8. 也可以使用 BotFather 命令自定义机器人:
    • /setdescription - 设置机器人的描述
    • /setabouttext - 设置“关于”部分
    • /setuserpic - 上传机器人的头像
查找 Chat ID

对于公开频道,使用 @channel_username 作为 chat ID。

对于私人频道和群组,请获取数字 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

私人频道和群组的 ID 是负数(例如,-1001234567890)。

API 参考

所有 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
sendMessage - 文本消息

向频道或群组发送文本消息:

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"
  }'

**响应:**成功时返回一个 JSON 对象,其中 ok: true,并包含已发送的 message 对象。请检查 ok 以确认消息已送达。可以保存 message.message_id,以便稍后编辑或删除消息。

sendPhoto - 图片

通过 URL 或文件 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"

**图片限制:**文件最大为 10 MB。图片会被压缩。若要发送最大为 50 MB 的未压缩图片,请改用 sendDocument。

sendDocument - 文件和文档

发送任意文件(PDF、ZIP、未压缩图片等):

# 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"

**文档限制:**文件最大为 50 MB。

sendPoll - 投票和问答

创建互动投票以提高参与度:

# 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"
  }'

**投票限制:**问题文本为 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 - 在浏览器中打开 URL
  • callback_data - 将数据发送回机器人(需要通过 webhook 处理)
  • switch_inline_query - 提示用户选择聊天并发送内联查询
editMessageText - 编辑已有消息

更新之前发送的消息:

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"
  }'
deleteMessage - 删除消息

从频道中删除消息:

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
  }'
pinChatMessage - 置顶重要消息

将消息置顶到频道或群组顶部:

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
  }'

消息格式

HTML 模式(推荐)

设置 "parse_mode": "HTML" 并使用以下标签:

标签效果示例
<b>text</b>粗体<b>Important</b>
<i>text</i>斜体<i>Note:</i>
<u>text</u>下划线<u>highlight</u>
<s>text</s>删除线<s>old price</s>
<code>text</code>等宽字体<code>variable</code>
<pre>text</pre>代码块<pre>code block</pre>
<a href="url">text</a>链接<a href="https://example.com">点击这里</a>
<tg-emoji emoji-id="ID">emoji</tg-emoji>自定义表情Premium 功能
<blockquote>text</blockquote>引用块<blockquote>引用</blockquote>
<tg-spoiler>text</tg-spoiler>剧透内容<tg-spoiler>隐藏内容</tg-spoiler>

**HTML 转义规则:**将所有不属于 HTML 标签的文本中的 & 替换为 &amp;,< 替换为 &lt;,> 替换为 &gt;。无法识别的标签会被移除。标签必须正确闭合。

MarkdownV2 模式

设置 "parse_mode": "MarkdownV2" 并使用以下语法:

语法效果
*bold*粗体
_italic_斜体
__underline__下划线
~strikethrough~删除线
`code`等宽字体
```code block```代码块
[text](url)链接
`
>blockquote引用块(行首)

**MarkdownV2 转义规则:**在代码块之外,这些字符必须在前面加上反斜杠进行转义:_ * [ ] ( ) ~ > # + - = | { } . !。因此 MarkdownV2 容易出错。对于大多数用例,推荐使用 HTML 模式,以避免转义问题。

格式设置提示
  • 使用空行(\n\n)在视觉上分隔各个部分。
  • 表情符号可直接用于消息文本,无需特殊处理。
  • 可以组合格式:HTML 模式下 <b><i>粗斜体</i></b> 有效。
  • 可以将链接隐藏在文本后:<a href="https://example.com">点击这里</a>。
  • 如需发送静默消息(不发送通知),请在请求正文中添加 "disable_notification": true。

营销内容模板

模板 1:产品公告
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"}
        ]
      ]
    }
  }'
模板 2:博客文章分享
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"}
        ]
      ]
    }
  }'
模板 3:社区动态 / 新闻简报摘要
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 👇"
  }'
模板 4:附带图片的产品发布
# 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"}
        ]
      ]
    }
  }'
模板 5:互动投票
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
  }'
模板 6:知识问答
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
  }'
模板 7:活动 / 网络研讨会公告
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 没有内置的排期功能。请使用以下方法定时发送内容。

方法 1:使用 sleep 延迟发送(简单)

从终端发送一次性定时消息:

# 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"
  }'
方法 2:at 命令(指定时间)

为指定日期和时间安排消息:

# 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
方法 3:Cron 任务(定期发送)

用于定期发送消息(每日提示、每周摘要):

# 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
方法 4:批量内容队列

准备多条消息,并在每条消息之间添加延迟后发送:

# 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

发送到多个频道

如果用户管理多个频道,请接收聊天 ID 列表并向所有频道广播:

# 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)"
  }'

速率限制和最佳实践

Telegram API 速率限制
  • **发送到同一聊天:**每秒约 30 条消息(但应远低于此速率)。
  • **发送到不同聊天:**总计每秒约 30 条消息。
  • **批量通知:**向大量用户发送消息时,Telegram 建议每秒不超过 30 条。广播时,请在每次发送之间添加 sleep 1。
  • **文件上传:**文档每个文件最大 50 MB,照片最大 10 MB。
Telegram 频道内容最佳实践
实践详情
发布频率活跃频道每天发布 1-3 条。超过 5 条可能导致用户静音或取消订阅。
最佳时段受众主要时区的上午 9-11 点和下午 6-8 点。
消息长度信息流帖文控制在 1,000 个字符以内。文章可以较长。
媒体带图片的帖文获得的互动量是纯文本帖文的 2-3 倍。
排版关键内容使用粗体,使用项目符号列表以便快速浏览,并将链接放在末尾。
互动提出问题。每周发起投票。及时回复评论。
静默帖文对非紧急更新使用 disable_notification: true,避免打扰订阅者。
置顶消息置顶重要公告。取消置顶旧消息,确保置顶区域内容相关。
链接预览Telegram 会自动生成链接预览。若要禁用,请设置 disable_web_page_preview: true。
发布工作流

当用户要求在 Telegram 发布内容时:

  1. 检查凭据 - 确认已设置 TELEGRAM_BOT_TOKEN 和 TELEGRAM_CHAT_ID。
  2. 生成内容 - 使用合适的排版和模板撰写消息。
  3. 预览 - 向用户展示将要发送的确切消息,包括:
    • 带排版的消息文本
    • 任何内联键盘按钮
    • 媒体附件(URL 或文件路径)
    • 目标聊天 ID
  4. 确认 - 发送前请用户批准。
  5. 发送 - 执行 API 调用。
  6. 报告 - 显示响应,包括供后续操作(编辑、删除、置顶)使用的 message_id。

未经用户明确确认,绝不自动发布。

收集需求

撰写 Telegram 消息前,请收集以下信息:

  1. 消息类型 - 文本、照片、文档、投票或测验。
  2. 内容 - 消息主题是什么?请提供文案或用于生成内容的主题。
  3. 目标 - 要发送到哪个频道或群组?使用 TELEGRAM_CHAT_ID,或询问具体目标。
  4. 排版 - HTML 还是 MarkdownV2。默认使用 HTML。
  5. 按钮 - 是否需要 CTA 按钮?请提供每个按钮的标签和 URL。
  6. 媒体 - 是否要附加图片或文件?请提供 URL 或本地文件路径。
  7. 时间安排 - 立即发送、安排稍后发送,还是定期发送?
  8. 通知 - 静默(不发送通知)还是正常通知?

如果用户提供了博客文章 URL、文章或内容来源,请使用 WebFetch 获取内容,并据此生成合适的 Telegram 帖文。

错误处理

常见的 Telegram Bot API 错误及解决方法:

错误原因解决方法
401 UnauthorizedBot token 无效通过 @BotFather 重新生成 token
400 Bad Request: chat not found聊天 ID 错误,或 Bot 不在该聊天中验证聊天 ID;将 Bot 添加到频道并设为管理员
403 Forbidden: bot is not a memberBot 已被移出频道重新将 Bot 添加为频道管理员
403 Forbidden: bot can't send messagesBot 缺少发布权限授予 Bot“发布消息”管理员权限
429 Too Many Requests超出速率限制等待响应中指定的 retry_after 秒
400 Bad Request: can't parse entitiesHTML/Markdown 格式错误检查排版;转义特殊字符;切换到 HTML 模式

务必检查 API 响应中的 ok 字段。如果 ok 为 false,请向用户显示 description 字段,并提供问题的解决建议。

评论

支持 Markdown