如何使用电报网络钩子?

我想在 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将开始侦听提供的 webhook url,因此您无需在此bot.launch()之后调用。

bot.launch() 将被默认启动轮询模式机器人如果没有选项被指定为你的情况。

删除bot.launch(),机器人应该以 webhook 模式启动。

Telegraf.js ^4.0.0

如果您使用的是 Telegraf.js 4.0 或更高版本,则更改日志指出:

现在应该始终使用 bot.launch 和长轮询(默认)或 webhooks 的相应配置来启动机器人。

因此,您也可以尝试删除bot.telegram.setWebhook()and bot.startWebhook(),改为添加以下代码:

bot.launch({
  webhook: {
    domain: 'https://myaddress.com',
    port: 4000
  }
})

请参阅文档中的此示例以供参考。


以上是如何使用电报网络钩子?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>