node-telegram-bot-api
node-telegram-bot-api copied to clipboard
using setWebHook the program just ends in a second
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?
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");
});
Nothing happened! I also logged setWebHook function result and it is true. should I use a specific configuration for bot??
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);
});
Just checking, did you set up a web server (express, etc) which will act as the webhook endpoint?
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 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.
witch port should the express server listen for?
@mrdimaan Most port numbers are good. Port 25000, 25001 or 25002 as what @sidelux mentioned are fine.
so does telegram send the updates to all these ports?!
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.
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.
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.
Have you gotten a valid SSL certificate? If not, you can use Let's Encrypt to generate one for free.