MaxySpark
MaxySpark

Reputation: 565

How To Response To telegram bot Webhook Request? Same Request Are Coming Repeatedly

I am trying to make a telegram bot(for learning purpose) with nodejs using official telegram bot api. I set a webhook to heroku. I am able to reply the request but after some time the same request come again after some time. Is it normal to get same request or I did not response to the coming request. when I call the getwebhookinfo method it shows pending_update_count but my code did response to all request coming from webhook. I use this to reply to the coming requests

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

Upvotes: 1

Views: 3290

Answers (1)

Phagun Baya
Phagun Baya

Reputation: 2227

try adding next in the callback function of the API function(req, res, next) and call next() function after you do res.status(201).send('Working").

Similar applies to other POST API ('/'+config.bot_token); in the success and error callback of /sendMessage API, call res.status().send() and then next();

Always call next() as a standard practice in working with express.js

Upvotes: 2

Related Questions