K48 K48 - 8 months ago 55
C# Question

Receive location from user in MS bot framework via Facebook Messenger

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 + "\n\n" + activity.Attachments[0].ContentType + "\n\n" +
activity.Attachments[0].ContentUrl + "\n\n" +
, 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 a 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?

Answer Source

The location is actually in the Entities collection, rather than the attachments collection. Look there and you should find what you need.