Trouble receiving Telegram bot data through Node.js websocket

Hey everyone, I’m stuck trying to get my Telegram bot working with a Node.js HTTPS server. I’ve set up a self-signed SSL cert and created a simple server, but I’m hitting a wall.

My server is online and accessible, and Telegram says the webhook is set up correctly. I can see connections from Telegram’s servers in my firewall logs, but my Node.js app isn’t getting any requests.

Here’s a simplified version of my server code:

const https = require('https');
const fs = require('fs');

const serverConfig = {
  key: fs.readFileSync('./private-key.pem'),
  cert: fs.readFileSync('./public-cert.pem')
};

https.createServer(serverConfig, (req, res) => {
  console.log('Incoming request:', req.url);
  res.end('Server is running');
}).listen(9876, '0.0.0.0');

I’ve allowed incoming connections on the server port in my firewall. Any ideas what could be going wrong? I’m using Windows 8.1 and Node.js v5.9.1.

Thanks in advance for any help!

hve u checked if ur ssl cert is valid? self-signed certs can be tricky. maybe try using lets encrypt for a free valid cert. also, make sure ur webhook URL matches exactly what u set in telegram. double-check ur firewall rules too, sometimes thats the culprit

Have you considered using a more recent version of Node.js? Version 5.9.1 is quite outdated and might be causing compatibility issues. Upgrading to a Long Term Support (LTS) version like 14.x or 16.x could potentially resolve your problem.

Additionally, ensure your webhook URL is correctly set in the Telegram Bot API. It should be something like ‘https://your-domain.com:9876/your-bot-token’. The path is crucial for security.

Lastly, try adding some error handling to your server code. This might give you more insight into what’s going wrong:

https.createServer(serverConfig, (req, res) => {
  console.log('Incoming request:', req.url);
  res.end('Server is running');
}).listen(9876, '0.0.0.0', () => {
  console.log('Server running on port 9876');
}).on('error', (e) => {
  console.error('Server error:', e);
});

This will log any server errors, which could be invaluable for debugging.

I’ve dealt with similar issues before. One thing that’s not immediately obvious is that Telegram can be quite picky about SSL/TLS versions. Make sure your server is configured to use TLS 1.2 or higher. Also, check your Node.js version - v5.9.1 is quite old and might not support the latest TLS standards out of the box.

Another potential gotcha is the content type. Telegram sends updates as application/json, so you might need to explicitly parse the request body. Something like:

let body = '';
req.on('data', chunk => {
    body += chunk.toString();
});
req.on('end', () => {
    const update = JSON.parse(body);
    // Process the update
});

Lastly, ensure your server can handle POST requests. The code snippet you provided doesn’t differentiate between request methods. Hope this helps!