wit-js

0.0.1 • Public • Published

Node integration for Wit.ai npm

wit-js is an alternative Node integration for Wit.ai using Promises and Events.

Install

In your Node.js project, run:

npm install --save wit-js

Quickstart

Run in your terminal:

let Wit = require('wit-js');
 
client = new Wit.Client({apiToken: '<your-api-token>'});
 
client.message('Hello!', {})
    .then((response) => {
        console.log(response.entities);
    })
    .catch((err) => {
        console.error(err);
    });

API

Overview

The Wit module provides a Client class with the following methods:

  • message - the Wit message API
  • converse - the low-level Wit converse API
  • run - a higher-level method to the Wit converse API
  • on - a simple method to bind to events fired by the client

Client class

The Client constructor requires a single options paramter, which may contain the following values:

  • apiToken - the access token of your Wit instance, required
  • apiVersion - the version of the Wit API you wish to targer, optional
  • apiRoot - the base URL for the API, useful for proxying or targeting other implementations optional

.message(...)

The Wit message API.

Takes the following parameters:

  • message - the text you want Wit.ai to extract the information from
  • context - (optional) the object representing the session state

Example:

client.message('Hello!', {})
    .then((response) => {
        console.log('Wit responded!');
        console.log(response.entities);
    })
    .catch((err) => {
        console.error(err);
    });
});

.run(...)

A higher-level method to the Wit converse API.

Takes the following parameters:

  • session - a unique identifier describing the user session
  • message - the text received from the user
  • context - the object representing the session state

This method runs recursively and acts upon actions retrieved from the API, fire the relevant events where necessary. Use the .on method of the client to subscribe to events.

Example:

client.on('msg', (data, context) => {
    console.log(`Wit said: ${data.msg}`);
});
client.run('my-session-id', 'what is the weather in London?', {});

.on(...)

Bind a listener to a Wit event. This interface is functionally identical to Node's EventEmitter.

Takes the following parameters:

  • event - the name of the event to which to bind
  • listener - the function to be called when the event is emitted

There are a total of 4 default events that will be emitted by the Client, they are:

  • merge - emitted as the first step of every bot action
  • msg - emitted when the bot engine "says" something
  • stop - the final action of the bot's "conversation", use this for any clean up
  • error - emitted if the bot engine encounters a problem

On top of these four, any actions configured within the bot engine will be emitted in the format action:<action-name>, for example action:fetch-weather.

Examples:

client.on('merge', (data, context) => {
    console.log(context);
});
client.on('action:query-database', (data, context) => {
    // maybe you are querying a mongo collection
    myCollection.find(context.query);
});

.converse(...)

The low-level Wit converse API.

Takes the following parameters:

  • session - a unique identifier describing the user session
  • message - the text received from the user
  • context - the object representing the session state

Example:

 
client.run('my-session-id', 'what is the weather in London?', {})
    .then((response) => {
        console.log('Wit responded!');
        console.log(response.entities);
    })
    .catch((err) => {
        console.error(err);
    });

Readme

Keywords

Package Sidebar

Install

npm i wit-js

Weekly Downloads

9

Version

0.0.1

License

MIT

Last publish

Collaborators

  • grieve