Home > Enterprise >  Telegram bot doesn't run on heroku
Telegram bot doesn't run on heroku

Time:02-14

Problem

My problem is that when trying to deploy bot on heroku I get r10 error boot timeout, but it works when running locally and I cant seem to find the fix for it

heroku logs

Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch
Stopping process with SIGKILL
Process exited with status 137
State changed from starting to crashed

My code

process.env.NTBA_FIX_319 = 1
const TelegramBot = require('node-telegram-bot-api')
require('dotenv').config()

const token = process.env.TOKEN
const myInfo = process.env.INFO
const error = process.env.ERROR
const git = process.env.GIT
const bot = new TelegramBot(token, { polling: true })

bot.on('message', (msg) => {
  const chatId = msg.chat.id
  const name = msg.chat.first_name
  const { text } = msg

  if (text === '/start' || text === '/help') {
    bot.sendMessage(chatId, `Hi ${name}! These are the commands below:`, {
      reply_markup: {
        keyboard: [
          [
            { text: '/start' },
            { text: '/about' },
            { text: '/links' },
            { text: '/help' },
          ],
        ],
        resize_keyboard: true,
        one_time_keyboard: true,
      },
    })
  } else if (text === '/about') {
    bot.sendMessage(chatId, `${myInfo}`)
  } else if (text === '/links') {
    bot.sendMessage(chatId, `${git}`)
  } else {
    bot.sendMessage(chatId, `${error}`)
  }
})

Dockerfile

FROM node:16.13.2-alpine
WORKDIR /Bot1
ENV PORT 88
COPY package.json /Bot1/package.json
RUN npm install
COPY . .
CMD ["node", "app.js"]

comands to deploy bot

  • heroku container:push web
  • heroku container:release web

CodePudding user response:

You have deployed your code as a web process. web processes listen for HTTP requests and must bind to a port provided at runtime shortly after starting up.

Since your bot does not respond to HTTP requests it should not be deployed as a web process. A common name for such processes is worker.

First, remove the web container you have already deployed:

heroku container:rm web

Now, redeploy your code as a worker process:

heroku container:push worker
heroku container:release worker

You may need to scale your dynos after doing this. Something like

heroku ps:scale worker=1

should do the trick.

  • Related