二者互斥。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 可见时)。
