Facebook Integration with NPM

Create a bot API key

Each bot needs its own API key for tracking.

Create a bot to get an API key.

Install Dashbot via NPM

npm install --save dashbot

Include Dashbot

Use the API key created above.

const dashbot = require('dashbot')(<strong>process.env.DASHBOT_API_KEY</strong>).facebook;

Log whenever your webhook is called

app.use(bodyParser.json())
...
app.post('/guessnumber', (request, response) => {
  <strong>dashbot.logIncoming(request.body);</strong>
  ...
})

Whenever you send a message, log the request and response

const requestData = {
  url: 'https://graph.facebook.com/v2.6/me/messages',
  qs: {access_token: process.env.FACEBOOK_PAGE_TOKEN},
  method: 'POST',
  json: {
    recipient: {id: sender},
    message: {
      text: 'You are right when you say: ' + text
    }
  }
};
request(requestData, (error, response, body) => {
  <strong>dashbot.logOutgoing(requestData, response.body);</strong>
});

Example

View sample code for a Facebook Messenger Bot, with Dashbot analytics already integrated.