GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Telegram Bot Manager
  • 设置 Webhook
  • 检查 Webhook(getWebhookInfo)
  • 删除 Webhook
  • 机器人命令编辑器
  • English
  • Русский
  • Español
  • Português

This view

  • Original page
  • New address

GeezerVison is showing f1devbin.github.io — 如何在线设置 Telegram 机器人 Webhook(setWebhook)

如何设置 Telegram 机器人 Webhook

使用 Webhook 后,Telegram 会把每条更新推送到你的 HTTPS 地址,无需轮询 getUpdates。下面介绍如何正确、安全地进行设置。

几次点击即可完成——无需写代码、无需服务器,直接在浏览器中操作。

打开 Webhook 管理器

要求

  • 公网可访问的 HTTPS 地址,不支持普通 HTTP。
  • 端口为 443、80、88 或 8443。
  • 有效的 TLS 证书(TLS 1.2+)。自签名证书需上传其公钥才能使用。
  • 服务器必须快速返回 HTTP 2xx,否则 Telegram 会重试,更新会不断积压。

在线设置 Webhook

  1. 打开 Telegram Bot Manager,粘贴从 @BotFather 获取的机器人 Token。
  2. 在 Webhook 选项卡中填写处理程序的 URL。
  3. 点击生成创建 Secret Token,并保存到服务器配置中。
  4. 如果只需要部分更新类型(或需要默认关闭的 chat_member / 表情回应),请选择 allowed_updates。
  5. 点击设置 Webhook,状态区域会立即显示结果。

参数

参数作用
url以 JSON POST 请求接收更新的 HTTPS 地址。
secret_token1–256 个字符 A-Z a-z 0-9 _ -,在每个请求的 X-Telegram-Bot-Api-Secret-Token 请求头中发送。
allowed_updates要接收的更新类型。空列表 = 除 chat_member、message_reaction、message_reaction_count 外的全部类型。
max_connections同时 HTTPS 连接数,1–100(默认 40)。
ip_address使用固定 IP 代替 DNS 解析。
certificate自签名证书的公钥。
drop_pending_updates丢弃队列中等待的更新。

使用 curl

curl -X POST "https://api.telegram.org/bot$BOT_TOKEN/setWebhook" \
  --form-string "url=https://example.com/telegram/webhook" \
  --form-string "secret_token=$WEBHOOK_SECRET" \
  --form-string 'allowed_updates=["message","callback_query"]' \
  --form-string "max_connections=40"

使用自签名证书:

curl -X POST "https://api.telegram.org/bot$BOT_TOKEN/setWebhook" \
  --form-string "url=https://203.0.113.10:8443/webhook" \
  -F "[email protected]"

在服务器上校验 Secret Token

务必将请求头与你的密钥比对并拒绝其他请求——仅靠 Webhook URL 并不安全。

// Node.js (Express): reject requests without the right secret
app.post('/telegram/webhook', (req, res) => {
  if (req.get('X-Telegram-Bot-Api-Secret-Token') !== process.env.WEBHOOK_SECRET) {
    return res.sendStatus(401);
  }
  // handle req.body (Update) ...
  res.sendStatus(200);
});

常见错误

  • Bad Request: bad webhook: An HTTPS URL must be provided for webhook——请使用 https://。
  • Bad Request: bad webhook: Webhook can be set up only on ports 80, 88, 443 or 8443——更换端口,或在前面放置反向代理。
  • Bad Request: bad webhook: Failed to resolve host——域名没有公网 DNS 记录。
  • Bad Request: secret token contains unallowed characters——只能使用字母、数字、_ 和 -。

几次点击即可完成——无需写代码、无需服务器,直接在浏览器中操作。

打开 Webhook 管理器

更多教程

检查 Webhook(getWebhookInfo)在线检查 Telegram 机器人 Webhook:URL、待处理更新、最近错误及其含义。修复 SSL、DNS、超时和 4xx/5xx Webhook 错误。 删除 Webhook在线删除 Telegram 机器人 Webhook(deleteWebhook),丢弃待处理更新,解决“409 Conflict: can't use getUpdates method while webhook is active”。 机器人命令编辑器在线编辑 Telegram 机器人命令:按范围和语言设置 setMyCommands、deleteMyCommands、限制与格式。免费,无需写代码。
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.