最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Telegraf bot stopped receiving messages, no errors - Stack Overflow

programmeradmin7浏览0评论

I have a bot that works like a tool for "accepting requests", and I have a web-interface for answering that requests. I noticed multiple time recently (at November 13th, and at December 2nd) that that bot just stopped receiving that requests. That never happened before, that bot worked without any complaints for 7 months.

No errors, no anything. Also, the answers that go through that same bot, appears to arrive to our clients correctly. The bot token is valid.

What happened? Maybe Telegram changed some rules, do you know something? The code for bot initialization is quite simple and standard:

import { Telegraf, Markup, Composer, Scenes, session } from 'telegraf'
import { message } from 'telegraf/filters'
import { Mongo as mongoSession } from "@telegraf/session/mongodb";
import { stages as menuWizardStages } from './wizards/menu'
import { stages as newTicketWizardStages } from './wizards/newTicket'
import { stages as oldTicketWizardStages } from './wizards/oldTicket'
import { startHandler } from './commands'
import { blockedUserHandler } from './commands'
import handleMessage from './handleMessage'
import mediaGroup from 'telegraf-media-group'

export async function setupBot() {
    try {
        console.log('in setupBot')
        
        await global.getDBInitialized();

        const bot = new Telegraf(process.env.BOT_TOKEN)

        bot.telegram.setMyCommands([
            { command: 'start', description: 'Start' },
        ]).catch(e => console.warn(e));
        bot.telegram.setMyDescription("My bot description").catch(e => console.warn(e));
        global.telegrafBot = bot;
        
        bot.use(blockedUserHandler)
        bot.use(session()); // { store: dbSessionStore }
        const menuWizard = new Scenes.WizardScene('menuWizard', ...menuWizardStages);
        const newTicketWizard = new Scenes.WizardScene('newTicketWizard', ...newTicketWizardStages);
        
        const oldTicketWizard = new Scenes.WizardScene('oldTicketWizard', ...oldTicketWizardStages);
        
        bot.use(mediaGroup());
        const stages = new Scenes.Stage([menuWizard, newTicketWizard, oldTicketWizard])
        bot.use(stages.middleware());
        bot.start(startHandler)

        bot.on('edited_message', async function (ctx) {
            return await ctx.reply(`We do not process messages edits`)
        })
        bot.on('message', handleMessage)
        bot.launch()

        // Enable graceful stop
        process.once('SIGINT', function () {
            console.log('bot stopping with SIGINT')
            bot.stop('SIGINT')
        })
        process.once('SIGTERM', function () {
            console.log('bot stopping with SIGTERM')
            bot.stop('SIGTERM')
        })

        console.log('Bot is launched.')
    } catch (e) {
        console.warn(e)
    }
}

Normally, when a user sends /start to our bot, the bot accepts it and shows menu. When my problem appears, bot does nothing.

The code for startHandler:

export const startHandler = async function (ctx) {
    console.log('startHandler, ctx.scene', !!ctx.scene, 'ctx.wizard', !!ctx.wizard)
    await ctx.scene?.leave();
    
    await ctx.scene?.enter('menuWizard');

    return;
}

I have logs all over my bot app. At some point, bot just stops receiving any messages. Now we have around 150 requests per day (that is implying that 150 Telegraf Wizards are created through that day).

I am now "fixing" the problem by restarting the process on my server. That is a crutch, I need some real solution.

发布评论

评论列表(0)

  1. 暂无评论