【问题标题】:Telegram API, get the sender's phone number?Telegram API,获取发件人的电话号码?
【发布时间】:2016-11-13 11:55:05
【问题描述】:

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

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

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

【问题讨论】:

  • 您有用户 ID 吗?如果是这样,您可以查询getFullUser,然后从中检索User,其中包含电话号码作为字符串。
  • 我应该收到一条消息,收到后,检查消息的发件人。您是指我提到的对等 ID 吗?
  • 这不是真的:“不可能使用 Bot 接收发件人的电话号码。”[reference] 您可以通过特殊键检索用户电话号码,如果他/她点击它,他/她的电话号码将发送给机器人。我带着你的telegram-bot 关键字来到这里,但你的问题与它没有任何关系。请删除该关键字friend。
  • 嗯,这个想法是识别任意发件人的电话号码。我希望@James Paterson 的想法能够做到。
  • @ted 你不需要手机号码,如果你说你只需要回复给你发消息的用户。

标签: php chat bots telegram telegram-bot


【解决方案1】:

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

以及用户按下按钮后从“visit card”获取 unic 用户电话的代码

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

【讨论】:

  • \@vladimir 是 $auth_keyboard = $keyboard 吗?是错字吗?
  • @inMILD 请在希望他回复之前查看他的最后一次见面。
【解决方案2】:

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

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

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';
}

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

【讨论】:

    猜你喜欢
    • 2018-03-01
    • 1970-01-01
    • 2014-05-29
    • 2018-07-18
    • 2020-01-04
    • 2017-08-28
    • 2022-12-08
    • 1970-01-01
    • 2015-01-24
    相关资源
    最近更新 更多