Node integration for Wit.ai
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 = ; client = apiToken: '<your-api-token>'; client ;
API
Overview
The Wit module provides a Client class with the following methods:
message
- the Wit message APIconverse
- the low-level Wit converse APIrun
- a higher-level method to the Wit converse APIon
- 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, requiredapiVersion
- the version of the Wit API you wish to targer, optionalapiRoot
- 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 fromcontext
- (optional) the object representing the session state
Example:
client ;});
.run(...)
A higher-level method to the Wit converse API.
Takes the following parameters:
session
- a unique identifier describing the user sessionmessage
- the text received from the usercontext
- 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;client;
.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 bindlistener
- 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 actionmsg
- emitted when the bot engine "says" somethingstop
- the final action of the bot's "conversation", use this for any clean uperror
- 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;client;
.converse(...)
The low-level Wit converse API.
Takes the following parameters:
session
- a unique identifier describing the user sessionmessage
- the text received from the usercontext
- the object representing the session state
Example:
client ;