【问题标题】:How to show nearing place in telegram bot?如何在电报机器人中显示附近的地方?
【发布时间】:2017-05-29 14:37:10
【问题描述】:

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

<?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

【问题讨论】:

    标签: php mysql location bots telegram


    【解决方案1】:

    你的意思是sendVenue

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

    【讨论】:

    • 感谢您的建议,但我不喜欢使用 /setinlinegeo 我喜欢从用户那里接收位置并发送结果,范围为 2 公里
    • @MasihKolahdouzan 我知道内联地理并不适合所有情况,所以请尝试有关 sendVenue 的第一个解决方案。
    【解决方案2】:

    我在 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 表示公里

    【讨论】:

      猜你喜欢
      • 2016-03-31
      • 2017-08-18
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 2019-09-16
      相关资源
      最近更新 更多