I am trying to create my own admin bot that can handle a lot of the admin stuff.
how do I get a users ID via their username? I don’t see any way to get it from the API, or did I just miss it?
If you are interested, I’m using Telebot.
user = bot.get_chat(username)
2
Answers
In Telegram, you cannot directly obtain a user’s unique identifier (UserID) using their username unless the user has interacted with your bot at least once. Once a user interacts with your bot, you can obtain their UserID.
Here’s how you can use the
telebot
library to get a user’s UserID :Telegram APIs
There are two types of free Telegram APIs, as explained here:
The latter is much more powerful and it had methods to obtain the user_id from a provided username, without the need of chats or messaging interaction like the Bot Api would require.
Creating your Telegram Application
To use the MTProto Telegram API you first need to get an API ID as explained here:
api_id
andapi_hash
parameters required for next stepA library to interact with Telegram
You can then use a MTProto telegram client like MadelineProto to interact with the API. The full documentation for this library is here. This can login with a phone number (MTProto API), or with a bot token (MTProto API).
Get a Bot Token to Login
@BotFather
), and enter the/newbot
command.A PHP Example
Head over to releases, get the latest phar file and include it in your application. You can then use getInfo or getFullInfo methods to retrieve the
user_id
from a givenusername
.Here’s a working example: