Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect whether a user deletes the Telegram Bot chat?

Is it possible to check, via an API, whether somebody has deleted the Bot Chat in Telegram?

My test : Currently if a user deletes a chat, new messages will not stop sending to user.

like image 851
Yoones Mehdian Avatar asked Feb 08 '16 06:02

Yoones Mehdian


People also ask

Can Telegram BOT delete messages?

If the bot is an administrator in a supergroup, it can delete messages from any other user and service messages about people joining or leaving the group (other types of service messages may only be removed by the group creator). In channels, bots can only remove their own messages.

How can I know who is using my telegram bot?

Unfortunately telegram doesn't provide such info. You should log the ID of any user, using your bot. A simple solution could be to create a database with the user ID primary key, the registration timestamp and the last usage timestamp. Show activity on this post.


2 Answers

Nope. Only by getting error while sending user something.

Even calling sendChatAction method does not return error if user blocked the chat:

$ curl https://api.telegram.org/bot***/sendChatAction -d 'chat_id=7975895' -d "action=typing"
{"ok":true,"result":true}
$ curl https://api.telegram.org/bot***/sendMessage -d 'chat_id=81083738' -d "text=ololo"
{"ok":false,"error_code":403,"description":"[Error]: Bot was blocked by the user"}
like image 146
ihoru Avatar answered Sep 18 '22 08:09

ihoru


Someone in other answers suggested to use getChat method.

Do not use it for two reasons:

  • even if they can return some special errors, they can change at anytime. Instead, any send method should safe! (This is the answer I got contacting the official support months ago)
  • from my usage I know that getChat results can be cached for a lot of weeks and you won’t have a safe result. i.e. I was able to get regular results of groups using getChat even after a month they kicked the bot from the group.

So what can you do? You can use send methods and I would suggest you to use sendChatAction so you won’t bother users.

You can handle exceptions and check the code of the error returned (403 is the error for this case). Furthermore at the moment I write this answer if the user blocked the bot the returned string contains the word “blocked”, while if he deleted the account the string contains the word “deactivated”.

For example for my bot I developed a function that is automatically runned on intervals, it has a for loop and try to sendChatAction to every users. I check the code of the error (403 is for this) and I check the text of the error. If it contains “blocked” I just mark the user as blocked in the database because I want to keep preferences, otherwise if it contains “deactivated“ I delete the user from the database. I did this to have stats about how many users my bot has and how many of them didn’t block the bot. If you want to do something like this also remember to add a sleep in the for loop because you can use only 30 sendChatAction per second before of hitting limits.

like image 40
91DarioDev Avatar answered Sep 20 '22 08:09

91DarioDev