I am trying to use MS bot framework connected to Facebook Messenger to create a simple demo. The demo would have a predefined list of company branches (e.g. coordinates) and when the user would send their location to the bot (using the “Share Location” function in FB messenger), it would call Google Maps API to measure distance to all branches and find the closest one.
However, I cannot acquire the location sent by the user as an attachment:
//if there is an attachment, assume it's a location
//and print out the attachments count and other info;
if (activity.Attachments != null)
{
int attachmentsCount = activity.Attachments.Count;
await SendReplyToUser(activity.Attachments.Count + " attachments.", activity);
if (attachmentsCount > 0)
{
await SendReplyToUser("I got your attachment! (" + attachmentsCount + ") " +
activity.Attachments[0].Content + "nn" + activity.Attachments[0].ContentType + "nn" +
activity.Attachments[0].ContentUrl + "nn" +
activity.Attachments[0].Name
, activity);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
//otherwise continue processing the user's text message
However, the attachments count turns out to be zero when I “Share location”. If I send anything else, like a picture, then the bot successfully receives it, but not the location. I can still figure out that the user did send an attachment (otherwise activity.Attachments
is null, if it’s just a text message), but the attachment count is zero, and I don’t find anything in activity.Attachments[0]
.
Am I doing something wrong or is the location-containing attachment deliberately blocked by Facebook or MS bot framework?
2
Answers
The location is actually in the Entities collection, rather than the attachments collection. Look there and you should find what you need.
Found it: the location from facebook can be received through activity.ChannelData.
The Activity object’s ChannelData property is just a JSON string that can be parsed; if the user sends a location, it will contain longitude, latitude, place name and a link to Bing Maps that can be used to view the location.
Here is a code example:
https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Samples/EchoBot/EchoLocationDialog.cs#L110