0

I want to create a bot that kicks users from group based on certain condition (doesn't matter). My problem is - I found function that kicks users ( Bot.ban_chat_member() from python-telegram-bot ), but it kicks them based on their IDs. How to obtain someone's ID, when the only data I have is their username?

Thank you!

Tried methods from python-telegram-bot library

2
  • This question is similar to: Telegram Bot: Is it possible to get userid using username?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 5, 2024 at 14:48
  • Please provide enough code so others can better understand or reproduce the problem. Commented Nov 5, 2024 at 15:55

1 Answer 1

0

How to find the user depends on how you are getting the username in the first place.

For example, if you are doing it based on a message the banned user sent, Message.from_user exposes the User object of the sender, giving you access to the user id.

Alternatively, if you have a command like /ban @username with the user tagged, the Message.entities returns you a list of MessageEntitys, which you can iterate through to find the tagged user's entity. See the documentation for Message.entities and MessageEntity.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.