Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

botframework

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

botframework

Framework for messaging bots

  • 0.8.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status semantic-release

Bot Framework

Bot Framework allows you to write bots for Facebook Messenger. But it has been designed to allow integration of other bots.

Install

npm install botframework

Usage

Plan where you will deploy your bot

In order to setup the Facebook Bot in next step you need to define a

For testing I can recommend http://localtunnel.me/

Setup your facebook bot

follow https://developers.facebook.com/docs/messenger-platform/quickstart to set up your bot. Note the access_token. We will need it

general

JavaScript

var bf = require('../');
var bot = new bf.Bot({
  fb: {
    page_id: <your facebook page id>,
    verify_id: <your verify id>,
    port: 3000,
    callback_path: '/facebook/receive',
    access_token: <access_token from facebook>
  }
}, new ctrl());

function ctrl() {
  this.newUser = function (data) {
    console.log('user'+ JSON.stringify(data));
    reply.text('hi');
  };
  this.textMessage = function(data, reply) {
    reply.text('Servus: ' + data.text);
  };
  // newUser?(msg: INewUserMessage, reply: IBotReply): void;
  // imageMessage?(imageMessage: IImageMessage, reply: IBotReply): void;
  // linkMessage?(linkMessage: ILinkMessage, reply: IBotReply): void;
  // locationMessage?(locationMessage: ILocationMessage, reply: IBotReply): void;
  // catchAll?(user: IBotUser, msg: Object, reply: IBotReply): void;
}

TypeScript

import {IBotSettings, , IBotController} from 'botframework';

let botSettings: IBotSettings = {
  fb: {
    page_id: <your facebook page id>,
    verify_id: <your verify id>,
    port: 3000,
    callback_path: '/facebook/receive',
    access_token: <access_token from facebook>
  }
} ;

class BotController implements IBotController {
  textMessage(msg: ITextMessage, reply: IBotReply): any {
    reply.text('hi');
  }
  
  // newUser?(msg: INewUserMessage, reply: IBotReply): void;
  // imageMessage?(imageMessage: IImageMessage, reply: IBotReply): void;
  // linkMessage?(linkMessage: ILinkMessage, reply: IBotReply): void;
  // locationMessage?(locationMessage: ILocationMessage, reply: IBotReply): void;
  // catchAll?(user: IBotUser, msg: Object, reply: IBotReply): void;
}
var bot = new Bot(botSettings, new BotController());

Handling other message types like Location, Image, Authentication

Botframework detects the facebook message type and calls the according handler callback function if its defined.

You can implement more handlers. Following callbacks are currently supported:

export interface IBotController {
  newUser?(msg: INewUserMessage, reply: IBotReply): void; // handles facebook Authentication callback
  textMessage?(textMessage: ITextMessage, reply: IBotReply): void; // handles text only messages
  imageMessage?(imageMessage: IImageMessage, reply: IBotReply): void; // handles received images
  linkMessage?(linkMessage: ILinkMessage, reply: IBotReply): void; // handles received links from mobile phone sendTo Plugin
  locationMessage?(locationMessage: ILocationMessage, reply: IBotReply): void; // handles received locations
  catchAll?(user: IBotUser, msg: Object, reply: IBotReply): void;
}

Replying

The Reply interfaces currently supports replying with a simple text message and a list message.

// reply with list
let botItems: Array<IBotReplyListItem> =  response.data.map( (obj: Object) => {
  let buttons = [
      {
        title: 'Open Link',
        url: obj.href,
        type: 'web_url'
      }
    ];
  return {
    title: obj.name,
    image_url: obj.img_url
    subtitle: obj.desc || '',
    buttons
  }
});
reply.list(botItems);


//////

//reply with text
reply.text('Hi there');

Keywords

FAQs

Package last updated on 19 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc