wit-js
v0.0.1
Published
A modern Node wrapper for the Wit.ai API using Promises and Events
Downloads
15
Readme
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 = 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 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.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 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.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 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.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 sessionmessage
- the text received from the usercontext
- 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);
});