skip to Main Content

I have a list of phone numbers.

I need their matching username and first_name in Telegram(if they subscribed to Telegram).

I want to get these info(username and first_name) with Telegram BOT API.

Is it possible? How can I do this? (with PHP)

This is my sample code:

<?php

//telegram sdk and configuration
$client = new ZeleninTelegramBotApi('someRandomToken');

$update = json_decode(file_get_contents('php://input'));

//your app
try {

    if($update->message->text != '/next_event_datetime')
        exit;

    $dom = SunraPhpSimpleHtmlDomParser::file_get_html('http://laratalks.com/');
    $element = $dom->find('#location header.section-header h3', 0);
    $dateTime = $element->plaintext;

    $response = $client->sendMessage([
        'chat_id' => $update->message->chat->id,
        'text' => $dateTime
    ]);

} catch (ZeleninTelegramBotNotOkException $e) {

    //echo error message ot log it
    //echo $e->getMessage();

}

2

Answers


  1. In short words it is IMPOSSIBLE. But read whole post, it may be useful for you.

    Getting phone number from Telegram and via Telegram Bot API is one-way,
    that means you can NOT send phone number and get users data from telegram.(Note: We assume you do not added users by phone number before)

    There is only two ways to get users phone number in telegram:

    1- Get it with specific KeyboardButton button. In this way Telegram sends users phone number after user accepted .( Note: User MUST be your Bot subscriber before)

    2- Ask user him/her phone number, but it is not reliable and you should check it with other ways(like SMS approve and so on)

    after all, although these two ways can be used to grab user’s phone number but when you grab phone number you have, so your list is not useful.

    BUT you want to grab username with phone number available,this is IMPOSSIBLE due to Telegram Bot API infrastructur .

    Login or Signup to reply.
  2. Well, you can get the username and user_id from a user when you have the phone number. I don’t know of any way to get the first name.

    Just send a contact (sendContact) with any first_name and the phone number. Telegram responds with the sent contact, which includes the user_id (if the user has Telegram) and username if the user has set one.

    Step by step:

    1. Send a contact with the number and and any first name to any chat (yourself, a group, doesn’t matter…)
    2. The Telegram API will respond with the send message or some error code. If the message was sent successful, the contact in the response may have more details (username and user_id).
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search