🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fb-bot

SDK wrapper to verifyRequest, send message, recieve with Facebook Messenger Bots.

1.0.3
latest
Version published
Weekly downloads
1
-80%
Maintainers
1
Weekly downloads
 
Created

fb-bot

SDK wrapper to verifyRequest, send message, recieve with Facebook Messenger Bots.

NPM

Install

$ npm install --save fb-bot

Initializing

This link initializing module parameters.


var express = require('express');
var facebookModule = require('fb-bot.js');
var app = express();
    
facebookModule.init({
  APP_SECRET: 'YOUR_APP_SECRET',
  VALIDATION_TOKEN: 'YOUR_VALIDATION_TOKEN',
  PAGE_ACCESS_TOKEN: 'YOUR_PAGE_ACCESS_TOKEN',
  SERVER_URL: 'YOUR_SERVER_UR'
});

app.use(bodyParser.json({ verify: facebookModule.verifyRequestSignature }));

Usage

 ### 2 important funtions
    facebookModule.sendTextMessage(_senderId, _message).then( ... )
    facebookModule.messageListener(data, callback)

Send an Echo message back to user

//Facebook message 
app.post('/webhook', facebookModule.parsePOST, function (req, res) {
  var dataList = req.afterParse ;
  dataList.forEach(function(data){
    facebookModule.messageListener(data, function(){
      facebookModule.sendTextMessage(data.senderId, data.message.text);
    });
  });
  res.sendStatus(200);
});

Send url of attachments back to user

//Facebook message 
app.post('/webhook', facebookModule.parsePOST, function (req, res) {
  var dataList = req.afterParse ;
  dataList.forEach(function(data){
    facebookModule.messageListener(data, function(){
      facebookModule.sendTextMessage(data.senderId, data.message.attachments[0].payload.url);
    });
  });
  res.sendStatus(200);
});

Two import routes

Important

  • GET /webhook
  • POST /webhook
  • /webhook is default url routes where facebook send verification to
##  Verification

app.get('/webhook', facebookModule.authGET, function (req, res, next) {
    res.status(200).send(req.query['hub.challenge']);
});

## Recieve Message

app.post('/webhook', facebookModule.parsePOST, function (req, res) {
  var dataList = req.afterParse ;
  dataList.forEach(function(data){
      // ....
  });
  res.sendStatus(200);
});

License

MIT

FAQs

Package last updated on 10 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts