Search code examples
facebookfacebook-messenger

Facebook messenger api - Metadata on the message is not coming back in the response


I have following code to send text message with metadata. When user responds with text, metadata field on the message is empty. Is it a bug or Messenger api does not support this functionality?

function sendTextMessage(recipientId, messageText, metadata) {
  var messageData = {
    recipient: {
      id: recipientId
    },
    message: {
      text: messageText,
      metadata: metadata,
    }
  };

  callSendAPI(messageData);
}

function callSendAPI(messageData) {
  request({
    uri: 'https://graph.facebook.com/v2.6/me/messages',
    qs: { access_token: PAGE_ACCESS_TOKEN },
    method: 'POST',
    json: messageData
  }, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var recipientId = body.recipient_id;
      var messageId = body.message_id;

      if (messageId) {
        console.log("FBHook Successfully sent message with id %s to recipient %s",
          messageId, recipientId);
      } else {
        console.log("FBHook Successfully called Send API for recipient %s",
          recipientId);
      }
    } else {
      console.error(response.error);
    }
  });
}

Solution

  • This is not how it is expected to behave. The metadata field will be returned to webhook immediately if subscribed to the "message_echoes" field. This is meant for co-ordination between multiple apps linked to the page.

    From the changelog - https://developers.facebook.com/docs/messenger-platform/changelog

    "New field: metadata, passed from the Send API and sent to the message_echoes callback, to help interoperability betwen multiple bots."