wowkin2
wowkin2

Reputation: 6355

How to track when user kicked Telegram Bot using python-telegram-bot (Bot API 5.1)

Telegram introduced Bot API 5.1 with ChatMemberUpdated and the fields my_chat_member and chat_member in the Update class.

But how to track when the user stopped the bot now if I'm using ConversationHandler?

Previously, my bot was waiting for commands like /cancel or /stop.

conversation_handler = ConversationHandler(
    entry_points=...,
    states=...,
    fallbacks=[
        # ...
        MessageHandler(Filters.regex('^/(cancel|stop)$'), flow_stop_chat),
        # ...
    ],
    allow_reentry=True,
    name=current_bot_label,
    persistent=True,
)

How to do that correctly now using ChatMemberUpdated?

Upvotes: 0

Views: 2508

Answers (1)

CallMeStag
CallMeStag

Reputation: 7050

Catch the update using ChatMemberHandler (new in PTB v13.4) and check update.my_chat_member.new_chat_member.status to see if your bot was blocked. However blocking a bot is not quite the same as simply sending a /cancel command, so I'd suggest to keep the MessageHandler (or better yet, change it to CommandHandler(['cancel', 'stop'], flow_stop_chat))

Upvotes: 1

Related Questions