Telegram Bot API: getChatMember throws USER_ID_INVALID for valid user
Asked Answered
L

2

9

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?

Lubric answered 20/4, 2018 at 5:14 Comment(0)
H
6

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 :)

Haiku answered 20/4, 2018 at 11:6 Comment(4)
Thanks for the reply. I understand what you say, but I think this is not the case. I know for a fact that my bot has seen these users at least once, because that's how I know their User IDs. Is it possible that somehow the bot has "unseen" those? Or can anything else cause this error?Lubric
Can you getUserProfilePhoto with their user ID?Haiku
Nope, it throws the same error. But interestingly enough, I asked one of those users to message the bot privately, and now the error is gone for that particular user. So it looks like somehow the bot "forgot" about some users. Do you have any idea what could cause that?Lubric
Is there somewhere that you can see the "seen list of the bot"? because suddenly tonight it seems like that my bot has forgotten all the users he knew and I get USER_ID_INVALID for all the users that I could ban before. if there is a place that bot saves the seen list maybe I could understand what has happened @SeanSordino
O
-2

You will create a variable who get your channel's result, like this:

$join : api.telegram.org/botYOURTOKEN/getchat .....

if($message && (strpos($join,'"status":"left"') or strpos($join,'"Bad Request: USER_ID_INVALID"') or strpos($join,'"status":"kicked"'))!== false) {
}
Overdye answered 24/3, 2021 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.