Wit Node.js SDK
node-wit
is the Node.js SDK for Wit.ai.
Install
In your Node.js project, run:
npm install --save node-wit
Quickstart
Run in your terminal:
# Node.js <= 6.x.x, add the flag --harmony_destructuring node --harmony_destructuring examples/basic.js <MY_TOKEN># Node.js >= v6.x.x node examples/basic.js <MY_TOKEN>
See examples
folder for more examples.
Messenger integration example
See examples/messenger.js
for a thoroughly documented tutorial.
Overview
The Wit module provides a Wit class with the following methods:
message
- the Wit message APIconverse
- the low-level Wit converse APIrunActions
- a higher-level method to the Wit converse API
You can also require a library function to test out your bot in the terminal. require('node-wit').interactive
Wit class
The Wit constructor takes the following parameters:
accessToken
- the access token of your Wit instanceactions
- (optional if only using.message()
) the object with your actionslogger
- (optional) the object handling the logging.apiVersion
- (optional) the API version to use instead of the recommended one
The actions
object has action names as properties, and action functions as values.
Action implementations must return Promises (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
You must provide at least an implementation for the special action send
.
send
takes 2 parameters:request
andresponse
- custom actions take 1 parameter:
request
Request
sessionId
(string) - a unique identifier describing the user sessioncontext
(object) - the object representing the session statetext
(string) - the text message sent by your end-userentities
(object) - the entities extracted by Wit's NLU
Response
text
(string) - The text your bot needs to send to the user (as described in your Wit.ai Stories)quickreplies
The logger
object should implement the methods debug
, info
, warn
and error
.
They can receive an arbitrary number of parameters to log.
For convenience, we provide a Logger
class, taking a log level parameter
Example:
const Wit log = ; const client = accessToken: MY_TOKEN actions: { return { console; return ; }; } { console; console; console; return Promise; } logger: logDEBUG // 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:
const client = accessToken: 'MY_TOKEN';client;
runActions
A higher-level method to the Wit converse API.
runActions
resets the last turn on new messages and errors.
Takes the following parameters:
sessionId
- a unique identifier describing the user sessionmessage
- the text received from the usercontext
- the object representing the session statemaxSteps
- (optional) the maximum number of actions to execute (defaults to 5)
Example:
const sessionId = 'my-user-session-42';const context0 = {};client;
See ./examples/messenger.js
for a full-fledged example
converse
The low-level Wit converse API.
Takes the following parameters:
sessionId
- a unique identifier describing the user sessionmessage
- the text received from the usercontext
- the object representing the session statereset
- (optional) whether to reset the last turn
Example:
client;
interactive
Starts an interactive conversation with your bot.
Example:
const interactive = ;;
See the docs for more information.
Changing the API version
On 2016, May 11th, the /message API was updated to reflect the new Bot Engine model: intent are now entities.
We updated the SDK to the latest version: 20160516.
You can target a specific version by passing the apiVersion
parameter when creating the Wit
object.
Version prior to 20160511 will return the old format: