繁体   English   中英

Telegram API,获取发件人的电话号码?

[英]Telegram API, get the sender's phone number?

我意识到不可能使用a Bot来接收发件人的电话号码。
但是,我确实需要实现一个类似机器人的客户端,它可以响应任何发送消息的人。 我在 apache 上使用 PHP。

它不是机器人,因为它不接受命令,而是响应任何拥有该电话号码的人发送的文本。 因此,您将用户添加为联系人(使用电话号码),然后向其发送文本。

我的目标是在收到发件人的电话号码时实现它,我在电报 API 上看到有一个对等 ID,但如果可能的话,我找不到如何获取电话号码......

从 github https://github.com/irazasyed/telegram-bot-sdk试试这个库

和在私人聊天中创建“访问卡”按钮的代码:

$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 ]);

和代码在用户按下按钮后从“访问卡”获取用户电话

$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 ]);
   }

最后我找到了如何获取电话号码。 我为 Laravel 8 使用包https://github.com/irazasyed/telegram-bot-sdk

下面的代码是发送按钮的命令,当用户按下按钮时,它会在用户点击“分享”后从用户那里获取电话号码。

电话号码命令.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
    ]);
}

控制器Webhook.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';
}

这对我有用! 希望你也能实施。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM