Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram Bot API: getChatMember throws USER_ID_INVALID for valid user

I'm trying to find out if a specific User is present in a supergroup, in order to keep track of those who left.

For that, I'm calling the Bot API method getChatMember for each User and checking if their status is either Left or Kicked. However, I noticed that (recently?) I'm getting USER_ID_INVALID errors for many valid users that are either in the supergroup or have been in the past and then left. I also confirmed that those accounts are still active on Telegram.

Here's the HTTP request I'm sending:

POST https://api.telegram.org/botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXX/getChatMember HTTP/1.1
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 46
Host: api.telegram.org

{"chat_id":-0000000000000,"user_id":000000000}

And here's the response I'm getting:

HTTP/1.1 400 Bad Request
Server: nginx/1.12.2
Date: Fri, 20 Apr 2018 04:17:32 GMT
Content-Type: application/json
Content-Length: 74
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection

{"ok":false,"error_code":400,"description":"Bad Request: USER_ID_INVALID"}

Any way I look at it, it looks like a perfectly valid request to me. And I haven't been able to find a common pattern between the users that throw this error.

What am I missing here?

EDIT: As @sean pointed out, having one of those users message the bot privately fixed the error for that particular user. But I'm absolutely sure that user was seen before because that's how I got his user ID. What could have caused the bot "forget" about him and how would I prevent this from happening in the future?

like image 564
Axel Magagnini Avatar asked Apr 20 '18 05:04

Axel Magagnini


People also ask

How can I send a message to someone with my telegram bot using their username?

Getting StartedOpen the telegram app and search for @BotFather. Click on the start button or send “/start”. Then send “/newbot” message to set up a name and a username. After setting name and username BotFather will give you an API token which is your bot token.

Why bots are not working in Telegram?

Suggestions: Ensure the Bot is in the group and as an admin. Try adding a new access token (revoke existing API key and create a new one then replace in plugin settings) If you edited the bot with @BotFather, make sure to restore it to the default settings.

How can I get Telegram API User ID?

When you've located @userinfobot, click or tap it. Then, select Start at the bottom of the chat. By clicking Start, you've automatically entered “/start.” It will prompt the bot to display your user ID, as well as your selected first name, last name, and the language of choice.

How can I get Telegram Channel users with Telegram Bot API?

To use MTProto, you need to login to https://my.telegram.org/ with your existing Telegram account and get credentials: api_id and api_hash . Here is a working example of how to use Telethon python library to get list of Telegram channel/group users. It is easy to search channels/users by name/phone/URL with client.


1 Answers

This error means your bot haven't seen this user before.

For instance, my user ID is 109780439, you can try getChatMember with @PublicTestGroup, it should response with 400 error.

And then, forward ANY of my message (e.g., this) to your bot, you will see the different result :)

like image 88
Sean Avatar answered Sep 22 '22 07:09

Sean