【问题标题】:How to get ChatBot user Latitude and Longitude using BotFramework如何使用 BotFramework 获取 ChatBot 用户纬度和经度
【发布时间】:2017-10-25 19:46:31
【问题描述】:

我正在使用 Microsoft 机器人框架构建聊天机器人。我被困在如何从“活动”中获取纬度和经度值。

有没有办法从“活动”中获取经纬度信息?

这里已经存在一个similar question,但这并不能回答这个问题。

我已经尝试过Microsoft..Bot.Builder.Location,这并没有给出用户的经纬度,但有助于从用户那里获取信息并对其进行验证。

任何建议都将不胜感激。

【问题讨论】:

  • 你会使用哪个频道?
  • Bot框架工作模拟器和facebook
  • 你找到获取用户当前位置的方法了吗?

标签: 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多