Telegram API, get the sender's phone number?
Asked Answered
Y

2

6

I realize it is not possible to use a Bot to receive a sender's phone number.
I do, however, need to implement a bot-like client, that responds to anyone who is messaging it. I am using PHP on apache.

It is not a Bot, as it does not take commands, but rather responds to sent text from anyone who has that phone number. So you add the user as a contact (using a phone number), and then send text to it.

My goal is to realise the sender's phone number as I receive it, I saw on the Telegram API that there's a peer ID, but I can't find how to get the phone number if that's even possible...

Yves answered 11/7, 2016 at 13:44 Comment(7)
Do you have the User ID? If so you can query for getFullUser, then retrieve the User from it which contains the phone number as a string.Phonemics
I am supposed to receive a message, upon receive, check the message's sender. Are you referring to what I mentioned as a peer ID?Yves
This is not true: " it is not possible to use a Bot to receive a sender's phone number."[reference] You can retrieve user phone number via a special key, that if he/she click on it,him/her phone number will send to bot. And I come here with your telegram-bot keyword but your question doesn't have any relation to it. please remove that keyword friend.Farceuse
Well, the idea was to recognize a phone number of an arbitrary sender. I am in hope that @James Paterson's notion should do.Yves
@ted you don't need the mobile number if as you say you only need to respond to the user who sent a message to you.Reinaldoreinaldos
@ted you receive the userid of the person that sent you a message. you should be able to use that to reply them directly. to get the phone number, and name possibly, you need something else.Reinaldoreinaldos
core.telegram.org/bots#location-and-numberDenudate
M
6

try this lib from github https://github.com/irazasyed/telegram-bot-sdk

and code to create 'visit card' button in private chat:

$keyboard = array(
                   array(
                        array( 
                              'text'=>"Send your visit card",
                              'request_contact'=>true
                              )
                        )
                 ); //user button under keyboard.

$reply_markup = $telegram->replyKeyboardMarkup([ 'keyboard' => $auth_keyboard, 'resize_keyboard' => true, 'one_time_keyboard' => false ]);
$telegram->sendMessage([ 'chat_id' => $chat_id, 'text' => $reply, 'reply_markup' => $reply_markup ]);

and code to get unic user phone from 'visit card' after user push the button

$user_phone = $result["message"]["contact"]["phone_number"];
if ($user_phone) {
        $reply = $user_phone;
        $telegram->sendMessage([ 'chat_id' => $chat_id, 'text' => $reply, 'reply_markup' => $reply_markup ]);
   }
Magnolia answered 3/5, 2017 at 12:38 Comment(2)
\@vladimir is $auth_keyboard = $keyboard ? Is it typo?Bisset
@Bisset please check his last seen before hoping he will reply.Oxide
O
2

finally i found how to get the phone number. I use package https://github.com/irazasyed/telegram-bot-sdk for Laravel 8.

The code below is command to send button and when user press the button, it'll catch the phone number from user after the user click on "share".

PhoneNumberCommand.php

public function handle()
{        
    $response = $this->getUpdate();
    $chat_id = $response->getChat()->getId();

    $btn = Keyboard::button([
        'text' => 'Varify',
        'request_contact' => true,
    ]);

    $keyboard = Keyboard::make([
        'keyboard' => [[$btn]],
        'resize_keyboard' => true,
        'one_time_keyboard' => true
    ]);

    return $this->telegram->sendMessage([
        'chat_id' => $chat_id, 
        'text' => 'Please click on Verify and Share.',
        'reply_markup' => $keyboard
    ]);
}

ControllerWebhook.php

public function commandHandlerWebHook()
{
    $updates = Telegram::commandsHandler(true);

    $chat_id = $updates->getChat()->getId();

    // Catch Phone Number
    $user_phone = array_key_exists('contact', $updates['message']) ? 
        $updates['message']['contact']['phone_number'] : null;
    $text = 'Phone number : ' . $user_phone;
    if($user_phone) return Telegram::sendMessage(['chat_id' => $chat_id, 'text' => $text]);

    return 'ok';
}

It's work for me! hope you can implement too.

Oui answered 23/1, 2021 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.