【问题标题】:How to get ChatBot user Latitude and Longitude using BotFramework如何使用 BotFramework 获取 ChatBot 用户纬度和经度
【发布时间】:2017-10-25 19:46:31
【问题描述】:
【问题讨论】:
标签:
geolocation
botframework
【解决方案1】:
您可以使用这个免费的 REST API 来获取该地点的纬度和经度,这个 REST API 适用于 Wi-Fi Traingulation。
REST API for getting Latitude and Longitude of a place
制作一个与用于反序列化的 REST API 的输出相对应的模型类:
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;
}
}
}
【解决方案2】:
对于移动版 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();
}