app.js 2.35 KB
'use strict';
let express = require("express"),
    bodyParser= require("body-parser"),
    app = express(),
    config = require('config'),
    controller = require('./controller');

app.use(bodyParser.urlencoded({ extended : false}));
app.use(bodyParser.json());

let users = {};

app.listen(process.env.PORT || 8989, () => console.log('Example app listening on por 8989!'));

app.get('/', (req, res) => res.send('Hello World!'));


// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {

  // Your verify token, Should be a random string.
  let VERIFY_TOKEN = config.get('facebook.page.verify_token');

  // Parse the query params
  let mode = req.query['hub.mode'];
  let token = req.query['hub.verify_token'];
  let challenge = req.query['hub.challenge'];


  // Checks if a token and mode is in the query string of the request
  if(mode && token) {

    // Checks the mode and token sent is correcct
    if (mode === 'subscribe' && token === VERIFY_TOKEN) {

      // Responds with the challenge token from the request
      console.log('WEBHOOK_VERIFIED');
      res.status(200).send(challenge);

    } else {
      // Responds with '403 Forbidden' if verify tokens do not match
      res.sendStatus(403);
    }
  }
});

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

  let body = req.body;

  if (body.object === 'page') {

    // Iterates over each entry - there may be multiple if batched
    body.entry.forEach(function(entry) {

      // Gets the message. entry.messaging is an array, but
      // will only ever contain one message, so we get index 0
      let webhook_event = entry.messaging[0];
      console.log(webhook_event);

      // Get the sender PSID
      let sender_psid = webhook_event.sender.id;
      console.log('Sender PSID: ' + sender_psid);

      // Check if the event is a message or postback and
      // pass the event to the appropriate handler function
      if (webhook_event.message) {
        controller.handleMessage(sender_psid, webhook_event.message);
      } else if (webhook_event.postback) {
        controller.handlePostback(sender_psid, webhook_event.postback);
      }
    });
    // Returns a '200 OK' response to all requests
    res.status(200).send('EVENT_RECEIVED');
  } else {
    // Returns a '404 Not Found' if event is ninteliot from a page subscription
    res.sendStatus(404);
  }
});