M
M
Maxim2017-11-29 17:21:44
PHP
Maxim, 2017-11-29 17:21:44

Telegram bot, how to process geolocation?

I'm learning php, trying to write a telegram bot. I request geolocation through the button with request-location
. In response, a point on the map comes. I don’t understand how to process it and write the $lat,$lon
code into variables:

$output = json_decode(file_get_contents('php://input'),true);
$id = $output['message']['chat']['id'];
$token='***';
$ApiKey='***';      
$message= $output['message']['text'];
$Location=$output['longitude']['latitude'];
switch ($message) {
case '/start':
    $message = 'Привет! Нажми отправить местоположение чтобы начать.';
    sendMessage($token, $id, $message . KeyboardMenu());
    break;
case $Location:
    $lat = $Location['latitude'];
    $lon = $Location['longitude'];
    if (isset($Location['longitude']['latitude']))
        {
            $message = "Отлично! ваше местонахождение определено." .$lat. $lon;
        }
   else
       {
            $message ="error";
       }
   sendMessage($token, $id, $message,$lat,$lon . KeyboardMenu().get_address($lat, $lon, $ApiKey));
break;
 default:
    $message='Неправильный запрос.';
    sendMessage($token,$id,$message.KeyboardMenu());
function sendMessage($token, $id,$message)
{
file_get_contents("https://api.telegram.org/bot" . $token . "/sendMessage?
chat_id=" . $id . "&text=".$message);
}
function KeyboardMenu(){
    $buttons = [,,];
     $keyboard =json_encode($keyboard=['keyboard' => $buttons,
                                        'resize_keyboard' => true,
                                        'one_time_keyboard'=> false,
                                        'selective' => true]);
   $reply_markup ='&reply_markup='.$keyboard.'';
   return $reply_markup;
}

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kapnuu, 2017-11-30
@Maxim_Evg3nievich

$Location=$output['message']['location'];
just keep in mind that message does not always contain location. however, text is also not always there. before processing the response, I advise you to look at the received json with your eyes - it can be useful)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question