2.1.5 • Published 4 years ago

bot-facebook v2.1.5

Weekly downloads
4
License
ISC
Repository
github
Last release
4 years ago

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",
}
2.1.2

4 years ago

2.1.1

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.5

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

1.0.0

4 years ago