Git Product home page Git Product logo

botbuilder-facebook's Introduction

botbuilder-facebook

Facebook Messenger bot connector for Microsoft BotBuilder.

Get started

  1. Install botbuilder-facebook
npm install botbuilder-facebook --save
  1. 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!');
});
  1. 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);

example

  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"
  });

example

  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" }
    ]
});

example

  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
                }
            ]
        }
    }
});

example

License

MIT License

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.