Telegram Bot: Is it possible to get userid using username?
Asked Answered
B

6

13

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?

Bumblebee answered 5/6, 2016 at 17:9 Comment(3)
check out the updated answerPellitory
@MohammedSohail I'll check it this weekendBumblebee
For anyone interested, yes there is a way to resolve a userid from username. check my answer below.Ganymede
P
5

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.

Pellitory answered 9/6, 2016 at 21:23 Comment(3)
Ok, what API call do you use?Nascent
I guess because you just say "Yeah, it's possible", but not how.Nascent
Can you give us an example how you get the userid of a user @foo?Nascent
G
6

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

GET ID BOT

Ganymede answered 27/12, 2020 at 11:32 Comment(2)
This is nice solution, but it doesn't work if user set forward privacy for his messages.Veridical
just want to say thank you for using my bot get_id_bot, I think I will add the getChat method mentioned in the answer of @danogentili to my botErysipelas
P
5

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.

Pellitory answered 9/6, 2016 at 21:23 Comment(3)
Ok, what API call do you use?Nascent
I guess because you just say "Yeah, it's possible", but not how.Nascent
Can you give us an example how you get the userid of a user @foo?Nascent
I
4

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.

Inkling answered 5/9, 2016 at 18:57 Comment(0)
H
1

No. It's not possible right now.

Helotry answered 6/6, 2016 at 8:0 Comment(1)
Hmmm... Damn... I will have to do something different to do what I want. Thanks anyway.Bumblebee
C
0

Telegram APIs

There are two types of free Telegram APIs, as explained here:

  • The Bot API for simple programs that use Telegram messages for an interface and
  • The Telegram API for more complex applications, even your own customized Telegram clients.

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.

Creating your Telegram Application

To use the MTProto Telegram API you first need to get an API ID as explained here:

  1. Sign up for Telegram using any application
  2. Log in to your Telegram core: https://my.telegram.org
  3. Go to "API development tools" and fill out the form
  4. You will then get api_id and api_hash parameters required for next step

A 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

  1. Log in to your Telegram, start a conversation with the BotFather (@BotFather), and enter the /newbot command.
  2. Choose a display name and username for your bot. you can edit image, description etc. later.
  3. BotFather will register your bot and reply with a token. Save that.

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 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'];
Cravens answered 15/5 at 22:7 Comment(0)
A
-2

In 2024 now we have "getMe" method: https://core.telegram.org/bots/api#getme

That returns User object and contains user's(bot) "id"

Absorber answered 2/5 at 11:44 Comment(1)
This has nothing to do with the original question.Cravens

© 2022 - 2024 — McMap. All rights reserved.