| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
node-wit is the Node.js SDK for Wit.ai.
In your Node.js project, run:
npm install --save node-witRun 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.
See examples/messenger.js for a thoroughly documented tutorial.
The Wit module provides a Wit class with the following methods:
You can also require a library function to test out your bot in the terminal. require('node-wit').interactive
The Wit constructor takes the following parameters:
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.
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} = require('node-wit');
const client = new Wit({
accessToken: MY_TOKEN,
actions: {
send(request, response) {
return new Promise(function(resolve, reject) {
console.log(JSON.stringify(response));
return resolve();
});
},
myAction({sessionId, context, text, entities}) {
console.log(`Session ${sessionId} received ${text}`);
console.log(`The current context is ${JSON.stringify(context)}`);
console.log(`Wit extracted ${JSON.stringify(entities)}`);
return Promise.resolve(context);
}
},
logger: new log.Logger(log.DEBUG) // optional
});The Wit message API.
Takes the following parameters:
Example:
const client = new Wit({accessToken: 'MY_TOKEN'});
client.message('what is the weather in London?', {})
.then((data) => {
console.log('Yay, got Wit.ai response: ' + JSON.stringify(data));
})
.catch(console.error);A higher-level method to the Wit converse API. runActions resets the last turn on new messages and errors.
Takes the following parameters:
Example:
const sessionId = 'my-user-session-42';
const context0 = {};
client.runActions(sessionId, 'what is the weather in London?', context0)
.then((context1) => {
console.log('The session state is now: ' + JSON.stringify(context1));
return client.runActions(sessionId, 'and in Brussels?', context1);
})
.then((context2) => {
console.log('The session state is now: ' + JSON.stringify(context2));
})
.catch((e) => {
console.log('Oops! Got an error: ' + e);
});See ./examples/messenger.js for a full-fledged example
The low-level Wit converse API.
Takes the following parameters:
Example:
client.converse('my-user-session-42', 'what is the weather in London?', {})
.then((data) => {
console.log('Yay, got Wit.ai response: ' + JSON.stringify(data));
})
.catch(console.error);Starts an interactive conversation with your bot.
Example:
const {interactive} = require('node-wit');
interactive(client);See the docs for more information.
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.
{
"msg_id" : "e86468e5-b9e8-4645-95ce-b41a66fda88d",
"_text" : "hello",
"entities" : {
"intent" : [ {
"confidence" : 0.9753469589149633,
"value" : "greetings"
} ]
}
}Version prior to 20160511 will return the old format:
{
"msg_id" : "722fc79b-725c-4ca1-8029-b7f57ff88f54",
"_text" : "hello",
"outcomes" : [ {
"_text" : "hello",
"confidence" : null,
"intent" : "default_intent",
"entities" : {
"intent" : [ {
"confidence" : 0.9753469589149633,
"value" : "greetings"
} ]
}
} ],
"WARNING" : "DEPRECATED"
}| Back | FazBrowse Home | New Git URL |