【问题标题】:Heroku deploy chat botHeroku 部署聊天机器人
【发布时间】:2022-01-07 18:44:37
【问题描述】:

所以,我的代码在本地和 docker 映像都可以工作,但是,当我部署到 heroku 时,它似乎在前 1 分钟工作,然后应用程序崩溃,在崩溃后有 heroku logs,那么可能是什么问题?任何想法?谢谢

这是我的代码

const { default: axios } = require('axios')
const telegramBot = require('node-telegram-bot-api')
const express = require('express')
const dotenv = require('dotenv').config()

const links = `
<a href="https://git.foxminded.com.ua/foxstudent100709">GitLab</a>
<a href="https://www.linkedin.com/in/elguja-mtchedlidze-39a746225/">Linkdin</a>
<a href="https://mtchedlidze-87ef7.web.app/">Personal</a>
  `
const bot = new telegramBot(process.env.TOKEN, { polling: true })
const aboutText = 'Hello, I am learning NODE JS!'
const app = express()
bot.on('message', (message) => {
  const id = message.chat.id

  if (message.text === '/start' || message.text === '/help') {
    bot.sendMessage(message.chat.id, 'avalable commands', {
      reply_markup: {
        keyboard: [['/about', '/links']],
        resize_keyboard: true,
        one_time_keyboard: true,
        force_reply: true,
      },
    })
  } else if (message.text === '/about') {
    bot.sendMessage(id, aboutText)
  } else if (message.text === '/links') {
    bot.sendMessage(id, links, { parse_mode: 'HTML' })
  } else {
    bot.sendMessage(
      message.chat.id,
      'no such command! there are avalable commands',
      {
        reply_markup: {
          keyboard: [['/about', '/links']],
          resize_keyboard: true,
          one_time_keyboard: true,
          force_reply: true,
        },
      }
    )
  }
})

码头文件:

FROM node:16-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install --production

COPY ./app.js .

ENV port=8080
ENV TOKEN=21*****:AAEF******************fKiQ
EXPOSE 8080

CMD [ "node", "app.js" ]

【问题讨论】:

    标签: heroku bots


    【解决方案1】:

    您的应用无法绑定到 Heroku 分配的端口:您不能自己设置端口(即 8080),而是绑定到 $PORT 环境变量定义的端口。

    查看Why is my Node.js app crashing with an R10 error?了解详情。

    在您的特定情况下,如果您在 polling 模式下运行 Bot(即拉取更改),您可以改用 worker 节点而不是 web:这样做您不需要绑定到端口,因为您的应用程序不需要处理传入的 HTTP 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      相关资源
      最近更新 更多