nikmlnkr
nikmlnkr

Reputation: 105

sendMessage not defined in Facebook Instant Games Webhook

I've deployed a webhook to make Messenger bot run when a player exits my game. I'm trying to implement this: https://developers.facebook.com/docs/games/instant-games/getting-started/bot-setup/#step-3--respond-to-messaging-game-plays-webhooks

Everything else is working but I am getting an error when the script deploys to sendMessage. It fails saying:

2019-03-11T15:11:16.289939+00:00 app[web.1]: ReferenceError: sendMessage is not defined

Although I do know that this error is because sendMessage is not defined anywhere in the script but after going through all the documentation I am not sure what exactly to write in sendMessage for the script to work.

Here's my end point for webhook:

// Creates the endpoint for our webhook 
app.post('/webhook', (req, res) => {  

let body = req.body;
console.log("body post : "+JSON.stringify(body));

// Checks this is an event from a page subscription
if (body.object === 'page') {

  // Iterates over each entry - there may be multiple if batched
  body.entry.forEach(function(entry) {
    console.log(JSON.stringify(entry));

    // Gets the message. entry.messaging is an array, but 
    // will only ever contain one message, so we get index 0
    let event = entry.messaging[0];
      // Get the sender PSID
      let sender_psid = event.sender.id;
      console.log('Sender PSID: ' + sender_psid);

      if(event.game_play){
        var senderId = event.sender.id; // Messenger sender id
        var playerId = event.game_play.player_id; // Instant Games player id
        var contextId = event.game_play.context_id; 
        var payload = event.game_play.payload;
          sendMessage(
            senderId, 
            contextId, 
            'Congratulations on your victory!', 
            'Play Again'
          );
      } else if (event.postback) {
        handlePostback(sender_psid, event.postback);
      }
  });

  // Returns a '200 OK' response to all requests
  res.status(200).send('EVENT_RECEIVED');
} 
});

Please help

Upvotes: 0

Views: 224

Answers (1)

Chris Hawkins
Chris Hawkins

Reputation: 784

You call a method sendMessage that has no implementation:

sendMessage(
  senderId, 
  contextId, 
  'Congratulations on your victory!', 
  'Play Again'
);

You should ensure that you have an implementation for this method that uses the Messenger Send API.

Upvotes: 1

Related Questions