Telegraf机器人Heroku部署报409冲突错误的解决咨询
问题:Telegraf机器人部署Heroku后出现409冲突错误
将基于Telegraf开发的机器人部署在Heroku平台,运行一段时间后出现如下错误:TelegramError: 409: Conflict: terminated by setWebhook request,不清楚错误含义及修复方法,寻求帮助。
机器人代码
export class TelegrafBot { private bot: Telegraf<Scenes.SceneContext>; private isInitialized: boolean = false; constructor() { this.bot = new Telegraf(TELEGRAM_TOKEN); } initialize() { try { this.bot.telegram.setWebhook('URL'); this.bot.launch(); this.isInitialized = true; process.once('SIGINT', () => this.bot.stop('SIGINT')); process.once('SIGTERM', () => this.bot.stop('SIGTERM')); } catch (e) { console.error(e); } } handleUpdate(body: Update, response: ServerResponse<IncomingMessage>) { if (!this.isInitialized) return; try { this.bot.handleUpdate(body, response); } catch (e) { console.log(e); } } } export const telegrafBot = new TelegrafBot();
错误日志
2022-12-25T06:21:25.231966+00:00 app[web.1]: TelegramError: 409: Conflict: terminated by setWebhook request 2022-12-25T06:21:25.231969+00:00 app[web.1]: at Telegram.callApi (/app/node_modules/telegraf/lib/core/network/client.js:293:19) 2022-12-25T06:21:25.231969+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:95:5) 2022-12-25T06:21:25.231970+00:00 app[web.1]: at async Polling.[Symbol.asyncIterator] (/app/node_modules/telegraf/lib/core/network/polling.js:30:33) 2022-12-25T06:21:25.231970+00:00 app[web.1]: at async Polling.loop (/app/node_modules/telegraf/lib/core/network/polling.js:73:30) 2022-12-25T06:21:25.231971+00:00 app[web.1]: at async Telegraf.launch (/app/node_modules/telegraf/lib/telegraf.js:189:13) { 2022-12-25T06:21:25.231972+00:00 app[web.1]: response: { 2022-12-25T06:21:25.231972+00:00 app[web.1]: ok: false, 2022-12-25T06:21:25.231972+00:00 app[web.1]: error_code: 409, 2022-12-25T06:21:25.231972+00:00 app[web.1]: description: 'Conflict: terminated by setWebhook request' 2022-12-25T06:21:25.231973+00:00 app[web.1]: }, 2022-12-25T06:21:25.231973+00:00 app[web.1]: on: { 2022-12-25T06:21:25.231974+00:00 app[web.1]: method: 'getUpdates', 2022-12-25T06:21:25.231974+00:00 app[web.1]: payload: { timeout: 50, offset: 0, allowed_updates: [] } 2022-12-25T06:21:25.231975+00:00 app[web.1]: } 2022-12-25T06:21:25.231975+00:00 app[web.1]: }
错误原因分析
这个409冲突错误的核心是Telegram机器人同时启用了两种更新接收模式:Webhook和Polling(轮询)。
从错误日志可见,代码执行了getUpdates方法(Polling模式的核心操作),但之前已经调用setWebhook开启了Webhook模式。Telegram不允许同一机器人同时使用两种模式,冲突时就会抛出该错误。
另外Heroku环境中,应用可能因重启、dyno缩放等原因多次调用initialize方法,重复设置Webhook并启动Polling,会进一步加剧冲突。
修复方案
1. 明确使用Webhook模式启动
使用Webhook时,不需要调用bot.launch()(默认启动Polling),直接通过自定义的handleUpdate处理更新。修改initialize方法:
initialize() { try { this.bot.telegram.setWebhook('YOUR_HEROKU_WEBHOOK_URL'); // 移除bot.launch()调用 this.isInitialized = true; process.once('SIGINT', () => this.bot.stop('SIGINT')); process.once('SIGTERM', () => this.bot.stop('SIGTERM')); } catch (e) { console.error(e); } }
2. 避免重复初始化
在Heroku环境中,确保initialize只执行一次,通过状态校验实现:
initialize() { if (this.isInitialized) { console.log('Bot already initialized, skipping'); return; } // 后续初始化逻辑... }
3. 配置正确的Webhook URL
确保设置的URL是Heroku应用的公网地址,格式通常为https://<your-app-name>.herokuapp.com/<webhook-path>,且该路径能正确映射到handleUpdate方法处理请求。
4. 验证Webhook状态
可通过API检查当前Webhook状态,确认无残留Polling进程:
// 可选:检查Webhook信息 this.bot.telegram.getWebhookInfo().then(info => { console.log('Webhook info:', info); });
内容的提问来源于stack exchange,提问作者malcoauri
相关产品推荐
相关产品推荐

