/node-fb-messenger

:envelope: Facebook Messenger Platform NodeJS API Wrapper

Primary LanguageJavaScriptMIT LicenseMIT

node-fb-messenger npm npm js-standard-style npm

Facebook Messenger Platform NodeJS API Wrapper

bitHound Overall Score Codacy Badge Code Climate

Installation

npm install fb-messenger --save

API

You must require fb-messenger and create an instance

// Constructor
var FBMessenger = require('fb-messenger')
var messenger = new FBMessenger(token[, notificationType])

// Functions
messenger.sendTextMessage(id, message[, notificationType][, cb]) // Sends a text message

messenger.sendImageMessage(id, imageURL[, notificationType][, cb]) // Sends an image from URL

messenger.sendHScrollMessage(id, elements[, notificationType][, cb]) // Sends an H-SCroll generic message

messenger.sendButtonsMessage(id, message, buttons[, notificationType][, cb]) // Sends a buttons message

messenger.sendReceiptMessage(id, payload[, notificationType][, cb]) // Sends a receipt message (No need for template_type in payload) 

messenger.sendQuickRepliesMessage(id, attachment, quickReplies[, notificationType][, cb]) // Sends a Quick Replies Message

messenger.sendMessage(id, messageData[, notificationType][, cb]) // Send a message from custom data

messenger.getProfile(id, cb) // Gets user information

messenger.setWelcomeMessage(pageId, message[, cb]) // Sets Page's Welcome Message (message can be a text string or a strucuted message)

messenger.setGreetingText (pageId, message[, cb]) // Sets Page's Greeting Text

messenger.setPersistentMenu (pageId, menuItems[, cb]) // Set's Page's Persistent Meny

messenger.sendThreadSettingsMessage (pageId, jsonObject[, cb]) // Send Manually Page's Thread Settings

Notification Types:

  • REGULAR
  • SILENT_PUSH
  • NO_PUSH

Examples

Basic Example

var FBMessenger = require('fb-messenger')
var messenger = new FBMessenger(<YOUR TOKEN>)

messenger.sendTextMessage(<ID>, 'Hello')

Callback Example

var FBMessenger = require('fb-messenger')
var messenger = new FBMessenger(<YOUR TOKEN>)

messenger.sendTextMessage(<ID>, 'Hello', function (err, body) {
  if(err) return console.error(err)
  console.log(body)
})

No push Example

var FBMessenger = require('fb-messenger')
var messenger = new FBMessenger(<YOUR TOKEN>)

messenger.sendTextMessage(<ID>, 'Hello', 'NO_PUSH')

Default to silent push Example

var FBMessenger = require('fb-messenger')
var messenger = new FBMessenger(<YOUR TOKEN>, 'SILENT_PUSH')

messenger.sendTextMessage(<ID>, 'Hello')

Complete Example

var FBMessenger = require('fb-messenger')
var messenger = new FBMessenger(<YOUR TOKEN>, 'NO_PUSH')

messenger.sendTextMessage(<ID>, 'Hello') // Send a message with NO_PUSH, no callback

// Send an image overriding default notification type with callback
messenger.sendImageMessage(<ID>, <IMG URL>, 'REGULAR', function (err, body) {
  if (err) return console.error(err)
  console.log('Image sent successfully')
})

License

MIT. Copyright (c) Diego RodrĂ­guez Baquero.