As the title says, is it possible to get userid using username? I mean, I would like to add a feature to my bot that tells you the user id from an other user using his username.
Does Telegram API give an option to do this?
As the title says, is it possible to get userid using username? I mean, I would like to add a feature to my bot that tells you the user id from an other user using his username.
Does Telegram API give an option to do this?
I believe what you mean by nickname is the username
and userid is the unique id
as described in the API documentation.It is still possible to do a reverse lookup using a bot.
This is possible with a boosted version of the Official Telegram API, known as PWRTelegram API
I quote from the documentation.
Resolving of usernames not only with channels and groups but also with normal users and bots
Update:
Here is a library I made for this purpose:
Update 2:
I no longer maintain the above library, you can however still resolve usernames with a td-cli binary.
The solution by Mohammod Sohail is partially wrong.
There are ways to get a user_id
from username
, but online tools mentioned above use more than the bot API. They have a telegram client running on their server.
That said there is a trick to get a user_id
from username
purely with bot API (doen't always work).
Lets say you are chatting with your friend X
and you want to know his user_id
. To get the user_id
, forward one of your conversations to your bot. The bot will get the message author's first name
, username
, user_id
, timestamp
, and so on in JSON format. Now you can extract the user_id
from that. Note that this also works even if the user does not have a username
, or if its a channel or another bot.
Here is an bot implementation of the above:
I've forwarded one of my friend's message to the bot @get_id_bot Then the bot returns the username and id of my friend's account
I believe what you mean by nickname is the username
and userid is the unique id
as described in the API documentation.It is still possible to do a reverse lookup using a bot.
This is possible with a boosted version of the Official Telegram API, known as PWRTelegram API
I quote from the documentation.
Resolving of usernames not only with channels and groups but also with normal users and bots
Update:
Here is a library I made for this purpose:
Update 2:
I no longer maintain the above library, you can however still resolve usernames with a td-cli binary.
You can use the pwrtelegram bot API to resolve usernames of bots and normal users.
It will automagically resolve the usernames you provide in the chat_id parameter using a built in mtproto client.
You can use usernames of bots and normal users in every method that requires the chat_id parameter, but to get info about a user/bot you can use the getChat method.
No. It's not possible right now.
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. This is the official way, no tricks, hacks or workarounds.
To use the MTProto Telegram API you first need to get an API ID as explained here:
api_id
and api_hash
parameters required for next stepYou 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).
@BotFather
), and enter the /newbot
command.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 given username
.
Here's a working example:
require_once 'madeline81.phar';
$settings = ( new \danog\MadelineProto\Settings\AppInfo() )
->setApiId( YOUR_API_ID )
->setApiHash( 'YOUR_API_HASH' );
$MadelineProto = new \danog\MadelineProto\API( 'session.madeline', $settings );
$MadelineProto->botLogin( 'YOUR_BOT_TOKEN' );
$MadelineProto->start();
$user = $MadelineProto->getInfo( $username );
echo $user['user_id'];
In 2024 now we have "getMe" method: https://core.telegram.org/bots/api#getme
That returns User object and contains user's(bot) "id"
© 2022 - 2024 — McMap. All rights reserved.