Telegram 机器人在 heroku 上没有 运行

Telegram bot doesn't run on heroku

问题

我的问题是,当尝试在 heroku 上部署 bot 时,出现 r10 错误启动超时,但它在本地 运行 时有效,我似乎找不到修复它的方法

heroku 日志

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

我的代码

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}`)
  }
})

Docker 文件

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"]

部署机器人的命令

您已将代码部署为 a web processweb 进程侦听 HTTP 请求,并且必须在启动后不久绑定到运行时提供的端口。

由于您的机器人不响应 HTTP 请求,因此不应将其部署为 web 进程。此类进程的通用名称是 worker.

首先,remove您已经部署的 web 容器:

heroku container:rm web

现在,将您的代码重新部署为 worker 进程:

heroku container:push worker
heroku container:release worker

完成此操作后您可能需要 scale your dynos。像

heroku ps:scale worker=1

应该可以解决问题。