0

Below I reported a simple code that allows me to know my current location by clicking the "Current location" button. Once I sent the location I would like to be able to send a message showing the latitude and longitude of the location. I don’t know how to interact with the location sent by Telegram, can you help me?

<?php

define ('url',"https://api.telegram.org/bot[botToken]/");

$update= file_get_contents('php://input');

$update= json_decode($update, TRUE);

$chatId= $update['message']['from']['id'];

$text= $update['message']['text'];

$button= '[{"text":"Current location", "request_location": true}]';

position($chatId, "Click the button to share your location", $button);



function position($IdChat, $text, $tastiera){

    $tastierino = '&reply_markup={"keyboard":['.$tastiera.'],"resize_keyboard": true,"one_time_keyboard": true}';
    $url=  url."sendMessage?chat_id=$IdChat&parse_mode=HTML&text=".urlencode($text).$tastierino;
    file_get_contents($url);
}

?>
Lorenzo
  • 31
  • 4

1 Answers1

1

When you send a location to the bot, it receives an update with a message object which contains the location object with latitude and longitude.

You can get latitude and longitude by doing this:

$latitude = $update['message']['location']['latitude'];
$longitude = $update['message']['location']['longitude'];
GioIacca9
  • 406
  • 4
  • 8
  • Thank you so much. – Lorenzo May 26 '20 at 13:38
  • Hi @Lorenzo if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – GioIacca9 Jun 11 '20 at 21:14