Node.js Telegram Bot TypeError & Отправляет сообщение без команды - PullRequest
0 голосов
/ 20 февраля 2020

Я создал бот-телеграмму, используя Node.js, который работает локально, но выходит из строя и падает после развертывания Heroku. Журналы cra sh от Heroku:

    2020-02-20T18:47:14.025987+00:00 heroku[web.1]: Starting process with command `npm start`
2020-02-20T18:47:16.231673+00:00 app[web.1]: 
2020-02-20T18:47:16.231821+00:00 app[web.1]: > sguni_bot@1.0.0 start /app
2020-02-20T18:47:16.231823+00:00 app[web.1]: > node index.js
2020-02-20T18:47:16.231824+00:00 app[web.1]: 
2020-02-20T18:47:16.662775+00:00 app[web.1]: 
2020-02-20T18:47:16.663220+00:00 app[web.1]: TypeError: Cannot read property 'includes' of undefined
2020-02-20T18:47:16.663220+00:00 app[web.1]: at /app/index.js:87:10
2020-02-20T18:47:16.663221+00:00 app[web.1]: at /app/node_modules/telegraf/composer.js:143:56
2020-02-20T18:47:16.663221+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.663221+00:00 app[web.1]: at async Promise.all (index 0)
2020-02-20T18:47:16.663275+00:00 app[web.1]: 
2020-02-20T18:47:16.663326+00:00 app[web.1]: 
2020-02-20T18:47:16.663470+00:00 app[web.1]: TypeError: Cannot read property 'includes' of undefined
2020-02-20T18:47:16.663471+00:00 app[web.1]: at /app/index.js:87:10
2020-02-20T18:47:16.663472+00:00 app[web.1]: at /app/node_modules/telegraf/composer.js:143:56
2020-02-20T18:47:16.663472+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.663473+00:00 app[web.1]: at async Promise.all (index 5)
2020-02-20T18:47:16.663600+00:00 app[web.1]: 
2020-02-20T18:47:16.666012+00:00 app[web.1]: Failed to process updates. TypeError: Cannot read property 'includes' of undefined
2020-02-20T18:47:16.666013+00:00 app[web.1]: at /app/index.js:87:10
2020-02-20T18:47:16.666014+00:00 app[web.1]: at /app/node_modules/telegraf/composer.js:143:56
2020-02-20T18:47:16.666014+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.666014+00:00 app[web.1]: at async Promise.all (index 0)
2020-02-20T18:47:16.770602+00:00 app[web.1]: 
2020-02-20T18:47:16.773283+00:00 app[web.1]: Error: 403: Forbidden: bot was kicked from the supergroup chat
2020-02-20T18:47:16.773284+00:00 app[web.1]: at /app/node_modules/telegraf/core/network/client.js:281:17
2020-02-20T18:47:16.773284+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.773285+00:00 app[web.1]: at async Promise.all (index 1)
2020-02-20T18:47:16.773307+00:00 app[web.1]: 
2020-02-20T18:47:16.877279+00:00 app[web.1]: 
2020-02-20T18:47:16.877505+00:00 app[web.1]: TypeError: Cannot read property 'includes' of undefined
2020-02-20T18:47:16.877507+00:00 app[web.1]: at /app/index.js:87:10
2020-02-20T18:47:16.877507+00:00 app[web.1]: at /app/node_modules/telegraf/composer.js:143:56
2020-02-20T18:47:16.877508+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.877508+00:00 app[web.1]: at async Promise.all (index 0)
2020-02-20T18:47:16.877594+00:00 app[web.1]: 
2020-02-20T18:47:16.877678+00:00 app[web.1]: 
2020-02-20T18:47:16.877851+00:00 app[web.1]: TypeError: Cannot read property 'includes' of undefined
2020-02-20T18:47:16.877852+00:00 app[web.1]: at /app/index.js:87:10
2020-02-20T18:47:16.877852+00:00 app[web.1]: at /app/node_modules/telegraf/composer.js:143:56
2020-02-20T18:47:16.877853+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.877853+00:00 app[web.1]: at async Promise.all (index 5)
2020-02-20T18:47:16.877899+00:00 app[web.1]: 
2020-02-20T18:47:16.878051+00:00 app[web.1]: Failed to process updates. TypeError: Cannot read property 'includes' of undefined
2020-02-20T18:47:16.878052+00:00 app[web.1]: at /app/index.js:87:10
2020-02-20T18:47:16.878053+00:00 app[web.1]: at /app/node_modules/telegraf/composer.js:143:56
2020-02-20T18:47:16.878053+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.878054+00:00 app[web.1]: at async Promise.all (index 0)
2020-02-20T18:47:16.965803+00:00 app[web.1]: 
2020-02-20T18:47:16.965958+00:00 app[web.1]: Error: 403: Forbidden: bot was kicked from the supergroup chat
2020-02-20T18:47:16.965959+00:00 app[web.1]: at /app/node_modules/telegraf/core/network/client.js:281:17
2020-02-20T18:47:16.965960+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.965961+00:00 app[web.1]: at async Promise.all (index 1)
2020-02-20T18:47:16.966015+00:00 app[web.1]: 
2020-02-20T18:47:16.974104+00:00 app[web.1]: 
2020-02-20T18:47:16.974394+00:00 app[web.1]: Error: 403: Forbidden: bot was kicked from the supergroup chat
2020-02-20T18:47:16.974395+00:00 app[web.1]: at /app/node_modules/telegraf/core/network/client.js:281:17
2020-02-20T18:47:16.974396+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.974398+00:00 app[web.1]: at async TelegrafInlineMenu.setMenuNow (/app/node_modules/telegraf-inline-menu/dist/source/index.js:267:17)
2020-02-20T18:47:16.974399+00:00 app[web.1]: at async Promise.all (index 3)
2020-02-20T18:47:16.974478+00:00 app[web.1]: 
2020-02-20T18:47:16.976146+00:00 app[web.1]: 
2020-02-20T18:47:16.976355+00:00 app[web.1]: Error: 403: Forbidden: bot was kicked from the supergroup chat
2020-02-20T18:47:16.976356+00:00 app[web.1]: at /app/node_modules/telegraf/core/network/client.js:281:17
2020-02-20T18:47:16.976357+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:16.976357+00:00 app[web.1]: at async TelegrafInlineMenu.setMenuNow (/app/node_modules/telegraf-inline-menu/dist/source/index.js:267:17)
2020-02-20T18:47:16.976358+00:00 app[web.1]: at async Promise.all (index 2)
2020-02-20T18:47:16.976432+00:00 app[web.1]: 
2020-02-20T18:47:17.162151+00:00 app[web.1]: 
2020-02-20T18:47:17.162360+00:00 app[web.1]: Error: 403: Forbidden: bot was kicked from the supergroup chat
2020-02-20T18:47:17.162361+00:00 app[web.1]: at /app/node_modules/telegraf/core/network/client.js:281:17
2020-02-20T18:47:17.162361+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:17.162362+00:00 app[web.1]: at async TelegrafInlineMenu.setMenuNow (/app/node_modules/telegraf-inline-menu/dist/source/index.js:267:17)
2020-02-20T18:47:17.162362+00:00 app[web.1]: at async Promise.all (index 3)
2020-02-20T18:47:17.162425+00:00 app[web.1]: 
2020-02-20T18:47:17.164315+00:00 app[web.1]: 
2020-02-20T18:47:17.164627+00:00 app[web.1]: Error: 403: Forbidden: bot was kicked from the supergroup chat
2020-02-20T18:47:17.164628+00:00 app[web.1]: at /app/node_modules/telegraf/core/network/client.js:281:17
2020-02-20T18:47:17.164628+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-02-20T18:47:17.164628+00:00 app[web.1]: at async TelegrafInlineMenu.setMenuNow (/app/node_modules/telegraf-inline-menu/dist/source/index.js:267:17)
2020-02-20T18:47:17.164629+00:00 app[web.1]: at async Promise.all (index 2)
2020-02-20T18:47:17.164629+00:00 app[web.1]: 
2020-02-20T18:47:17.426723+00:00 heroku[web.1]: State changed from starting to crashed
2020-02-20T18:47:17.431375+00:00 heroku[web.1]: State changed from crashed to starting
2020-02-20T18:47:17.411394+00:00 heroku[web.1]: Process exited with status 0
2020-02-20T18:47:18.836158+00:00 heroku[web.1]: State changed from crashed to starting
2020-02-20T18:47:19.712921+00:00 heroku[web.1]: Starting process with command `npm start`
(base) Jaydens-MacBook-Pro:sguni_bot Jayden$ heroku logs -n 100
2020-02-20T18:47:17.162361+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5)

Весь мой индекс. js, как показано ниже:

const Telegraf = require('telegraf')
const TelegrafInlineMenu = require('telegraf-inline-menu')
const menu = new TelegrafInlineMenu(ctx => `Hey ${ctx.from.first_name}, welcome to NUS! What would you like to find out?`)
const menu2 = new TelegrafInlineMenu(ctx => `Hey ${ctx.from.first_name}, welcome to NTU! What would you like to find out?`)
const menu3 = new TelegrafInlineMenu(ctx => `Hey ${ctx.from.first_name}, welcome to SMU! What would you like to find out?`)
const menu4 = new TelegrafInlineMenu(ctx => `Hey ${ctx.from.first_name}, please choose from the following FAQs!`)

const bot = new Telegraf(process.env.BOT_TOKEN)
bot.hears('\/discuss', (ctx) => ctx.reply('xxx'));
bot.hears('\/social', (ctx) => ctx.reply("xxx"));
bot.hears('\/start', (ctx) => ctx.reply("xxx"));
bot.hears('\/help', (ctx) => ctx.reply("xxx"));
bot.launch()

//NUS
menu.setCommand('NUS')
menu.simpleButton('IGP', 'a', {
  doFunc: ctx => ctx.replyWithDocument('xxx')
})
menu.simpleButton('Ask A NUS Student!', 'b', {
  doFunc: ctx => ctx.reply('xxx')
})
menu.simpleButton('Where do I apply?!', 'c', {
  doFunc: ctx => ctx.reply('xxx')
})

//NTU
menu2.setCommand('NTU')
menu2.simpleButton('IGP', 'd', {
  doFunc: ctx => ctx.replyWithDocument('xxx')
})
menu2.simpleButton('Ask A NTU Student!', 'e', {
  doFunc: ctx => ctx.reply('xxx')
})
menu2.simpleButton('Where do I apply?!', 'f', {
  doFunc: ctx => ctx.reply('xxx')
})

//SMU
menu3.setCommand('SMU')
menu3.simpleButton('IGP', 'g', {
  doFunc: ctx => ctx.reply('xxx')
})
menu3.simpleButton('Ask A SMU Student!', 'h', {
  doFunc: ctx => ctx.reply('xxx')
})
menu3.simpleButton('Where do I apply?!', 'i', {
  doFunc: ctx => ctx.reply('xxx')
})

//FAQ
menu4.setCommand('FAQ')
menu4.simpleButton('Choosing a course', 'j', {
  doFunc: ctx => ctx.reply('xxx')
})
menu4.simpleButton('University courses in a nutshell!', 'k', {
  doFunc: ctx => ctx.reply('xxx')
})
menu4.simpleButton('Double Degrees?!', 'l', {
  doFunc: ctx => ctx.reply('xxx')
})

bot.launch()
bot.use(menu.init({
  backButtonText: 'back…',
  mainMenuButtonText: 'back to main menu…'
}))
bot.use(menu2.init({
  backButtonText: 'back…',
  mainMenuButtonText: 'back to main menu…'
}))
bot.use(menu3.init({
  backButtonText: 'back…',
  mainMenuButtonText: 'back to main menu…'
}))
bot.use(menu4.init({
  backButtonText: 'back…',
  mainMenuButtonText: 'back to main menu…'
}))
bot.startPolling()

// general greetings
bot.on('message', context => {
const { reply, update } = context
const { message } = update
const { text } = message
if (text.includes('hi')) reply('Hello there! You may begin by using either /NUS, /NTU or /SMU! Alternatively, you can visit /FAQ or /social for more information on the upcoming university admissions!');
if (text.includes('hello')) reply('Hello there! You may begin by using either /NUS, /NTU or /SMU! Alternatively, you can visit /FAQ or /social for more information on the upcoming university admissions!');
if (text.includes('hey')) reply('Hello there! You may begin by using either /NUS, /NTU or /SMU! Alternatively, you can visit /FAQ or /social for more information on the upcoming university admissions!');
// easter egg greeting
if (text.includes('howdy')) reply('Howdy Partna!')
if (text.includes('sup')) reply('sup bud')
if (text.includes('suh')) reply('suh bruh')
})

Я не уверен, с чего начать с устранения неполадок, поскольку сборку удалось развернуть на Heroku без проблем. Когда приложение перезапускается, бот-телеграмма спамит тексты до тех пор, пока он не вылетает, например: спам-чат бота-телеграммы

Было бы замечательно получить несколько указателей на причины огромного количества ошибок. в результате чего бот падает. Спасибо!

1 Ответ

0 голосов
/ 20 февраля 2020

Здесь выдается ваша ошибка:

const { text } = message
if (text.includes('hi')) 

Ваша программа предполагает, что message.text существует и является строкой, и выдает ее, если ее нет.

В Heroku этот обработчик вызывается с сообщением, которое не соответствует этому. message существует, но не имеет свойства text.

Почему это другая проблема.

Может быть, ваши секреты / переменные env не загружены? Может быть, сетевые порты, которые ему нужны, не отображаются?

Я бы начал с верхней части приложения и выходил из системы на каждом этапе, чтобы проверить, где происходит разбивка. Если вы начинаете снизу, вы должны console.log(message) начать с.

...