Eleirbag89 / TelegramBotPHP

A very simple PHP Telegram Bot API for sending messages.
http://eleirbag89.github.io/TelegramBotPHP
MIT License
807 stars 341 forks source link

How get user location? #161

Closed marcelozao closed 3 years ago

marcelozao commented 6 years ago

How use Location function ?

Mohsen322 commented 6 years ago

hi. check this sample

$option = array(array($telegram->buildKeyboardButton("click to send location!",false,true)));
$keyb = $telegram->buildKeyBoard($option, $onetime=false);
$content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => "please send your location via Button...");
$telegram->sendMessage($content);
marcelozao commented 6 years ago

Great! Work! Thank you ! And where (variables) coordinates send by user, or how recovery ?

Mohsen322 commented 6 years ago

your welcome here my full code i wrote and tested before :)) ` include("Telegram.php");

// Set the bot TOKEN date_default_timezone_set("Asia/Tehran");

$bot_id = "Your_Token"; // Instances the class $telegram = new Telegram($bot_id);

// Take text and chat_id from the message $text = $telegram->Text(); $chat_id = $telegram->ChatID();

$type = $telegram->getUpdateType();

if($type=='location'){

$latitude = $telegram->Latitude();
$longitude = $telegram->Longitude();

$content = ['text' => '<b>Latitude:</b> '.$latitude, 'parse_mode' => 'HTML','chat_id' => $chat_id];
$telegram->sendMessage($content);

$content = ['text' => '<b>Longtitude:</b> '.$longitude, 'parse_mode' => 'HTML','chat_id' => $chat_id];
$telegram->sendMessage($content);

}

if($type=='contact'){

$phone = $telegram->getContactPhoneNumber();
$name = $telegram->getContactFirstName();

$content = ['text' => '<b>Phone Number:</b> '.$phone, 'parse_mode' => 'HTML','chat_id' => $chat_id];
$telegram->sendMessage($content);

$content = ['text' => '<b>Contact Name:</b> '.$name, 'parse_mode' => 'HTML','chat_id' => $chat_id];
$telegram->sendMessage($content);

} `

Mohsen322 commented 6 years ago

i just forgot one more thing!

these methods doesnt exist in main class. i added these, u can use freely 👍

` /// return latitude public function Latitude() { if ($this->getUpdateType() == "location") return $this->data["message"]["location"]["latitude"]; }

/// return longitude
public function Longitude()
{
    if ($this->getUpdateType() == "location")
        return $this->data["message"]["location"]["longitude"];
}`
marcelozao commented 6 years ago

Hum blank latitude and longitude, perhaps I have change getUpdateType() too?

Mohsen322 commented 6 years ago

maybe...

marcelozao commented 6 years ago

Ok, I did: $type = $telegram->getUpdateType(); $content = ['text' => 'Type: '.$type, 'parse_mode' => 'HTML','chat_id' => $chat_id]; $telegram->sendMessage($content);

and after send location or contact the response is: Type: reply

Why reply? Expected contact or location. Whats is wrong?

swim89 commented 5 years ago

@Mohsen322 with the same logic, is it possible to get the Live Location longitude/latitude?

jrdvm commented 3 years ago

Is it possible to send or how can I send 'contact phone' and 'contact location' values by InlineKeyboardButton?

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.