项目作者: DiegoRBaquero

项目描述 :
:envelope: Facebook Messenger Platform Node.js API Wrapper
高级语言: JavaScript
项目地址: git://github.com/DiegoRBaquero/node-fb-messenger.git
创建时间: 2016-04-14T00:39:43Z
项目社区:https://github.com/DiegoRBaquero/node-fb-messenger

开源协议:MIT License

下载


fb-messenger npm npm npm

Facebook Messenger Platform NodeJS API Wrapper

js-standard-style Greenkeeper badge Codacy Badge Code Climate

Installation

Requires Node 8+

  1. npm install fb-messenger --save

API

You must require fb-messenger and create an instance

  1. // Constructor
  2. const FBMessenger = require('fb-messenger')
  3. const messenger = new FBMessenger({token, notificationType})
  4. // token is optional, if not included, must be sent in each method, notificationType is optional, default = 'REGULAR'
  5. messenger.setToken(token) // Sets the instance token
  6. messenger.setNotificationType(notificationType) // Sets the instance notificationType
  7. // Methods (notificationType and token are optional)
  8. messenger.sendTextMessage({id, message, notificationType, token}) // Sends a text message
  9. messenger.sendAudioMessage({id, url, notificationType, token}) // Sends an audio from URL
  10. messenger.sendVideoMessage({id, url, notificationType, token}) // Sends an video from URL
  11. messenger.sendImageMessage({id, url, notificationType, token}) // Sends an image from URL
  12. messenger.sendFileMessage({id, url, notificationType, token}) // Sends an file from URL
  13. messenger.sendQuickRepliesMessage({id, attachment, quickReplies, notificationType, token}) // Sends a Quick Replies Message
  14. messenger.sendButtonsMessage({id, message, buttons, notificationType, token}) // Sends a buttons template message
  15. messenger.sendGenericMessage({id, elements, notificationType, token}) // Sends a generic template message
  16. messenger.sendListMessage({id, elements, buttons, top_element_type, notificationType, token}) // Sends a list template message
  17. messenger.sendMediaMessage({id, elements, notificationType, token}) // Sends a media template message
  18. messenger.sendOpenGraphMessage({id, elements, notificationType, token}) // Sends an open graph template message
  19. messenger.sendReceiptMessage({id, payload, notificationType, token}) // Sends a receipt template message (No need for template_type in payload)
  20. messenger.sendAction({id, actionType, token}) // Send an action type (One of 'mark_seen', 'typing_on', 'typing_off')
  21. messenger.sendMessage({id, data, notificationType, token}) // Send a message from custom data
  22. messenger.getProfile({id, token}) // Gets user information
  23. messenger.setWelcomeMessage({pageId, message, token}) // Sets Page's Welcome Message (message can be a text string or a strucuted message)
  24. messenger.setGreetingText ({pageId, message, token}) // Sets Page's Greeting Text
  25. messenger.setPersistentMenu ({pageId, menuItems, token}) // Set's Page's Persistent Menu
  26. messenger.setDomainWhitelist ({pageId, domains, token}) // Set's Page's Whitelisted Domains
  27. messenger.sendThreadSettingsMessage ({pageId, body, token}) // Send Manually Page's Thread Settings

Notification Types:

  • REGULAR
  • SILENT_PUSH
  • NO_PUSH

Examples

Basic Example

  1. const FBMessenger = require('fb-messenger')
  2. const messenger = new FBMessenger({token: '<YOUR TOKEN>'}) // Will always use this page's token for request unless sent on each method
  3. messenger.sendTextMessage({id: '<ID>', text: 'Hello'})

Catch errors Example

  1. const FBMessenger = require('fb-messenger')
  2. const messenger = new FBMessenger({token: '<YOUR TOKEN>'})
  3. try {
  4. const response = await messenger.sendTextMessage({id: '<ID>', text: 'Hello'})
  5. console.log(response)
  6. } catch (e) {
  7. console.error(e)
  8. }

No push Example

  1. const FBMessenger = require('fb-messenger')
  2. const messenger = new FBMessenger({token: '<YOUR TOKEN>'})
  3. messenger.sendTextMessage({id: '<ID>', text: 'Hello', notificationType: 'NO_PUSH'})

Default to silent push Example

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

Complete Example

  1. const FBMessenger = require('fb-messenger')
  2. const messenger = new FBMessenger({token: '<YOUR TOKEN>', notificationType: 'NO_PUSH'})
  3. try {
  4. await messenger.sendTextMessage({id: '<ID>', text: 'Hello'}) // Send a message with NO_PUSH, ignoring response
  5. console.log('Sent successfully')
  6. } catch(e) {
  7. console.error(e)
  8. }
  9. // Send an image overriding default notification type with callback
  10. try {
  11. const response = await messenger.sendImageMessage({id: '<ID>', url: '<IMG URL>', notificationType: 'REGULAR'})
  12. console.log('Sent image, response:')
  13. console.dir(response)
  14. } catch(e) {
  15. console.error(e)
  16. }
  17. messenger.sendTextMessage({id: '<ID>', text: 'Hello', token: '<YOUR OTHER TOKEN>'}) // Send message on another page

License

MIT. Copyright © Diego Rodríguez Baquero