我发现无法使用a Bot
来接收发件人的电话号码
但是,我确实需要实现类似机器人的客户端,它会响应任何正在发送消息的人。我在apache上使用PHP。
它不是Bot,因为它不接受命令,而是响应任何拥有该电话号码的人发送的文本。因此,您将用户添加为联系人(使用电话号码),然后向其发送文本。
我的目标是在收到发件人的电话号码时意识到这一点,我在Telegram API看到有同行ID,但我找不到如何获取电话号码,如果可能的话......
答案 0 :(得分:3)
从github https://github.com/irazasyed/telegram-bot-sdk
尝试此lib以及在私人聊天中创建“访问卡”按钮的代码:
$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 ]);
}
答案 1 :(得分:1)
我终于找到了如何获取电话号码的方法。 我为 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';
}
这对我有用!希望你也能实施。