What is causing this 404 error on my telegram bot

1.4k views Asked by At
const { Composer } = require('micro-bot');
const bot = new Composer;
const Telegram = require('telegraf/telegram')
const http = require('http');
const express = require('express');

const bodyParser = require('body-parser');


const telegram = new Telegram(process.env.BOT_TOKEN);

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));

app.post('/sms', (req, res) => {
  console.log('sms received')

  telegram.sendMessage(process.env.CHANNEL_ID, req.body.Body)


  res.send(req.body.Body);

});


http.createServer(app).listen(3000, () => {
  console.log(`Express server listening on port 3000`);
});



module.exports = bot;

I am getting this application log from heroku whenever i send any http request to the bot api:

at=info method=POST path="/sms" host=smsforwarder.herokuapp.com request_id=5dd30db1-aeb8-4f3f-8d56-0b0b576ef4e6 fwd="116.87.44.71" dyno=web.1 connect=0ms service=1ms status=404 bytes=101 protocol=https

When I try to use the env port

const port = process.env.PORT || 3000
http.createServer(app).listen(port, () => {
  console.log(`Express server listening on port ${port}`);
});

I get an error saying the port is already in use

2020-10-10T12:30:53.000000+00:00 app[api]: Build started by user [email protected]
2020-10-10T12:31:07.143293+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-10T12:31:06.978851+00:00 app[api]: Release v37 created by user [email protected]
2020-10-10T12:31:06.978851+00:00 app[api]: Deploy 7e8e22a9 by user [email protected]
2020-10-10T12:31:07.000000+00:00 app[api]: Build succeeded
2020-10-10T12:31:09.959000+00:00 heroku[web.1]: Starting process with command `micro-bot -p 4749`
2020-10-10T12:31:13.722882+00:00 heroku[web.1]: Process exited with status 1
2020-10-10T12:31:13.285259+00:00 app[web.1]: Express server listening on port 4749
2020-10-10T12:31:13.654218+00:00 app[web.1]: events.js:292
2020-10-10T12:31:13.654256+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2020-10-10T12:31:13.654257+00:00 app[web.1]:       ^
2020-10-10T12:31:13.654257+00:00 app[web.1]: 
2020-10-10T12:31:13.654258+00:00 app[web.1]: Error: listen EADDRINUSE: address already in use 0.0.0.0:4749
2020-10-10T12:31:13.654259+00:00 app[web.1]:     at Server.setupListenHandle [as _listen2] (net.js:1313:16)
2020-10-10T12:31:13.654259+00:00 app[web.1]:     at listenInCluster (net.js:1361:12)
2020-10-10T12:31:13.654260+00:00 app[web.1]:     at doListen (net.js:1498:7)
2020-10-10T12:31:13.654261+00:00 app[web.1]:     at processTicksAndRejections (internal/process/task_queues.js:85:21)
2020-10-10T12:31:13.654261+00:00 app[web.1]: Emitted 'error' event on Server instance at:
2020-10-10T12:31:13.654262+00:00 app[web.1]:     at emitErrorNT (net.js:1340:8)
2020-10-10T12:31:13.654262+00:00 app[web.1]:     at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-10-10T12:31:13.654263+00:00 app[web.1]:   code: 'EADDRINUSE',
2020-10-10T12:31:13.654263+00:00 app[web.1]:   errno: 'EADDRINUSE',
2020-10-10T12:31:13.654264+00:00 app[web.1]:   syscall: 'listen',
2020-10-10T12:31:13.654264+00:00 app[web.1]:   address: '0.0.0.0',
2020-10-10T12:31:13.654264+00:00 app[web.1]:   port: 4749
2020-10-10T12:31:13.654265+00:00 app[web.1]: }
2020-10-10T12:31:13.786291+00:00 heroku[web.1]: State changed from starting to crashed
1

There are 1 answers

0
mrchoc On BEST ANSWER

I found a solution to my problem. I had a procfile that had my micro bot listen on the env port, thats why when I made the webserver listen to the same port at the same time it threw an error saying that the port is already in use.