如何解决如何使用电报网络钩子?
我想在 Telegraf 中使用 webHook,但我不知道如何正确使用它。
这是我的简单代码。 但它仍然使用轮询。
const Telegraf = require('telegraf');
const bot = new Telegraf('123:ABC');
bot.telegram.setWebhook('https://myaddress.com');
bot.startWebhook(`/`,null,4000);
bot.use(function(ctx,next){
try{
if(ctx.chat == undefined) return;
console.log("Hello World");
}catch (e){
console.log("Error");
}
});
bot.launch();
解决方法
当 bot.startWebhook()
被称为 Telegraf will start listening to the provided webhook url 时,因此您不需要在此之后调用 bot.launch()
。
如果没有在您的情况下指定选项,则还 bot.launch()
will start the bot in polling mode by default。
删除 bot.launch()
,机器人应以 webhook 模式启动。
Telegraf.js ^4.0.0
如果您使用的是 Telegraf.js 4.0 或更高版本,changelog 声明:
机器人现在应该总是使用 bot.launch 和相应的长轮询(默认)或 webhooks 配置启动。
因此您也可以尝试删除 bot.telegram.setWebhook()
和 bot.startWebhook()
,改为添加以下代码:
bot.launch({
webhook: {
domain: 'https://myaddress.com',port: 4000
}
})
请参阅文档中的 this example 以供参考。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。