How to Receive Data When a User Blocks Your Telegram Bot

Telegram bot and Mini App can receive data (Update) when a user blocks the bot.

Graspil tracks this data and provides corresponding reports. This information is crucial as it affects other reports, such as the bounce rate. If you don't see user exit data, either you don't have it or you have disabled receiving such data.

How to enable receiving this data?

In the Telegram Bot API, you need to enable receiving my_chat_member updates. Once enabled, Telegram will start sending updates when a user blocks the bot.

If the bot works via getUpdates

Pay attention to the getUpdates method, which retrieves updates from Telegram. It has the allowed_updates parameter, where you can specify the types of updates to receive. Add my_chat_member to this list or leave it empty to receive all update types.

If the bot works via Webhook

To check which types of updates your bot currently receives, use the getWebhookInfo method. The allowed_updates parameter will list the update types that Telegram sends to your bot.

To modify these settings, use the setWebhook method. In the allowed_updates parameter, specify the update types you want to receive, including all existing ones, and add my_chat_member. Alternatively, leave allowed_updates empty to receive all update types.

Make sure to correctly fill in the other parameters in the request.

Testing

After making changes, test the botโ€™s functionality. Ensure that blocking and restarting the bot work correctly without errors.

These steps are sufficient for Graspil to start tracking this data.

Some technical details

Example of my_chat_member data:

{
    "update_id": 123456,
    "my_chat_member": {
        "chat": {
            "id": 123456,
            "first_name": "You",
            "last_name": "are",
            "username": "cool",
            "type": "private"
        },
        "from": {
            "id": 123456,
            "is_bot": false,
            "first_name": "You",
            "last_name": "are",
            "username": "cool",
            "language_code": "en",
            "is_premium": true
        },
        "date": 1740227800,
        "old_chat_member": {
            "user": {
                "id": 123,
                "is_bot": true,
                "first_name": "graspil_bot",
                "username": "graspil_bot"
            },
            "status": "member"
        },
        "new_chat_member": {
            "user": {
                "id": 123,
                "is_bot": true,
                "first_name": "graspil_bot",
                "username": "graspil_bot"
            },
            "status": "kicked",
            "until_date": 0
        }
    }
}

Pay attention to the old_chat_member and new_chat_member parameters. The status field indicates the userโ€™s new status. In the example above, the old status is "member," and the new status is "kicked," meaning the user has blocked the bot.

The my_chat_member update is also received when a user reactivates the bot. In this case, the situation is reversed: the new new_chat_member.status will be "member."