【发布时间】: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