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
BothFather
in process.env.BOT_TOKEN
new_chat_members
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With