S
S
s1veme2020-08-29 11:14:44
Bots
s1veme, 2020-08-29 11:14:44

Telebot - How to get user ID from username?

I know the username of the user, how to get his ID?
The bot is in a chat.

You can parse all chat participants, and then check the username - but this seems to be impossible.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-08-29
@aleksegolubev

There is no direct way,
there are methods to get the number of participants in the chat (you need to know the group_chat_id):
bot.get_chat_members_count(group_chat_id)
a method for getting admins
bot.get_chat_administrators(group_chat_id)
to get info on users, you need the chat_id of each, and here it is possible only with tricks
bot.get_chat_member(group_chat_id, chat_id )

  • record all chat messages from users and create a USERS table , with chat_id, username, first_name, last_name fields. If the chat is active, then quickly get everyone and match.
  • track " joined group " and " left group " messages

There is a difficulty that username, first_name, last_name are not mandatory fields, that is, there may be users who do not have them filled. Therefore, in my opinion, it is optimal to parse the messages of the group, and enter each one who wrote into the database table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question