How to reset user preferences when a Telegram bot is removed?

I’m working on a Telegram bot using a Java library. I need to find a way to detect when a user removes the bot from their contacts. This is important because I want to clear their settings from my database.

Right now, if a user removes the bot and adds it again later, they’re stuck with their old settings. I’d like them to start fresh each time they add the bot.

Does anyone know if there’s a way to catch this ‘bot removed’ event? Or maybe a workaround to achieve the same result?

I’ve been searching for a solution but haven’t found anything concrete yet. Any ideas or suggestions would be really helpful!

Update: I found a solution to this problem. I’ll share the details in case anyone else runs into the same issue.

Based on my experience, there’s no direct API method to detect when a user removes a Telegram bot. However, you could implement a ‘last active’ timestamp for each user in your database. Update this timestamp whenever the user interacts with the bot. Then, periodically check for users whose ‘last active’ date is older than a certain threshold (e.g., 30 days). For these inactive users, you could attempt to send a silent message. If it fails, assume the bot was removed and clear their settings.

This approach isn’t foolproof, but it’s a practical compromise. It allows you to maintain a relatively clean database without constantly pinging all users. Just be sure to handle exceptions properly and respect Telegram’s rate limits when implementing this solution.

I’ve dealt with a similar issue before, and there’s no direct way to detect when a user removes a bot. However, I found a decent workaround that might help you out.

What I did was implement a ‘heartbeat’ system. Basically, the bot sends a silent message to each user periodically (say, once a day). If the message fails to send, you can assume the user has blocked or removed the bot.

Here’s the catch though - you’ll need to handle exceptions carefully. Not all failed messages mean the bot was removed. Sometimes it’s just temporary network issues.

Also, consider adding a ‘reset preferences’ command that users can use when they add the bot back. It’s not automatic, but it gives users control and solves the old settings problem.

Remember to respect Telegram’s rate limits when implementing any periodic checks. Good luck with your bot!

hey john, have you tried using the getChat() method to check if the bot is still in the user’s contacts? you could run this periodically and if it fails, assume the bot was removed. then clear their settings. might not be perfect but could work as a workaround