首页 > 解决方案 > 如何使用 Laravel 在电报机器人 SDK 中处理联系请求

问题描述

我正在使用https://github.com/irazasyed/telegram-bot-sdk在我的 Laravel 项目中开发一个机器人。我已使用回复键盘标记成功请求联系。问题是我无法进一步处理联系人。我需要有关如何使用我请求的电话号码进行进一步处理的帮助。

这是我现在使用的代码,

public function requestContact() 
{
    $btn = Keyboard::button([
        'text' => 'Share my phone number',
        'request_contact' => true
    ]);
    $keyboard = Keyboard::make([
        'keyboard' => [[$btn]],
        'resize_keyboard' => true,
        'one_time_keyboard' => true
    ]);
    Telegram::sendMessage([
        'chat_id' => $this->chat_id,
        'text' => 'I need your contact to get your ID. Please click the Share Contact button below.',
        'reply_markup' => $keyboard
    ]);ref
}

标签: phplaraveltelegram-botphp-telegram-bot

解决方案


首先:您不需要“共享联系人”来获取您在此处编写的用户 ID:

'text' => '我需要你的联系人来获取你的 ID。请点击下方的分享联系人按钮。

第二:我通过我的一个机器人分享了我的联系方式,电报为我发送了这个回复:

 {
   "ok": true,
   "result": [{
     "update_id": 751829617,
     "message": {
       "message_id": 5971,
       "from": {
         "id": 223110107,
         "is_bot": false,
         "first_name": "\u011e\u0105me",
         "last_name": "\u01fever!",
         "username": "GameO7er",
         "language_code": "en"
       },
       "chat": {
         "id": 223110107,
         "first_name": "\u011e\u0105me",
         "last_name": "\u01fever!",
         "username": "GameO7er",
         "type": "private"
       },
       "date": 1568724975,
       "contact": {
         "phone_number": "+1*******88",
         "first_name": ".",
         "user_id": 223110107
       }
     }
   }]
 }

因此,您可以在上面的 JSON 格式示例中看到,您可以执行以下操作:

TelegramResponse->message->contact->phone_number

上面的代码只是一个示例,它不起作用。但你可以在phone_number 这里查找


推荐阅读