如何使用 BotFramework 获取 ChatBot 用户的经纬度
How to get ChatBot user Latitude and Longitude using BotFramework
我正在使用 Microsoft 机器人框架构建聊天机器人。我被困在如何从 "activity" 获取纬度和经度值上。
有没有办法从 "activity" 获取纬度和经度信息?
这里已经有一个 similar question,但它没有给出这个问题的答案。
我试过 Microsoft..Bot.Builder.Location 并没有给出用户的纬度和经度,但有助于从用户那里获取信息并进行验证。
如有任何建议,我们将不胜感激。
对于移动版 Messenger,用户可以使用内置功能专门发送他们的位置。一旦他们这样做了,您就可以从传入的消息中接收值:
var location = activity.Entities?.FirstOrDefault(e => e.Type == "Place");
if (location != null)
{
var latitude = location.Properties["geo"]?["latitude"]?.ToString();
var longitude = location.Properties["geo"]?["longitude"]?.ToString();
}
您可以使用这个免费的 REST API 获取该地点的纬度和经度,这个 REST API 适用于 Wi-Fi 训练。
REST API for getting Latitude and Longitude of a place
制作一个模型class对应于用于反序列化的RESTAPI的输出:
public class PlaceGeography
{
public string ip { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public string region_code { get; set; }
public string region_name { get; set; }
public string city { get; set; }
public string zip_code { get; set; }
public string time_zone { get; set; }
public float latitude { get; set; }
public float longitude { get; set; }
public int metro_code { get; set; }
}
现在,只需从 Bot Framework 中的 C# 代码发出 GET 请求,即可获得如下结果:
public async static Task<HttpResponseMessage> GetCoOrdinates()
{
string responseJSON = string.Empty;
PlaceGeography obj = new PlaceGeography();
using (HttpClient client = new HttpClient())
{
string URI = $"http://freegeoip.net/json/";
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (HttpResponseMessage msg = await client.GetAsync(URI))
{
if (msg.IsSuccessStatusCode) //if HTTP 200 then
{
responseJSON = await msg.Content.ReadAsStringAsync();
JsonConvert.PopulateObject(responseJSON, obj); // Deserialize model class object and get latitude and longitude
return msg;
}
else
return msg;
}
}
}
我正在使用 Microsoft 机器人框架构建聊天机器人。我被困在如何从 "activity" 获取纬度和经度值上。
有没有办法从 "activity" 获取纬度和经度信息?
这里已经有一个 similar question,但它没有给出这个问题的答案。
我试过 Microsoft..Bot.Builder.Location 并没有给出用户的纬度和经度,但有助于从用户那里获取信息并进行验证。
如有任何建议,我们将不胜感激。
对于移动版 Messenger,用户可以使用内置功能专门发送他们的位置。一旦他们这样做了,您就可以从传入的消息中接收值:
var location = activity.Entities?.FirstOrDefault(e => e.Type == "Place");
if (location != null)
{
var latitude = location.Properties["geo"]?["latitude"]?.ToString();
var longitude = location.Properties["geo"]?["longitude"]?.ToString();
}
您可以使用这个免费的 REST API 获取该地点的纬度和经度,这个 REST API 适用于 Wi-Fi 训练。
REST API for getting Latitude and Longitude of a place
制作一个模型class对应于用于反序列化的RESTAPI的输出:
public class PlaceGeography
{
public string ip { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public string region_code { get; set; }
public string region_name { get; set; }
public string city { get; set; }
public string zip_code { get; set; }
public string time_zone { get; set; }
public float latitude { get; set; }
public float longitude { get; set; }
public int metro_code { get; set; }
}
现在,只需从 Bot Framework 中的 C# 代码发出 GET 请求,即可获得如下结果:
public async static Task<HttpResponseMessage> GetCoOrdinates()
{
string responseJSON = string.Empty;
PlaceGeography obj = new PlaceGeography();
using (HttpClient client = new HttpClient())
{
string URI = $"http://freegeoip.net/json/";
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (HttpResponseMessage msg = await client.GetAsync(URI))
{
if (msg.IsSuccessStatusCode) //if HTTP 200 then
{
responseJSON = await msg.Content.ReadAsStringAsync();
JsonConvert.PopulateObject(responseJSON, obj); // Deserialize model class object and get latitude and longitude
return msg;
}
else
return msg;
}
}
}