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')(process.env.DASHBOT_API_KEY).facebook;

Log whenever your webhook is called

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

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) => {
dashbot.logOutgoing(requestData, response.body);
});

Example

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

Close Menu