Friday, 17 March 2017

Facebook Messenger API: Post request returning error 500

  1. my post webhook:

    router.post('/webhook', function (req, res) { var data = req.body;

          // Make sure this is a page subscription
          if (data.object == 'page') {
            // Iterate
    
     over each entry
    
     // There may be multiple if batched
        data.entry.forEach(function(pageEntry) {
          var pageID = pageEntry.id;
          var timeOfEvent = pageEntry.time;
          // Iterate over each messaging event
          pageEntry.messaging.forEach(function(messagingEvent) {
            if (messagingEvent.optin) {
              receivedAuthentication(messagingEvent);
            } else if (messagingEvent.message) {
              receivedMessage(messagingEvent);
            } else if (messagingEvent.delivery) {
              receivedDeliveryConfirmation(messagingEvent);
            } else if (messagingEvent.postback) {
              receivedPostback(messagingEvent);
            } else if (messagingEvent.read) {
              receivedMessageRead(messagingEvent);
            } else if (messagingEvent.account_linking) {
              receivedAccountLink(messagingEvent);
            } else {
              console.log("Webhook received unknown messagingEvent: ", messagingEvent);
            }
          });
        });
    
        // Assume all went well.
        //
        // You must send back a 200, within 20 seconds, to let us know
        // you've successfully received the callback. Otherwise, the request
        // will time out and we will keep trying to resend.
        res.sendStatus(200);
      }
    });
    
    

2.my receivedMessage function:

function receivedMessage(event) {
  var senderID = event.sender.id;
  var recipientID = event.recipient.id;
  var timeOfMessage = event.timestamp;
  var message = event.message;

  console.log("Received message for user %d and page %d at %d with message:", 
    senderID, recipientID, timeOfMessage);
  console.log(JSON.stringify(message));

  var messageId = message.mid;

  var messageText = message.text;
  var messageAttachments = message.attachments;

  if (messageText) {

    // If we receive a text message, check to see if it matches a keyword
    // and send back the example. Otherwise, just echo the text we received.
    switch (messageText) {
      case 'generic':
        sendGenericMessage(senderID);
        break;

      default:
        sendTextMessage(senderID, messageText);
    }
  } else if (messageAttachments) {
    sendTextMessage(senderID, "Message with attachment received");
  }
}

this is the server log when i run my code with out sendTextMessage which calls the callsend api where i think my code fails:

2017-03-17T10:06:11.296017+00:00 app[web.1]: Received message for user 1367047720019479 and page 1334554393257327 at 1489745171043 with message: 2017-03-17T10:06:11.298010+00:00 app[web.1]: {"mid":"mid.$cAAS9xUXoM5lhDQCaY1a27tcMAHJS","seq":157393,"text":"testing message"} 2017-03-17T10:06:11.298044+00:00 app[web.1]: {"recipient":{"id":"1367047720019479"},"message":{"text":"testing message"}} 2017-03-17T10:06:11.298649+00:00 app[web.1]: POST /webhook/ 200 3.244 ms - 2

now when i include sendtext and callSendAPI:

function sendTextMessage(recipientId, messageText) {
  var messageData = {
    recipient: {
      id: recipientId
    },
    message: {
      text: messageText
    }
  };
console.log(JSON.stringify(messageData));

callSendAPI(messageData);

}


function callSendAPI(messageData) {

 ***console.log("inside the call api");***
request.post({
    uri: 'https://graph.facebook.com/v2.6/me/messages',
    qs: { access_token: access },
    //method: 'POST',
    json: messageData

  }, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var recipientId = body.recipient_id;
      var messageId = body.message_id;

      console.log("Successfully sent generic message with id %s to recipient %s", 
        messageId, recipientId);
    } else {
      console.error("Unable to send message.");
      console.error(response);
      console.error(error);
    }
  });  
}

this is the output it gives :

2017-03-17T10:09:38.141702+00:00 app[web.1]: Received message for user 1367047720019479 and page 1334554393257327 at 1489745373029 with message: 2017-03-17T10:09:38.141719+00:00 app[web.1]: {"mid":"mid.$cAAS9xUXoM5lhDQOvZVa275xBT7LW","seq":157396,"text":"testing message"} 2017-03-17T10:09:38.141757+00:00 app[web.1]: {"recipient":{"id":"1367047720019479"},"message":{"text":"testing message"}} 2017-03-17T10:09:38.141778+00:00 app[web.1]: inside the call api 2017-03-17T10:09:38.143120+00:00 app[web.1]: POST /webhook/ 500 1.686 ms - 253

please help out how can i fix this error: all tokens set correctly page access and verify token.i am new to the messenger platform



via B.I.G.D

No comments:

Post a Comment