skip to Main Content

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…

2

Answers


  1. 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 ]);
       }
    
    Login or Signup to reply.
  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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search