npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bot-facebook

v2.1.5

Published

support send message,config menu

Downloads

30

Readme

Facebook Messenger Platform App

Node API adapter in ECMAScript 2015 (ES6)

npm npm JavaScript Style Guide npm

Codacy Badge

Table of Contents

Installation

Install the bot-facebook package in your node server app's local node_modules folder.

npm i bot-facebook

How to start

I take for granted that you've already setup your Messenger Platform app:

  1. Created a Facebook App and Page
  2. Setup the Webhook
  3. Got a Page Access Token
  4. Subscribed the App to the Page

After installing the fb-messenger-app package with npm as shown above, import it in your app

const botFacebook = require('bot-facebook/disk/bot-facebook')

Then create a new messenger instance passing your Facebook App page access token (this token will include all messenger permissions)

var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN)

Receive Messages

You'll have to listen for GET calls at your webhook. Callbacks will be made to this webhook.

var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN)
app.get('/webhook', function (req, res) {
    messenger._verify(req,res,verifyToken);
})

Event Message

var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN)
app.post('/webhook', function (req, res) {
     var entries = req.body.entry;
            for (var entry of entries) {
                var messaging = entry.messaging;
                for (var message of messaging) {
                    var senderId = message.sender.id;

                    if (message.message) {
                        var text = message.message.text;
                        // Send message Text, Replies, TemplateButton, Action
                          if (message.message.attachments) {
                              //Save attachment 
                               // Send message Text, Replies, TemplateButton, Action
                          }
                    }
                     if (message.postback) {
                         // Send message Text, Replies, TemplateButton ,Action
                     }
                     if(message.)
                }
            }
})

Send message text

    var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN)
    messenger._sendMessageText(senderId,"^^ Hello");
Result
{
  "recipient_id": "1254477777772919",
  "message_id": "AG5Hz2Uq7tuwNEhXfYYKj8mJEM_QPpz5jdCK48PnKAjSdjfipqxqMvK8ma6AC8fplwlqLP_5cgXIbu7I3rBN0P"
}  

Send message Attachment

The Messenger Platform allows you to attach assets to messages, including audio, video, images, and files. The maximum attachment size is 25 MB. The maximum resolution for images is 85 Megapixel. There are three ways to attach an asset to a message:

  • URL
  • attachment_id

Attachment Types The Messenger Platform supports the following attachment types, specified in the attachment.type property of the message:

  • audio
  • video
  • image
  • file
    var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN);
    var attachment = {
      "type":"image", 
      "payload":{
        "url":"http://www.messenger-rocks.com/image.jpg", 
        "is_reusable":true
      }
    }
    var attach = {
      "type":"image", 
      "payload":{
        "attachment_id": "1745504518999123"
      }
    }
    messenger._sendAttachment(senderId,attachment)
    messenger._sendAttachment(senderId,attach)

Send message Quickreplies

   var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN);
   var quickreplies = [
      {
        "content_type":"text",
        "title":"Red",
        "payload":"<POSTBACK_PAYLOAD>",
        "image_url":"http://example.com/img/red.png"
      },{
        "content_type":"text",
        "title":"Green",
        "payload":"<POSTBACK_PAYLOAD>",
        "image_url":"http://example.com/img/green.png"
      }
    ]
    messenger._sendQuickReplies(senderId,"Hello",quickreplies)

Send Action read,typing on,typing off

    var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN);
    // Make seen
    messenger._sendAction(senderId,"mark_seen")
    // Typing On
    messenger._sendAction(senderId,"typing_on")
    // Typing Off
    messenger._sendAction(senderId,"typing_off")

Send TemplateButton

    var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN);
    let arraybtn =[{
                                "type": "postback",
                                "title": "Hello everyOne",
                                "payload": "<POSTBACK_PAYLOAD>"
                     }
                ]
     messenger._sendTemplateButton(senderId,text,arraybtn);

Save Attachment

    var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN);
    /**
    * @param {retrieved in the event message} message.message.attachments
    */
    messenger._saveAttachment(senderId,message.message.attachments)

Get started

    var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN);
    messenger._getStart({
        "get_started":[
        {
            "payload":"USER_DEFINED_PAYLOAD"
            }
        ]
    })

Persistent Menu

    var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN);
    messenger._persistentMenu([{ "title": "Xem tin tức", "type": "postback", "payload": "<POSTBACK_PAYLOAD>" }]);

GetInfoUser

    var messenger = new botFacebook(MY_PAGE_ACCESS_TOKEN);
    messenger._getInfoUser(senderId);
  • Result
{
  "first_name": "Peter",
  "last_name": "Chang",
  "profile_pic": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p200x200/13055603_10105219398495383_8237637584159975445_n.jpg?oh=1d241d4b6d4dac50eaf9bb73288ea192&oe=57AF5C03&__gda__=1470213755_ab17c8c8e3a0a447fed3f272fa2179ce",
  "locale": "en_US",
  "timezone": -7,
  "gender": "male",
}