二者互斥。Webhook 啟用時,getUpdates 回傳 409。開發用輪詢,生產用 Webhook。
總覽
只選一種投遞模式。輪詢是主動拉取,Webhook 是被動推送。setWebhook 與 deleteWebhook 是高危受控操作——勿對生產 Bot 隨意執行。
getUpdates 參數
offset——上一批最大update_id + 1,避免重複消費。limit——1 到 100,預設 100。timeout——0 到 50 秒長輪詢。allowed_updates——型別過濾,如["message"]。
成功回應會把該批標記為已消費。未投遞的待處理 updates 最多保留 24 小時。
curl -s -X POST "https://call.mp.net/bot/bot${MPCHAT_BOT_TOKEN}/getUpdates" \
-H "Content-Type: application/json" \
-d '{"offset":0,"timeout":30,"limit":50,"allowed_updates":["message"]}'
配置 Webhook
curl -s -X POST "https://call.mp.net/bot/bot${MPCHAT_BOT_TOKEN}/setWebhook" \
-H "Content-Type: application/json" \
-d '{"url":"https://example.com/mp-bot/webhook","secret_token":"YOUR_WEBHOOK_SECRET"}'
url——必填,必須 HTTPS。空字串等同deleteWebhook。secret_token——可選;設定後請求帶X-MpChat-Bot-Api-Secret-Token標頭。max_connections、allowed_updates——可選調優與型別過濾。
儲存前伺服端會發一個 HTTPS 連通性探測;你的 endpoint 必須回傳任意 2xx(即使是空 body)。
校驗 secret_token(伺服端虛擬碼)
const incoming = req.headers["x-mpchat-bot-api-secret-token"];
if (incoming !== process.env.WEBHOOK_SECRET) {
return res.status(401).end();
}
如何選型
本地開發:getUpdates;用
getWebhookInfo確認未配置 Webhook。生產:Webhook +
secret_token校驗。切換:先在測試環境驗證,再對目標 Bot 執行
setWebhook。
相關文章
入群事件為 message.new_chat_members,退群為 message.left_chat_member,置頂為 message.pinned_message(當 Bot 可見時)。
