• Stars
    star
    211
  • Rank 186,867 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

✉️ Facebook Messenger Platform Node.js API Wrapper

fb-messenger npm npm npm

Facebook Messenger Platform NodeJS API Wrapper

js-standard-style Greenkeeper badge Codacy Badge Code Climate

Installation

Requires Node 8+

npm install fb-messenger --save

API

You must require fb-messenger and create an instance

// Constructor
const FBMessenger = require('fb-messenger')
const messenger = new FBMessenger({token, notificationType})
// token is optional, if not included, must be sent in each method, notificationType is optional, default = 'REGULAR'

messenger.setToken(token) // Sets the instance token

messenger.setNotificationType(notificationType) // Sets the instance notificationType

// Methods (notificationType and token are optional)
messenger.sendTextMessage({id, message, notificationType, token}) // Sends a text message

messenger.sendAudioMessage({id, url, notificationType, token}) // Sends an audio from URL

messenger.sendVideoMessage({id, url, notificationType, token}) // Sends an video from URL

messenger.sendImageMessage({id, url, notificationType, token}) // Sends an image from URL

messenger.sendFileMessage({id, url, notificationType, token}) // Sends an file from URL

messenger.sendQuickRepliesMessage({id, attachment, quickReplies, notificationType, token}) // Sends a Quick Replies Message

messenger.sendButtonsMessage({id, message, buttons, notificationType, token}) // Sends a buttons template message

messenger.sendGenericMessage({id, elements, notificationType, token}) // Sends a generic template message

messenger.sendListMessage({id, elements, buttons, top_element_type, notificationType, token}) // Sends a list template message

messenger.sendMediaMessage({id, elements, notificationType, token}) // Sends a media template message

messenger.sendOpenGraphMessage({id, elements, notificationType, token}) // Sends an open graph template message

messenger.sendReceiptMessage({id, payload, notificationType, token}) // Sends a receipt template message (No need for template_type in payload) 

messenger.sendAction({id, actionType, token}) // Send an action type (One of 'mark_seen', 'typing_on', 'typing_off')

messenger.sendMessage({id, data, notificationType, token}) // Send a message from custom data

messenger.getProfile({id, token}) // Gets user information

messenger.setWelcomeMessage({pageId, message, token}) // Sets Page's Welcome Message (message can be a text string or a strucuted message)

messenger.setGreetingText ({pageId, message, token}) // Sets Page's Greeting Text

messenger.setPersistentMenu ({pageId, menuItems, token}) // Set's Page's Persistent Menu

messenger.setDomainWhitelist ({pageId, domains, token}) // Set's Page's Whitelisted Domains 

messenger.sendThreadSettingsMessage ({pageId, body, token}) // Send Manually Page's Thread Settings

Notification Types:

  • REGULAR
  • SILENT_PUSH
  • NO_PUSH

Examples

Basic Example

const FBMessenger = require('fb-messenger')
const messenger = new FBMessenger({token: '<YOUR TOKEN>'}) // Will always use this page's token for request unless sent on each method

messenger.sendTextMessage({id: '<ID>', text: 'Hello'})

Catch errors Example

const FBMessenger = require('fb-messenger')
const messenger = new FBMessenger({token: '<YOUR TOKEN>'})

try {
  const response = await messenger.sendTextMessage({id: '<ID>', text: 'Hello'})
  console.log(response)
} catch (e) {
  console.error(e)
}

No push Example

const FBMessenger = require('fb-messenger')
const messenger = new FBMessenger({token: '<YOUR TOKEN>'})

messenger.sendTextMessage({id: '<ID>', text: 'Hello', notificationType: 'NO_PUSH'})

Default to silent push Example

const FBMessenger = require('fb-messenger')
const messenger = new FBMessenger({token: '<YOUR TOKEN>', notificationType: 'SILENT_PUSH'})

Complete Example

const FBMessenger = require('fb-messenger')
const messenger = new FBMessenger({token: '<YOUR TOKEN>', notificationType: 'NO_PUSH'})

try {
  await messenger.sendTextMessage({id: '<ID>', text: 'Hello'}) // Send a message with NO_PUSH, ignoring response
  console.log('Sent successfully')
} catch(e) {
  console.error(e)
}

// Send an image overriding default notification type with callback
try {
  const response = await messenger.sendImageMessage({id: '<ID>', url: '<IMG URL>', notificationType: 'REGULAR'})
  console.log('Sent image, response:')
  console.dir(response)
} catch(e) {
  console.error(e)
}

messenger.sendTextMessage({id: '<ID>', text: 'Hello', token: '<YOUR OTHER TOKEN>'}) // Send message on another page

License

MIT. Copyright © Diego Rodríguez Baquero

More Repositories

1

BTorrent

🌐 Fully-featured WebTorrent Client
HTML
544
star
2

awesome-webtorrent-clones

👥 Product clones using WebTorrent/WebRTC
198
star
3

PeerFast

⚡️ P2P Fast.com Clone
HTML
100
star
4

uWebTorrentTracker

🔆 Simple, robust, WebTorrent tracker server implementation
JavaScript
64
star
5

node-v

🔒 Secure ❄️ Synchronized ⚡️ Realtime ☁️ Cloud 🌈 Native JavaScript Variables & Events
JavaScript
28
star
6

node-os

NodeJS Core Module
JavaScript
20
star
7

PeerTube

YouTube Clone using WebTorrent
JavaScript
17
star
8

node-bogota

🚀 Run tape tests concurrently with tap-spec output
JavaScript
15
star
9

node-1337

Node leet speak conversion
JavaScript
12
star
10

node-vm

NodeJS Core Module
JavaScript
11
star
11

uniandes-auto-wifi

Aplicación para conectarse automáticamente a SENECA
JavaScript
9
star
12

apk-decompiler

Decompiles APK to dex, smali and java files
Shell
6
star
13

ls-chunk-store

Browser localStorage chunk store that is abstract-chunk-store compliant
JavaScript
5
star
14

node-sr

Unblock your event loop! Require modules in a child process effortlessly
JavaScript
4
star
15

geostatistics-project

Geostatistics Project using Goverment's Open Data Personal Robberies in Bogotá 2017
HTML
4
star
16

BMagnet

An extension to the magnet URI scheme for WebTorrent
JavaScript
3
star
17

sequelize-express-findbyid

Find by id using a sequelize model, returning 404 error if not found
JavaScript
2
star
18

node-dinli

Wrapper for the Dinli URL Shortener
JavaScript
2
star
19

express-debug-async-wrap

express async wrapper that passes custom debug instance
JavaScript
2
star
20

express-route-autoloader

Automatically loads routes from a directory structure
JavaScript
2
star
21

node-colombia

Colombia como módulo JS
2
star
22

express-sequelize-crud-router

express CRUD router based on a sequelize model
JavaScript
2
star
23

lf-chunk-store

Browser localForage chunk store that is abstract-chunk-store compliant
JavaScript
2
star
24

taller6-pruebas-automaticas

JavaScript
1
star
25

taller7-pruebas-automaticas

Java
1
star