【问题标题】:How to use telegraf webhook?如何使用电报网络钩子?
【发布时间】:2021-01-13 11:53:51
【问题描述】:

我想在 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();

【问题讨论】:

    标签: node.js telegraf.js


    【解决方案1】:

    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 启动机器人,并使用长轮询(默认)或 webhook 的相应配置。

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

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

    请参阅文档中的this example 以供参考。

    【讨论】:

      【解决方案2】:

      这对我有用:

        bot.startWebhook('/messages', null, 8443);
        bot.launch();
      

      第二个参数是tlsOptions,它是可选的。
      我读过 Telegram 只接受 80、88、443 和 8443 上的 webhook。
      不确定是否属实,但考虑到这一点很重要,因为很难对 webhook 进行故障排除。

      【讨论】:

        猜你喜欢
        • 2015-03-11
        • 1970-01-01
        • 2019-01-02
        • 2011-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-14
        相关资源
        最近更新 更多