npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

api-ai-botkit-facebook

v2.0.0

Published

Utility lib for creating Facebook Messenger bots with Botkit and api.ai

Downloads

20

Readme

api-ai-botkit-facebook

npm

Utility lib for creating Facebook Messenger bots with Botkit and api.ai. Based off of the api-ai-botkit.

For usage sample code see examples/sample_bot.js

Steps for using lib

Install library from npm

npm install --save api-ai-botkit-facebook

Import Library

const apiaibotkit = require('api-ai-botkit-facebook');

Create apiai object using token from http://api.ai website

const apiai = apiaibotkit({ token: 'some-API.ai-token' });

or to use sticky sessionIds, which generate the api.ai session id from the message channel, so sessions will stick even after a server restart

const apiai = apiaibotkit({ token: 'some-API.ai-token', useStickySessions: true });

Use apiai object in controller.hears

controller.hears('.*', 'message_received', function (bot, message) {
    apiai.process(message, bot);
});

Implement different reactions to appropriate actions

apiai
  .action('greetings', function (message, resp, bot) {
    var responseText = resp.result.fulfillment.speech;
    bot.reply(message, responseText);
  })
  .action('input.unknown', function (message, resp, bot) {
    bot.reply(message, "Sorry, I don't understand");
  })
  .action(null, function(message, resp, bot) {
    // handle all actions no not specified with
    // other actions (e.g., greetings and input.unknown)
    var responseText = resp.result.fulfillment.speech;
    bot.reply(message, responseText);
  })
;

Middleware

The functionality can be extended using middleware functions. These functions can plugin to the api.ai running processes at couple useful places and make changes to both the query or response.

Middleware Endpoints

The module currently supports middleware insertion in two places:

  • When sending a request, before the query is sent to api.ai
  • When receiving a response, before triggering any events

Request and Response middleware functions are added to the module using an Express-style "use" syntax. Each function receives a set of parameters and a next function which must be called to continue processing the middleware stack.

Request Middleware

Request middleware can be used to do things like preprocess the query or options before it gets sent out to api.ai.

apiai.middleware.query.use((message, query, options, bot, next) => {
  // do something...
  // options.contexts.resetContexts = true;
  next();
});

Response Middleware

Response middleware can be used to do things like preprocess the response content. Additional information can be added to the response object for use down the chain.

apiai.middleware.response.use((message, response, bot, next) => {
  // do something...
  // response.extrainfo = 'bar';
  next();
});

Advanced Queries

Add an apiaiOptions property to the message to pass options to api.ai in the query.

controller.hears('contact me', 'message_received', function (bot, message) {
  message.apiaiOptions = {
    contexts: [
      {
        "name": "contact",
        "lifespan": 4,
        "parameters": {
          "username": "fred"
        }
      }
    ]
  };
  apiai.process(message, bot);
});

Add an event property to the message to call the eventRequest function. By default, process calls the textRequest function.

controller.hears('trigger event', 'message_received', function (bot, message) {
  message.event = {
    name: 'Event1',
    data: {
      parameterName: 'parameter_value',
      foo: 'bar'
    }
  };
  apiai.process(message, bot);
});