fb-messenger
Facebook Messenger Platform NodeJS API Wrapper
Installation
Requires Node 8+
npm install fb-messenger --save
API
You must require fb-messenger and create an instance
const FBMessenger = require('fb-messenger')
const messenger = new FBMessenger({token, notificationType})
messenger.setToken(token)
messenger.setNotificationType(notificationType)
messenger.sendTextMessage({id, message, notificationType, token})
messenger.sendAudioMessage({id, url, notificationType, token})
messenger.sendVideoMessage({id, url, notificationType, token})
messenger.sendImageMessage({id, url, notificationType, token})
messenger.sendFileMessage({id, url, notificationType, token})
messenger.sendQuickRepliesMessage({id, attachment, quickReplies, notificationType, token})
messenger.sendButtonsMessage({id, message, buttons, notificationType, token})
messenger.sendGenericMessage({id, elements, notificationType, token})
messenger.sendListMessage({id, elements, buttons, top_element_type, notificationType, token})
messenger.sendMediaMessage({id, elements, notificationType, token})
messenger.sendOpenGraphMessage({id, elements, notificationType, token})
messenger.sendReceiptMessage({id, payload, notificationType, token})
messenger.sendAction({id, actionType, token})
messenger.sendMessage({id, data, notificationType, token})
messenger.getProfile({id, token})
messenger.setWelcomeMessage({pageId, message, token})
messenger.setGreetingText ({pageId, message, token})
messenger.setPersistentMenu ({pageId, menuItems, token})
messenger.setDomainWhitelist ({pageId, domains, token})
messenger.sendThreadSettingsMessage ({pageId, body, token})
Notification Types:
- REGULAR
- SILENT_PUSH
- NO_PUSH
Examples
Basic Example
const FBMessenger = require('fb-messenger')
const messenger = new FBMessenger({token: '<YOUR TOKEN>'})
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'})
console.log('Sent successfully')
} catch(e) {
console.error(e)
}
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>'})
License
MIT. Copyright © Diego Rodríguez Baquero