Core: How to request location by InlineKeyboard?

Created on 3 Sep 2017  路  8Comments  路  Source: php-telegram-bot/core

Hello. How can I request a user's location and contact details by pressing an InlineKeyboard button? Is there any possibility to attach such request to callback_data?

All 8 comments

Not that I'm aware of.

You could however use a conversation and ask the user to either send a location or share a contact directly with the bot, then update the inline keyboard. This could be a bit tricky though.

So it's possible only by "simple" keyboard? And is it possible to attach some data to "standard" keyboard when it sends user's location? Because it works fine if I use "Send my location" button from the example "KeyboardCommand.php". But if there would have been existing the same "function" but with inline keyboard then it would be the best :)

So, the script looks next way: Bot asks a user a question and shows one button "send location". After pressing this button bot will have to convert location into address and send it in a next message to the user. And is it true that it can't be done by InlineKeyboard, but only by Keyboard?

Yes, it would be really cool if sending location and contact would work with inline keyboards too, maybe in the future!

So what you want to do is get the user location and convert it to an address? With an external API?

Yes. I want to convert coordinates. Here is the API I will use: https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding

And is the only way to attach additional parameters to InlineKeyboard to write it with a delimiter and then explode? Thanx for replies :)

Yes and no. Using delimiters in your callback_data and exploding is the easiest, for sure.

Alternatively, you could also manage all callback related data in the DB, which i haven't used so far.

Can you please tell how I can read the location by the bot right after a user sent it?
I'm using GenericmessageCommand.php, and this is how I'm trying to catch the answer:

$message = $this->getMessage();
$chat    = $message->getChat();
$chat_id = $chat->getId();
$user_id = $message->getFrom()->getId();
$text    = trim($message->getText(true));

$data = [
        'chat_id' => $chat_id,
        ];

if (strstr($text, 'phone_number') !== false) {
        $getNumAndName = json_decode($text);
        $data['text'] = 'Your phone number is:'.$getNumAndName['phone_number'] . PHP_EOL . 'Wait for approval.';
        return Request::sendMessage($data);
}

As example here I used the same request but for contact.

Sorry, just read the Entities and found the next code working:

```PHP
$message = $this->getMessage();
$chat = $message->getChat();
$chat_id = $chat->getId();
$user_id = $message->getFrom()->getId();
$text = trim($message->getText(true));
$contact = $message->getContact();

$data = [
'chat_id' => $chat_id,
];

if ($contact->getPhoneNumber()) {
$data['text'] = 'Your phone number is:' . $contact->getPhoneNumber() . PHP_EOL . 'Wait for approval.';
return Request::sendMessage($data);
}

You might find inline queries interesting too:

https://core.telegram.org/bots/api#inlinequeryresultlocation
https://core.telegram.org/bots/api#inputlocationmessagecontent

This would allow something like this:
User types @your_bot <latitude>,<longitude> (or whatever format) and you then show the map with the appropriate address!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noplanman picture noplanman  路  3Comments

NabiKAZ picture NabiKAZ  路  3Comments

TheSleepySlee picture TheSleepySlee  路  3Comments

smaznet picture smaznet  路  4Comments

Recouse picture Recouse  路  3Comments