Giter Site home page Giter Site logo

node-fb-messenger's Introduction

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

node-fb-messenger's People

Contributors

agathaftrindade avatar diegorbaquero avatar greenkeeper[bot] avatar greenkeeperio-bot avatar kalepail avatar lhernandezcruz avatar ryanscottaudio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-fb-messenger's Issues

Any way to access JSON sent to Facebook

I have a basic app that:

  1. takes a message via a REST API
  2. relays that message to Facebook Messenger using your wonderful library
  3. returns a response via the same REST API

The problem I'm running in to is that in step 3 I'd like to return the JSON sent to Facebook Messenger, so that a QA script can hit my API and verify that the incoming message resulted in the correct outgoing message to Facebook ... but there is currently no way to access the request. It's created inside the various send* methods, then passed to sendRequest which sends it off to Facebook without exposing it in any way.

Could you please expose the request, or at least req.json? It sees to me that the simplest way to do this would be to change line 187 from:

    cb(null, body);

to:

    cb(null, body, req);

However, that only gives you the request after the response comes back. What would truly be ideal, in my opinion, is if sendRequest could simply return the req, and every send* method returned the result of sendRequest ...

sendTextMessage (id, text, notificationType, cb) {
  const messageData = {
    text: text
  }
  return this.sendMessage(id, messageData, notificationType, cb)
}

const sendRequest = (req, cb) => {
  request(req, (err, res, body) => {
    if (!cb) return
    if (err) return cb(err)
    if (body.error) return cb(body.error)
    cb(null, body)
  })
  return req;
}

Could you please consider implementing one of these ways (or some alternative way) to expose the request object?

Cannot build persistent menu

My code:

messenger.setPersistentMenu({
    pageId: app.get('page_id'),
    menuItems: [{
        type: "postback",
        title: "Talk to an agent",
        payload: "CARE_HELP"
    },
    {
        type: "postback",
        title: "Outfit suggestions",
        payload: "CURATION"
    },
    {
        type: "web_url",
        title: "Shop now",
        url: "https://3000-c5d48a10-3ffd-4635-b0e9-36c6e897295d.ws-us02.gitpod.io/",
        webview_height_ratio: "full"
    }],
    // token: app.get('admin_token')
})
    .then(console.log)
    .catch(console.log)

It throw Error

{
  error: {
    message: '(#200) User does not have sufficient administrative permission for this action on this page. If the page business requires Two Factor Authentication, the user also needs to enable Two Factor Authentication.',
    type: 'OAuthException',
    code: 200,
    fbtrace_id: 'ADYHrfkOhmngibbxuEQclAr'
  }
}

When I try to get Profile receiving error "Service temporarily unavailable"

Hi,

Today I got a issue, every time when I try to get profile I receive this erro "Service temporarily unavailable"

facebook error: { message: '(#2) Service temporarily unavailable',
type: 'OAuthException',
is_transient: true,
code: 2,
fbtrace_id: 'H58BCfKkfuO' }

I don't know why.. but it happen every time

Related to token and ID

The token specified for sending msg is it Users token Right???
And ID is msg receivers id is it right?
But i got an error like this

Try Users token:

{ message: 'Unsupported post request. Object with ID 'me' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api',
type: 'GraphMethodException',
code: 100,
fbtrace_id: 'B69oYdvdIH3' }

Try page token I got this:

Error: { message: '(#100) No matching user found',
type: 'OAuthException',
code: 100,
fbtrace_id: 'HBjp0Pu1DOb' }

Why i got this error??

[IMPORTANT] No updates for 0.0.x

SemVer caret for 0.0.x releases doesn¡t allow updates at all.

I just bumped to 0.1.x to allow minor updates as SemVer treats 0.x.y as 0.major.minor from 0.1.0

Please update to 0.1.x manually or use npm i --save [email protected] to get the latest version and receive the minor updates that will come soon!

Thank you

Whitlisting domains

When sending links in cards the link as to be on the page's whitelist for the message to be sent.
Can you add a method for that? Or validate my request if I find time to write it ?

unsupported post request

Permission for the User's Profile Information in a Messenger Bot should be implicitly granted "When a user begins a 1:1 thread with the bot for the first time".

My bot is still in sandbox mode (not sure if that is the issue), but with my own personal messenger account where I started the conversation with my bot -> my bot does not have access to my User Profile. It gets the following error:

{u'error': {u'message': u"Unsupported post request. Object with ID 'PAGE_SCOPED_USER_ID' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api", u'code': 100, u'type': u'GraphMethodException', u'fbtrace_id': u'Ev1G0M7KVxE', u'error_subcode': 33}}
Do you know why????

Thanks!

Typo in Documentation

This is really minor, but I thought you might want to fix:

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

to instead be:

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

(ie. "Menu" at the end, not "Meny").

Buttons

Do we have to generate the JSON for the buttons on our own?

Support Multiple Pages

One bot can receive in its webhook multiple pages so the module needs to support them.

How to send quick replies?

i am sorry to open this issue,
i tried but failed to use quickreplies can you just give me example how to use that ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.