1
我想用nodejs使用官方报文bot api制作一个电报机器人(用于学习目的)。我为heroku设置了一个webhook。 我可以回复请求,但经过一段时间后,同样的请求会在一段时间后再次出现。得到相同的请求是否正常?或者我没有回应即将到来的请求。当我调用getwebhookinfo方法时,它显示了pending_update_count,但我的代码确实响应了来自webhook的所有请求。 我用这个回复的到来请求如何应对电报bot Webhook请求?相同的请求正在反复
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var config = require('./lib/config');
var request = require('request');
var port = process.env.PORT || 3000;
var reply_url = "https://api.telegram.org/bot"+config.bot_token;
app.use(bodyParser.json());
app.get('/',function(req,res) {
res.send("Working");
request({
url: "https://api.telegram.org/bot"+config.bot_token+'/getMe',
json : true
}, (err,res,body)=>{
console.log(body);
});
});
app.post('/'+config.bot_token , (req,res)=>{
var body = req.body;
console.log(body);
console.log(body.message.entities);
request.post((reply_url+'/sendMessage'),{form:{chat_id:body.message.chat.id,text:"POST REPLY SUCCESS",reply_to_message_id:body.message.message_id}});
});
app.listen(port,() =>
{
console.log("Server is Started at - "+port);
});
试加在执行res.send('Working')后,在API函数(req,res,next)的回调函数中调用next并调用next()函数。类似的适用于其他POST API('/'+config.bot_token)。在/ sendMessage API的成功和错误回调中调用res.status()。send()然后next(); –
我得到了Typeerror,你可以为我重写代码。 – MaxySpark
好的我得到了res.status(200).send('OK') – MaxySpark