简体   繁体   English

Telegraf 和 Heroku 没有在 webhook [nodejs] 上发送第一个回复

[英]Telegraf and Heroku not sending the first reply on webhook [nodejs]

I am tring to write a simple bot that after some task reply with a text.我想写一个简单的机器人,在一些任务后用文本回复。

The bot offline with app.polling is working perfectly.带有 app.polling 的离线机器人运行良好。 But when I deploy it on heroku, if I write only one ctx.reply(), no message is sent.但是当我在heroku上部署它时,如果我只写一个ctx.reply(),则不会发送任何消息。 If I write it twice, one message is sent on telegram.如果我写两次,就会通过电报发送一条消息。 You can see the code on the snippet below, I included only the necessary code (the log show me with console.log that all the function are working and the final text is ready to be sent, I also commented the code a little to exlpain better the situation).你可以在下面的代码片段中看到代码,我只包含了必要的代码(日志显示我有 console.log 所有功能都在工作并且最终文本已准备好发送,我还对代码进行了一些注释以解释更好的情况)。

So this appear strange to me,can anyone explain why?所以这对我来说很奇怪,谁能解释一下为什么?

 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); });

NEWS消息

I want to add some feedback while the server works, so I add a ctx.reply("searching...") after the command right before the function getdata is launched.我想在服务器工作时添加一些反馈,所以我在函数 getdata 启动之前在命令之后添加了一个 ctx.reply("searching...") 。 Now all two messages are sent to telegram.现在所有两条消息都发送到电报。 On the previous case is possible that heroku "shut down the webhook" and at the first ctx.reply was wake up and than at the second ctx.reply the message was sent?在前一种情况下,heroku 有可能“关闭 webhook”并且在第一个 ctx.reply 被唤醒,而不是在第二个 ctx.reply 消息被发送?

I'm using node-telegram-bot-api for node.js/Heroku and this config work well for me:我正在为 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}`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM