【问题标题】:Telegraf and Heroku not sending the first reply on webhook [nodejs]Telegraf 和 Heroku 没有在 webhook [nodejs] 上发送第一个回复
【发布时间】:2019-01-11 06:30:37
【问题描述】:

我正在尝试编写一个简单的机器人,在完成一些任务后回复一条文本。

使用 app.polling 离线的机器人运行良好。但是当我在heroku上部署它时,如果我只写一个ctx.reply(),就不会发送任何消息。如果我写两遍,就会通过电报发送一条消息。你可以在下面的 sn-p 上看到代码,我只包含了必要的代码(日志用 console.log 显示所有功能都在工作并且最终文本已准备好发送,我还对代码进行了一点注释更好地解释情况)。

所以这对我来说很奇怪,谁能解释为什么?

const Telegraf = require('telegraf');
const request = require('request');
const date = require('date-and-time');
const API_TOKEN = process.env.API_TOKEN || ''; //the api token is into env var
const PORT = process.env.PORT || 3000;
const URL = process.env.URL || 'url of my heroku account';


const app = new Telegraf(API_TOKEN);

app.telegram.setWebhook(`${URL}/bot${API_TOKEN}`);
app.startWebhook(`/bot${API_TOKEN}`, null, PORT);

  function getdata(ctx,stazione1,stazione2){
    let tempo = gettempo();
    let linkget = '....';
    var options = {
      url: linkget,
      headers: {
        'Referer':'http://m.trenord.it/site-lite/index.html',
        'secret': '...'
      }
    }; 
    let linkget1 = '...';
    var options1 = {
      url: linkget1,
      headers: {
        'Referer':'...',
        'secret': '...'
      }
    }; 
    request(options, function(error, response, body){
        request(options1, async function(error1, response1, body1){
            let text = await gettext(body,stazione1,stazione2);//return text
            let text1 = await gettext(body1,stazione2,stazione1);//return text
            let final = await text+"\r\n\r\n"+text1;
            console.log(ctx);
            //here is the problem, if i write only one reply no message is sent on the bot, but if i wrtite it twice one message is sent. 
            ctx.replyWithMarkdown(final);
            ctx.replyWithMarkdown(final);            
        });

    });

}//getdata

 app.command('pl', function(ctx){
   getdata(ctx,stazione1,stazione2); 
 });

新闻

我想在服务器工作时添加一些反馈,所以我在启动函数 getdata 之前的命令之后添加了一个 ctx.reply("searching...")。现在所有两条消息都发送到电报。在前一种情况下,heroku 有可能“关闭 webhook”并在第一个 ctx.reply 被唤醒,然后在第二个 ctx.reply 发送消息?

【问题讨论】:

    标签: node.js heroku telegram webhooks telegraf


    【解决方案1】:

    我在 node.js/Heroku 中使用 node-telegram-bot-api,这个配置对我来说效果很好:

    const options = {
      webHook: {
        // Port to which you should bind is assigned to $PORT variable
        // See: https://devcenter.heroku.com/articles/dynos#local-environment-variables
        port: process.env.PORT,
        // you do NOT need to set up certificates since Heroku provides
        // the SSL certs already (https://<app-name>.herokuapp.com)
        // Also no need to pass IP because on Heroku you need to bind to 0.0.0.0
      },
    };
    // okk
    // Heroku routes from port :443 to $PORT
    // Add URL of your app to env variable or enable Dyno Metadata
    // to get this automatically
    // See: https://devcenter.heroku.com/articles/dyno-metadata
    const url = process.env.APP_URL || 'https://my-bot.herokuapp.com:443';
    const bot = new Tg(config.BOT_TOKEN, options);
    // This informs the Telegram servers of the new webhook.
    // Note: we do not need to pass in the cert, as it already provided
    bot.setWebHook(`${url}/bot${config.BOT_TOKEN}`);
    

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 2019-06-02
      • 2014-12-14
      相关资源
      最近更新 更多