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
- Create a Telegram bot using @BothFather
- Replace the token got from
BothFatherinprocess.env.BOT_TOKEN - Create the bot pasting the code above
- Create a group and with another account join to the group
- In the console will appear
new_chat_members - Now lef the group, as you can see the event
left_chat_memberwill 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?