Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

left_chat_member not invoked

Bot Description

I'm using TelegrafJS for develop a Telegram bot. I've created my own middleware that should handle all the update messages sended to a group, in particular, if the update is a service message the bot must remove it.

The Problem

The problem's that the bot only catch new_chat_members but when a user left the chat, the event left_chat_member is not invoked.

Middleware code

class MessageHandler {
    /**
     * Check if is a message service
     */
    _checkServiceMessage(ctx) {
        let unwantedMessages = ['new_chat_members', 'left_chat_member', 'new_chat_title'];
        let isUnwanted = unwantedMessages.some(x => ctx.updateSubTypes.includes(x));

        if (isUnwanted) {

            console.log(ctx.updateSubTypes);

            // Delete service message
            ctx.deleteMessage().then(function (result) {

                // add or remove user to my own db

            }).catch(function (e) {
                console.log(e);
            });
        }
    }

    middleware() {
        return (ctx, next) => {

            this._checkServiceMessage(ctx);

            return next();
        }
    }
}

module.exports = MessageHandler;

this is how to use the Middleware:

const Telegraf = require('telegraf');
const MessageHandler = require('../middlewares/handler');
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.use(new MessageHandler().middleware());
bot.startPolling();

How to replicate

  1. Create a Telegram bot using @BothFather
  2. Replace the token got from BothFather in process.env.BOT_TOKEN
  3. Create the bot pasting the code above
  4. Create a group and with another account join to the group
  5. In the console will appear new_chat_members
  6. Now lef the group, as you can see the event left_chat_member will not appear.

The weird thing is that: if I remove ctx.deleteMessage(), so all the codes in isUnwanted, the event left_chat_member appear.

I threw away days on this problem and I can't get over it. I opened a ticket on TelegrafJS but so far nothing yet.

I need to store in my own database the user which join to my group, and remove when a user leave.

In my code I used the bot in two groups, I don't know if this could cause a problem but I don't think so.

Could someone explain me what's going on?

like image 654
sfarzoso Avatar asked Nov 16 '22 13:11

sfarzoso


1 Answers

Bots with privacy mode enabled don't get service message updates (as well as other types of updates). Telegram also doesn't send leave events if the group has >50 members.

like image 112
Avrumy Avatar answered Dec 23 '22 10:12

Avrumy