如何在电报机器人中显示接近的位置?

时间:2017-05-29 14:37:11

标签: php mysql location bots telegram

我用电报创建机器人显示在餐馆附近我的机器人必须按下第一个按钮插入地理坐标餐厅和我的服务器recive lat和长并保存它我创建第一个按钮和良好的工作 但我不能创建第二个按钮显示在餐厅附近这是我的代码 请帮我 谢谢

<?php
//$json=file_get_contents('php://input');
$update = json_decode(file_get_contents('php://input'));

//main input
$message    = $update->message->text;
$chatid     = $update->message->chat->id;
$last_name  = $update->message->from->last_name;
$first_name = $update->message->from->first_name;
$usernameid = $update->message->from->username;
$latitude   = $update->message->location->latitude;
$longitude  = $update->message->location->longitude;

$text1 = "insert location";
$text2 = "show near restaurant";

$token = '';

$servername = "localhost";
$dbname     = "";
$username   = "";
$password   = "";

//$rep=json_decode(file_get_contents("https://api.telegram.org/bot".$token."/SendMessage?chat_id=".$chatid."&text=".urldecode($json)));

$reply_markup = json_encode(["keyboard"        =>
                                 [
                                     [["text" => $text1, "request_location" => true]],
                                     //[["text"=>$text1]],
                                     [["text" => $text2]],

                                 ],
                             "resize_keyboard" => true]);

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_query($conn, 'set names "utf8"');

// Check connection
if ($conn->connect_error) {
    die("Connection failed: ".$conn->connect_error);
}
echo "Connected successfully";

date_default_timezone_set("Asia/Tehran");
$time = date("h:i:sa");

$date = date("Y/m/d");

if ($message == "/start") {

    $starttxt = "hello".$first_name." ".$last_name."how are you?"."✌️✌️✌️";
    $rep      = json_decode(file_get_contents("https://api.telegram.org/bot".$token."/SendMessage?chat_id=".$chatid."&reply_markup=".$reply_markup."&text=".urldecode($starttxt)));

    $sql    = "INSERT INTO user_info (date,time,T_id,T_firstname,T_lastname,T_username) VALUES ('$date','$time','$chatid','$first_name','$last_name','$usernameid')";
    $result = $conn->query($sql);
}

if ($longitude != null) {

    $rep = json_decode(file_get_contents("https://api.telegram.org/bot".$token."/SendMessage?chat_id=".$chatid."&reply_markup=".$reply_markup."&text=".urldecode("long is: ".$longitude." lat is: ".$latitude)));

    $sql    = "INSERT INTO insert_location (T_id,T_username,date,time,latitude,longitude) VALUES ('$chatid','$usernameid','$date','$time','$latitude','$longitude')";
    $result = $conn->query($sql);
}

if ($message == "show near restaurant") {

    $rep = json_decode(file_get_contents("https://api.telegram.org/bot".$token."/SendMessage?chat_id=".$chatid."&reply_markup=".$reply_markup."&text=".urldecode($starttxt)));

    $sql = SELECT id, (3959 * acos(cos(radians(37)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-122)) + sin(radians(37)) * sin(radians(latitude)))) AS distance FROM insert_location HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

    $result = $conn->query($sql);
}

?>

当用户点击第一个按钮时,此图像形成mysql

and this image form mysql when user click on first button

2 个答案:

答案 0 :(得分:0)

您的意思是sendVenue吗?

顺便说一句,我建议将/setinlinegeoVenue Result一起使用,它可以让用户从最多50个结果中进行选择。

答案 1 :(得分:0)

我在google maps api v_3中找到了这个问题的答案,用于显示附近的地方

SELECT id, latitude, longitude, ( 6371 * acos ( cos ( radians($latitude) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($longitude) ) + sin ( radians($latitude) ) * sin( radians( latitude ) ) ) ) AS distance FROM insert_location HAVING distance < 3;

6371 for kmometer