TelegramBot / Api

Native PHP Wrapper for Telegram BOT API
MIT License
1.07k stars 324 forks source link

Как сделать кнопку sendContact? #272

Closed Albert2106 closed 3 years ago

Albert2106 commented 3 years ago

Как сделать кнопку sendContact? Можно пример?

MyZik commented 3 years ago

Как-то так.

$buttons = [
    ['text' => 'Send contact', 'request_contact' => true],
];

$replyMarkup = new ReplyKeyboardMarkup($buttons, true, false, true);

https://core.telegram.org/bots/api#replykeyboardmarkup

https://core.telegram.org/bots/api#keyboardbutton

Albert2106 commented 3 years ago

Не получаеться вот код:

$bot->command("getcontact", function ($message) use ($bot) {
            $buttons =
                [
                    ['text' => 'Send contact', 'request_contact' => true]
                ];

    $replyMarkup = new \TelegramBot\Api\Types\ReplyKeyboardMarkup($buttons, true, false, true);

    $bot->sendContact($message->getChat()->getId(), "Отправьте номер",$replyMarkup);
});

Выходят ошибки: Fatal error: Uncaught Error: Object of class TelegramBot\Api\Types\ReplyKeyboardMarkup could not be converted to string in \vendor\telegram-bot\api\src\BotApi.php:258

MyZik commented 3 years ago

$bot->sendMessage($message->getChat()->getId(), "Отправьте номер", null, false, null, $keyboard);

MyZik commented 3 years ago

@L1keStyle проблема решена?

Albert2106 commented 3 years ago

Спасибо

CvekCoding commented 3 years ago

Не знаю как раньше было, но сейчас Телеграм выдает ошибку: Bad Request: field "keyboard" of the ReplyKeyboardMarkup must be an Array of Arrays. Массив

            $buttons =
                [
                    ['text' => 'Send contact', 'request_contact' => true]
                ];

Преобразуется в массив объектов, а не в массив массивов как требует API

Dioved commented 2 years ago

Кнопку запроса телефона юзера у меня так получилось сделать:

$bot = new \TelegramBot\Api\Client($token);

// кнопка
$bot->command('phone', function ($message) use ($bot) { 
    $buttons =
        [[
            ['text' => 'Отправить номер', 'request_contact' => true]
        ]];
    $replyMarkup = new \TelegramBot\Api\Types\ReplyKeyboardMarkup($buttons, true, false, true);
    $bot->sendMessage($message->getChat()->getId(), 'Мы запомнили Ваш номер', null, false, null, $replyMarkup);
});

// получение номера
$bot->on(function (\TelegramBot\Api\Types\Update $update) use ($bot) {
    $message = $update->getMessage();
    if ($message->getContact() != null) {
        $bot->sendMessage($message->getChat()->getId(), 'Your phone: ' . $message->getContact()->getPhoneNumber());
    } else {
        $bot->sendMessage($message->getChat()->getId(), 'Your message: ' . $message->getText());
    }
}, function () {
    return true;
});
snitko21 commented 1 year ago

I fixed your problem, your $replyMarkup stay in position attribute reply message id!

$bot = new \TelegramBot\Api\Client($token);

// кнопка
$bot->command('phone', function ($message) use ($bot) { 
    $buttons =
        [[
            ['text' => 'Отправить номер', 'request_contact' => true]
        ]];
    $replyMarkup = new \TelegramBot\Api\Types\ReplyKeyboardMarkup($buttons, true, false, true);
    $bot->sendMessage($message->getChat()->getId(), 'Мы запомнили Ваш номер', null, false, null, null, $replyMarkup);
});

// получение номера
$bot->on(function (\TelegramBot\Api\Types\Update $update) use ($bot) {
    $message = $update->getMessage();
    if ($message->getContact() != null) {
        $bot->sendMessage($message->getChat()->getId(), 'Your phone: ' . $message->getContact()->getPhoneNumber());
    } else {
        $bot->sendMessage($message->getChat()->getId(), 'Your message: ' . $message->getText());
    }
}, function () {
    return true;
});