2

I am building a chatbot using Microsoft bot framework. I am stuck on how to get latitude and longitude values from "activity".

Is there a way to get latitude and longitude info from "activity"?

There is a similar question exists already here but that does not give an answer to this question.

I have tried Microsoft..Bot.Builder.Location and that does not give lat and long of the user but helps in getting info from the user and validate it.

Any suggestion would be appreciated.

Dheeraj Palagiri
  • 1,829
  • 3
  • 23
  • 46

2 Answers2

3

You can use this free REST API to get the Latitude and Longitude of the place, this REST API works on Wi-Fi Traingulation.

REST API for getting Latitude and Longitude of a place

Make a model class corresponding to the output of the REST API used for deserialization:

 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; }
        }

Now, simply make a GET request from your C# code in Bot Framework and get result like this:

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;
                }

            }
        }
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
1

For Messenger on mobile, the user can specifically send their location using the built-in functionality. Once they do that, you can receive the values from the incoming message:

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();
}
stuartd
  • 70,509
  • 14
  • 132
  • 163