skip to Main Content

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


  1. 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 :

    @bot.message_handler(commands=['start'])
    def start(message):
        user_id = message.from_user.id
        bot.reply_to(message, f"Your UserID is {user_id}.")
    
    
    Login or Signup to reply.
  2. 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.

    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 danogMadelineProtoSettingsAppInfo() )
        ->setApiId( YOUR_API_ID )
        ->setApiHash( 'YOUR_API_HASH' );
    
    $MadelineProto = new danogMadelineProtoAPI( 'session.madeline', $settings );
    
    $MadelineProto->botLogin( 'YOUR_BOT_TOKEN' );
    $MadelineProto->start();
    $user = $MadelineProto->getInfo( $username );
    
    echo $user['user_id'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search