I’m working on a Telegram bot using TypeScript and I’m having trouble with event detection. My bot can detect when people join a group, but it doesn’t seem to notice when they leave.
The message handler works fine when someone joins the group (my bot is an admin there), but nothing happens when a user leaves. Am I using the wrong event listener for this? What’s the proper way to track when members leave a group?
Your bot probably doesn’t have the right admin permissions to catch when people leave. Check that “Get basic group info” is turned on in your group settings - you need this for chat_member updates even if your bot’s already an admin. Also, Telegram can be slow with leave events, especially in big groups. Sometimes there’s a 2-10 second delay. Try logging all incoming updates first to see if the events are actually reaching your bot or if your code’s filtering them out.
You’re missing the my_chat_member event handler - that’s what catches when people leave. Your code only listens for regular messages, but member changes come through different update types. Add this:
telegramBot.on('my_chat_member', (update) => {
if (update.new_chat_member.status === 'left' || update.new_chat_member.status === 'kicked') {
console.log('User left the group:', update.from)
}
})
I ran into the same thing with my group bot last year. Here’s the key difference: chat_member tracks changes to other members, while my_chat_member tracks changes your bot can actually see. Also check that your bot has permissions to receive these updates in the group settings.
you gotta handle chat_member events on their own - they ain’t covered in the msg handler. just add telegramBot.on('chat_member', (update) => { console.log('member left:', update) }), cause leave events come through that listener, not the message one.