Skip to content

Instantly share code, notes, and snippets.

@egin10
Last active January 23, 2021 14:53
Show Gist options
  • Save egin10/5f2edff33a0ae0a6c67d6e007e1e6fd2 to your computer and use it in GitHub Desktop.
Save egin10/5f2edff33a0ae0a6c67d6e007e1e6fd2 to your computer and use it in GitHub Desktop.
Get Phone Number with package telegram bot for laravel 8 : "irazasyed/telegram-bot-sdk": "^3.4"
<?php
namespace App\Telegram\Commands;
use Telegram\Bot\Keyboard\Keyboard;
use Telegram\Bot\Commands\Command;
use Telegram;
/**
* Class VerifyCommand.
*/
class PhoneNumberCommand extends Command
{
/**
* @var string Command Name
*/
protected $name = 'phone';
/**
* @var array Command Aliases
*/
protected $aliases = ['phonenumber'];
/**
* @var string Command Description
*/
protected $description = 'Get phone number.';
/**
* {@inheritdoc}
*/
public function handle()
{
$response = $this->getUpdate();
$chat_id = $response->getChat()->getId();
$btn = Keyboard::button([
'text' => 'Share',
'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 button and chose share.',
'reply_markup' => $keyboard
]);
}
}
<?
...
class TelegramController extends Controller
{
...
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';
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment