node-telegram-bot-api icon indicating copy to clipboard operation
node-telegram-bot-api copied to clipboard

using setWebHook the program just ends in a second

Open rxmoein opened this issue 6 years ago • 13 comments

I am trying to set webhook and I need some help. I am using the code like this:

bot.setWebHook('VPS_IP', {
        certificate: 'public.pem',
    });

but when I run the bot is just exiting cleanly without any error or warning. what am I doing wrong?

rxmoein avatar Sep 19 '18 14:09 rxmoein

Try to use this code to debug:

process.on('uncaughtException', function (error) {
    console.log("\x1b[31muncaughtException: " + error + "\x1b[0m");
});
process.on('unhandledRejection', function (error, p) {
    console.log("\x1b[31munhandledRejection: " + error.message + "\x1b[0m");
});

sidelux avatar Sep 19 '18 15:09 sidelux

Nothing happened! I also logged setWebHook function result and it is true. should I‌ use a specific configuration for bot??

rxmoein avatar Sep 19 '18 15:09 rxmoein

Then you should setup an express part to respond with HTTP 200 to webhook:

var express = require('express');
var bodyParser = require('body-parser');

var bot = new TelegramBot(token);
var app = express();
var path = "yourpath" + token;
var port = 25001;

bot.setWebHook('yourserver' + path);

app.listen(port);
app.use(bodyParser.json());
app.post(path, function (req, res) {
    bot.processUpdate(req.body);
    res.sendStatus(200);
});

sidelux avatar Sep 19 '18 15:09 sidelux

Just checking, did you set up a web server (express, etc) which will act as the webhook endpoint?

DtCarrot avatar Sep 19 '18 15:09 DtCarrot

It's a more complicated system for me, i made a nginx server that redirect all port to the same for all bots (25001, 25002, etc.) and it setup certificates.

sidelux avatar Sep 19 '18 15:09 sidelux

@sidelux Anyway, was replying to @mrdimaan in case you were responding to my message. Our replies were seconds apart haha. I have similar configuration as well btw.

DtCarrot avatar Sep 19 '18 15:09 DtCarrot

witch port should the express server listen for?

rxmoein avatar Sep 19 '18 15:09 rxmoein

@mrdimaan Most port numbers are good. Port 25000, 25001 or 25002 as what @sidelux mentioned are fine.

DtCarrot avatar Sep 19 '18 15:09 DtCarrot

so does telegram send the updates to all these ports?!

rxmoein avatar Sep 19 '18 15:09 rxmoein

It will only send to the url that you defined in bot.setWebHook(serverURL).

So for example if your url is serverURL = 'localhost:25000/bot', then it will only send to that url at the given port.

DtCarrot avatar Sep 19 '18 15:09 DtCarrot

this is the URL given to setWebHook function: https://x.x.x.x:25000 but got an error message that webhook can be set on other ports like 443. then I tried 443 port for URL and made the server to listen on port 443. I don't have any error but I don't get the updates yet.

rxmoein avatar Sep 19 '18 16:09 rxmoein

Ok. now I think the problem is with the certificate. how should I configure it? I made the request manually by postman and it works fine by HTTP and not by https.

rxmoein avatar Sep 19 '18 16:09 rxmoein

Have you gotten a valid SSL certificate? If not, you can use Let's Encrypt to generate one for free.

DtCarrot avatar Sep 20 '18 00:09 DtCarrot