1.0.7 • Published 7 years ago

botbuilder-facebook v1.0.7

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

botbuilder-facebook

Facebook Messenger bot connector for Microsoft BotBuilder.

Get started

  1. Install botbuilder-facebook

    npm install botbuilder-facebook --save
  2. Initialize Facebook Bot.

    bot.js:

    'use strict'
    
    const FacebookBot = require('botbuilder-facebook');
    
    const bot = new FacebookBot({
      pageToken: 'YOUR_FB_PAGE_TOKEN',
      validationToken: 'APP_VERIFICATION_TOKEN'
    });
    
    bot.add('/', session => {
      session.send('Hello!');
    });
  3. Run express server and listen to messages

    'use strict';
    
    const server     = require('express')();
    const bodyParser = require('body-parser');
    
    const bot = require('./bot');
    
    server.use(bodyParser.json());
    
    server.get('/', (req, res) => {
      bot.botService.validate(req.query, function(err, challenge) {
        if (err) {
          console.error(err);
          res.send('Error, validation failed');
          return;
        }
        console.log('validation successful');
        res.send(200, challenge);
      });
    });
    
    server.post('/', (req, res) => {
      bot.botService.receive(req.body);
      res.sendStatus(200);
    });
    
    server.listen(5000, function() {
      console.log(`Bot server listening on port 5000`);
    });

Message examples

All examples is done using builder.Message object, but you can use a plain JS object as well. Something like this:

session.send({
  attachments: [{
    thumbnailUrl: "http://petersapparel.parseapp.com/img/item101-thumb.png",
    title: "Classic Grey T-Shirt",
    titleLink: "https://petersapparel.parseapp.com/view_item?item_id=101",
    text: "Soft white cotton t-shirt is back in style"
  }]
});
  1. Message with image attachment
  var msg = new builder.Message()
    .addAttachment({
      contentUrl: "http://www.theoldrobots.com/images62/Bender-18.JPG",
      contentType: "image/jpeg"
    });
  return session.send(msg);
  1. Generic template
var msg = new builder.Message()
  .addAttachment({
      thumbnailUrl: "http://petersapparel.parseapp.com/img/item101-thumb.png",
      title: "Classic Grey T-Shirt",
      titleLink: "https://petersapparel.parseapp.com/view_item?item_id=101",
      text: "Soft white cotton t-shirt is back in style"
  });
  1. Generic template with Call-To-Action items and bubbles
var msg = new builder.Message();
msg.addAttachment({
    title: "Classic White T-Shirt",
    text: "Soft white cotton t-shirt is back in style",
    thumbnailUrl: "http://petersapparel.parseapp.com/img/item100-thumb.png",
    actions: [
        { title: "View Item", url: "https://petersapparel.parseapp.com/view_item?item_id=100" },
        { title: "Buy Item", message: "buy:100" },
        { title: "Bookmark Item", message: "bookmark:100" }
    ]
});
msg.addAttachment({
    title: "Classic Grey T-Shirt",
    text: "Soft gray cotton t-shirt is back in style",
    thumbnailUrl: "http://petersapparel.parseapp.com/img/item101-thumb.png",
    actions: [
        { title: "View Item", url: "https://petersapparel.parseapp.com/view_item?item_id=101" },
        { title: "Buy Item", message: "buy:101" },
        { title: "Bookmark Item", message: "bookmark:101" }
    ]
});
  1. Receipt or any other custom message template
var msg = new builder.Message();
msg.setChannelData({
    "attachment":{
        "type":"template",
        "payload":{
            "template_type":"receipt",
            "recipient_name":"Stephane Crozatier",
            "order_number":"12345678902",
            "currency":"USD",
            "payment_method":"Visa 2345",
            "order_url":"http://petersapparel.parseapp.com/order?order_id=123456",
            "timestamp":"1428444852",
            "elements":[
                {
                    "title":"Classic White T-Shirt",
                    "subtitle":"100% Soft and Luxurious Cotton",
                    "quantity":2,
                    "price":50,
                    "currency":"USD",
                    "image_url":"http://petersapparel.parseapp.com/img/whiteshirt.png"
                },
                {
                    "title":"Classic Gray T-Shirt",
                    "subtitle":"100% Soft and Luxurious Cotton",
                    "quantity":1,
                    "price":25,
                    "currency":"USD",
                    "image_url":"http://petersapparel.parseapp.com/img/grayshirt.png"
                }
            ],
            "address":{
                "street_1":"1 Hacker Way",
                "street_2":"",
                "city":"Menlo Park",
                "postal_code":"94025",
                "state":"CA",
                "country":"US"
            },
            "summary":{
                "subtotal":75.00,
                "shipping_cost":4.95,
                "total_tax":6.19,
                "total_cost":56.14
            },
            "adjustments":[
                {
                    "name":"New Customer Discount",
                    "amount":20
                },
                {
                    "name":"$10 Off Coupon",
                    "amount":10
                }
            ]
        }
    }
});

License

MIT License

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago