James Hennessy
James Hennessy

Reputation: 507

Send Location in Facebook Messenger

Trying to send location to the user from my bot? Is that allowed? If it is , what is wrong with my code I have the JSON format correct and I get the

error:  { message: '(#100) Unsupported attachment type',
    type: 'OAuthException', 
    code: 100,
    error_subcode: 2018046,
    fbtrace_id: 'CObB3+0fgMw' }

If anybody could provide context it would be great

function sendLocationMessage(sender,event){
    let messageData={
         attachment: {
            "type": "location",
            "payload": {
              "coordinates":{
                 "lat": event.message.attachments[0].payload.coordinates.lat,
                 "long": event.message.attachments[0].payload.coordinates.long
    }    }
}
    }
     request({
        url: 'https://graph.facebook.com/v2.6/me/messages',
        qs: {access_token:token},
        method: 'POST',
        json: {
            recipient: {id:sender},
            message: messageData,
        }
    }, function(error, response, body) {
        if (error) {
            console.log('Error sending messages: ', error)
        } else if (response.body.error) {
            console.log('Error: ', response.body.error)
        }
    })
}

Upvotes: 2

Views: 3795

Answers (2)

María Teresa
María Teresa

Reputation: 37

In addition if you want to add some functionality by opening the image when clicking the card, you can add default action. And in the url, call the api with escape characters (%7C)

    messageData = {
        "attachment": {
          "type": "template",
          "payload": {
            "template_type": "generic",
            "elements": [{
              "title": 'Location Shared By Bot',
              "subtitle": "Location Subtitle",
              "image_url": "https://maps.googleapis.com/maps/api/staticmap?key=YOUR_GMAPS_TOKEN&markers=color:red|label:B|YOUR_LATITUDE,YOUR_LONGITUDE&size=360x360&zoom=13",
              "default_action": {
                  "type": "web_url",
                  "url": "https://maps.googleapis.com/maps/api/staticmap?key=YOUR_GMAPS_TOKEN&markers=color:red%7Clabel:A%7YOUR_LATITUDE,YOUR_LONGITUDE&size=360x360&zoom=13",
                  "messenger_extensions": true,
                  "webview_height_ratio": "tall"
                }
            }]
          }
        }

Upvotes: 1

Sri
Sri

Reputation: 1336

No. The Bot cannot share its location with attachment type as "type": "location",. The error message calls it out clearly Unsupported attachment type.

Instead, one of the options you can do is, share a link of Google Maps (static).

messageData = { "attachment": { "type": "template", "payload": { "template_type": "generic", "elements": [{ "title": 'Location Shared By Bot', "subtitle": "Location Subtitle", "image_url": https://maps.googleapis.com/maps/api/staticmap?key= + "YOUR_GMAPS_TOKEN" + "&markers=color:red|label:B|" + lat + "," + long + "&size=360x360&zoom=13" }] } }

Reference : Messenger send API ref - https://developers.facebook.com/docs/messenger-platform/send-api-reference

Thanks, Sriram

Upvotes: 5

Related Questions