11

There is a way to send my location to facebook-messenger from a mobile client, but how can I send some location from a bot? (messenger platform api)

When I try to send a similar structure from bot, I get an error: (#100) Unsupported attachment type

Is there a way to send my location from bot?

Example of received message to bot:

{
   "object": "page",
   "entry": [{
       "id": "1719442148306048",
       "time": 1466780344978,
       "messaging": [{
           "sender": {"id": "123456789"},
           "recipient": {"id": "987654321"},
           "timestamp": 1466780344847,
           "message": {
               "mid": "mid.12345698875:c80066d69b6cee1779",
               "seq": 65,
               "attachments": [{
                   "title": "Dmitry's Location",
                   "url": "Link to bing.com through facebook redirect"
                   "type": "location",
                   "payload": {"coordinates": {"lat": 55, "long": 37}}
               }]
           }
       }]
   }]
}

I try send message with attachment like this:

               "attachment": {
                   "type": "location",
                   "payload": {"coordinates": {"lat": 55, "long": 37}}
               }
Dmitry
  • 2,057
  • 1
  • 18
  • 34
  • 1
    Can you show us a [mcve] that represents the way you're sending the message with the location data intact? – gravity Jun 24 '16 at 16:02
  • 1
    Yes. I edited question. – Dmitry Jun 24 '16 at 16:09
  • `"attachment"` is an array within the JSON. Notice the `[` and `]` encapsulating all of the data (`type`, and `payload`)? Try adding the `[]` brackets to indicate the start and end of `attachment` and let us know if that solved it? – gravity Jun 24 '16 at 16:13
  • 2
    @gravity adding braces in it not working, can't we send location as messenger send to us without explicitly open it as describing below, i've also checked in the facebook docs but they don't have any location type attachment https://developers.facebook.com/docs/messenger-platform/send-api-reference – Rishabh Agrawal Jul 14 '16 at 19:09

2 Answers2

25

There is workaround. We can send generic template with static map image and url to dynamic. For iOS native map app we can use address http://maps.apple.com/maps (which redirects all non iOS users to Google Maps with the same parameters). On Android it opens Google Maps app.

{
    "recipient": {"id": "132456"},
    "message": {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": {
                    "element": {
                        "title": "Your current location",
                        "image_url": "https:\/\/maps.googleapis.com\/maps\/api\/staticmap?size=764x400&center="+lat+","+long+"&zoom=25&markers="+lat+","+long,
                        "item_url": "http:\/\/maps.apple.com\/maps?q="+lat+","+long+"&z=16"
                    }
                }
            }
        }
    }
}
Ulas Keles
  • 1,681
  • 16
  • 20
Dmitry
  • 2,057
  • 1
  • 18
  • 34
4

Dmitry, thanks for the hack! FB API has slightly changed, here's what worked for me in the payload section:

    payload = dict()
    payload['type'] = 'template'
    payload['text'] = dict(
        template_type="generic",
        elements=[
            dict(
                title='{venue} location',
                # subtitle='Test',
                image_url="https://maps.googleapis.com/maps/api/staticmap?size=764x400&center=" + lat + "," + long +
                          "&zoom=15&markers=" + lat + "," + long,  
                default_action=dict(
                    type="web_url",
                    url="http://maps.apple.com/maps?q=" + venue + "&ll=" + lat + "," + long +
                        "&z=15"
                )
            )  # buttons=[])
        ]
    )
  • thanks.. for those looking for Java examples, there is a open source java sdk for facebook api's, which is named as restfb. And within restfb, you can use GenericItemPayload object, which can contain Bubbles. And each Bubble can contain- image_url, item_url and title. – a3.14_Infinity Oct 30 '17 at 05:42
  • That request body is the `generic templates`'s. Does it prompt messenger to open `google maps` for you? I've tried using the `url button` request body but it opens google maps in the `webview` rather that on the native app. – n1nsa1d00 Apr 08 '18 at 22:51