Giter Site home page Giter Site logo

fb-bot-api's Introduction

Facebook Bot Node API

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][downloads-url] [![Dependency Status][david-image]][david-url] [npm-image]: https://img.shields.io/npm/v/fb-bot-api.svg?style=flat-square [npm-url]: https://npmjs.org/package/fb-bot-api [downloads-image]: http://img.shields.io/npm/dm/fb-bot-api.svg?style=flat-square [downloads-url]: https://npmjs.org/package/fb-bot-api [david-image]: http://img.shields.io/david/Voronchuk/fb-bot-api.svg?style=flat-square [david-url]: https://david-dm.org/Voronchuk/fb-bot-api

Getting Started

Use this library to communicate with the Facebook Messenger API to develop a bot for Facebook Messenger. Go to developers.facebook.com to learn more and start building a bot.

[npm install fb-bot-api][npm-url]

License

The Facebook Messenger bot library is released under the terms of the MIT license. See License for more information or see https://opensource.org/licenses/MIT.

Process

Sample config:

var config = {
    WEBSERVER: {
        PORT: 8080,
        PROXY_CONFIG: 'loopback',
        URL_PREFIX: '/facebook'
    },
    FB_MESSAGE_URL: 'https://graph.facebook.com/v2.6',
    VERIFY_TOKEN: '...',
    APP_ID: '...',
    PAGE_ID: '...',
    PROFILE_TOKEN: '...',
    
    MESSAGE_DELIVERY_TRACKING_TIMEOUT: 30000 // Optional settings which enables tracking a delivery of sent messages
};

Make bot instance:

var FacebookBot = require('fb-bot-api').Facebook;

var instance = new FacebookBot(config);
instance.on('message', function (sender, message) {
    ...
});
instance.on('message-image', function (sender, imageUrl) {
    ...
});
instance.on('message-postback', function (sender, postback) {
    ...
});
instance.on('message-read', function (sender, read) {
    ...
});
instance.on('message-optin', function (sender, optin) {
    ...
});
instance.on('error', function (error) {
    ...
});

// If MESSAGE_DELIVERY_TRACKING_TIMEOUT is set
instance.on('message-delivered', function (sender, signature) {
    ...
});

instance.listen();


// Welcome message
instance.bot.setWelcome('Hello world!');
instance.bot.setWelcome('Hello world!', [
    {
        type: 'postback',
        label: 'Start'
    },
    {
        type: 'postback',
        label: 'End'
    }
]);

// Normal messages with and without links
instance.bot.sendText(facebookUserId, 'Some message');
instance.bot.sendText(facebookUserId, 'Some message', [
    {
        type: 'postback',
        label: 'Answer'
    },
    {
        type: 'web_url',
        label: 'Google',
        content: 'https://google.com'
    }
]);

// Image messages
instance.bot.sendImage(facebookUserId, 'https://images.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');
instance.bot.sendImage(facebookUserId, '/local/path/to/file.png');

// Structured messages with horizontal scrolling
instance.bot.sendItems(facebookUserId, [
    {
        title: "Welcome to My Company #1!",
        item_url: "https://www.petersbowlerhats.com",
        image_url: "https://www.petersbowlerhats.com/img/hat.jpeg",
        subtitle: "We have the right hat for everyone.",
        buttons: [
            {
                type: "web_url",
                label: "View Website 1",
                content: "https://www.petersbowlerhats.com"
            },
            {
                type: "postback",
                label: "Start Chatting 1"
            }
        ]
    },
    {
        title: "Welcome to My Company #2!",
        item_url: "https://www.petersbowlerhats.com",
        image_url: "https://www.petersbowlerhats.com/img/hat.jpeg",
        subtitle: "We have the right hat for everyone.",
        buttons: [
            {
                type: "web_url",
                label: "View Website 2",
                content: "https://www.petersbowlerhats.com"
            },
            {
                type: "postback",
                label: "Start Chatting 2"
            }
        ]
    }
]);

By default the system initialise Express 4 web-server instance to listen for FB Messenger webhook messages, if thats an overkill for you can pass custom web-server engine as the second param of FacebookBot constructor, like new FacebookBot(config, express()).

Diclaimer

This library is still early in it's development, lacks tests and features, so use on your own risk.

fb-bot-api's People

Contributors

alexmold avatar voronchuk avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.