简体   繁体   中英

Microsoft Bot Framework Geolocation

How do I get a users geocoordinates with the Bot Framework? I know I have to use entities but am not sure how to actually get the coordinates and not define them.

Unfortunately, there's no automatic way of getting user's location because it will violate user privacy .

However, you can always request for user's location. Depends on the chat channel they're using, they might be able to send you their location (I tested on telegram )

As a starting point, you might need to read about

How to send location

Sample Code:

var reply = activity.CreateReply();
reply.ChannelData = new TelegramCustomMessage
{
    Method = "sendLocation",
    Parameters = new Parameters
    {
        ChatId = "your_chat_id",
        Latitute = 0,
        Longitute = 0
    }
};

public class TelegramCustomMessage
{
    [JsonProperty(PropertyName = "method")]
    public string Method { get; set; }
    [JsonProperty(PropertyName = "parameters")]
    public Parameters Parameters { get; set; }
}

public class Parameters
{
    [JsonProperty(PropertyName = "chat_id")]
    public string ChatId { get; set; }
    [JsonProperty(PropertyName = "latitute")]
    public float Latitute { get; set; }
    [JsonProperty(PropertyName = "longitute")]
    public float Longitute { get; set; }
}

https://docs.botframework.com/en-us/csharp/builder/sdkreference/channels.html#customtelegrammessages

https://core.telegram.org/bots/api#sendlocation

How to extract geolocation from activity

Sample Code:

if (entity.Type == "Place")
{
    Place place = entity.GetAs<Place>();
    GeoCoordinates geoCoord = place.Geo.ToObject<GeoCoordinates>();
    // geoCoord object will contains Longtitude & Latitude
}  

https://docs.botframework.com/en-us/csharp/builder/sdkreference/activities.html#places

The team just added support to the Framework for location services. Super helpful: https://github.com/Microsoft/BotBuilder-Location

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM