node-wit
is the Node.js SDK for Wit.ai.
In your Node.js project, run:
npm install --save node-wit
Run in your terminal:
node examples/basic.js <WIT_TOKEN>
See examples
folder for more examples. Some examples have associated .zip files, do not forget to import those when creating a new app and grab your access token from the Settings section.
See examples/messenger.js
for a thoroughly documented tutorial.
The Wit module provides a Wit class with the following methods:
runComposerAudio
- the Composer integration for voice;runComposer
- the Composer integration for other inputs;converse
- the Wit converse API;event
- the Wit event API;message
- the Wit message API;speech
- the Wit speech API;dictation
- the Wit dictation API;synthesize
- the Wit synthesize API.You can also require a library function to test out your Wit app in the terminal. require('node-wit').interactive
The Wit constructor takes the following parameters:
accessToken
- the access token of your Wit instance;actions
- the object of client action definitions for Composer;logger
- (optional) the object handling the logging;apiVersion
- (optional) the API version to use instead of the recommended oneThe 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 actions = {
confirm_order(contextMap) {
return {context_map: {...contextMap, order_confirmation: 'PIZZA42'}};
},
};
const client = new Wit({
accessToken: MY_TOKEN,
actions,
logger: new log.Logger(log.DEBUG), // optional
});
console.log(client.message('set an alarm tomorrow at 7am'));
The Composer integration for voice.
Takes the following parameters:
sessionId
- a unique string identifying the user sessioncontentType
- the Content-Type headerbody
- the audio Readable
streamcontextMap
- the context map objectEmits partialTranscription
, response
and fullTranscription
events.
Run the provided actions
as instructed by the API response, and calls back with the resulting updated context map (unless the action returns stop: true
).
The Promise returns the final JSON payload of the last API call (POST /converse or POST
/event).
See lib/interactive.js
for an example.
The Composer integration for other inputs, including text.
Takes the following parameters:
sessionId
- a unique string identifying the user sessioncontextMap
- the context map objectmessage
- the optional user text queryEmits response
events.
Run the provided actions
as instructed by the API response, and calls back with the resulting updated context map (unless the action returns stop: true
).
The Promise returns the final JSON payload of the last POST /event API call.
See lib/interactive.js
for an example.
The Wit converse API.
Takes the following parameters:
sessionId
- a unique string identifying the user sessioncontentType
- the Content-Type headerbody
- the audio Readable
streamcontextMap
- the context map objectEmits partialTranscription
, fullTranscription
, and response
events. Runs
intermediate actions
as instructed by the API.
We recommend to use .runComposerAudio()
instead of this raw API.
The Wit event API.
Takes the following parameters:
sessionId
- a unique string identifying the user sessioncontextMap
- the context map objectmessage
- the optional user text queryEmits response
events, and run intermediate actions
as instructed by the API.
We recommend to use .runComposer()
instead of this raw API.
The Wit message API.
Takes the following parameters:
q
- the text input you want Wit.ai to extract the information fromcontext
- (optional) the Context objectn
- (optional) the max number of intents and traits to get backExample:
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);
See lib/interactive.js
for another example integration.
The Wit speech API.
Takes the following paramters:
contentType
- the Content-Type headerbody
- the audio Readable
streamcontext
- (optional) the Context objectn
- (optional) the max number of intents and traits to get backEmits partialTranscription
, partialUnderstanding
and fullTranscription
events.
The Promise returns the final JSON payload.
See lib/interactive.js
for an example.
The Wit dictation API.
Takes the following paramters:
contentType
- the Content-Type headerbody
- the audio Readable
streamEmits partialTranscription
, and fullTranscription
events.
The Promise returns the final JSON payload.
See examples/synthesize-speech.js
for an example.
The Wit synthesize API.
Takes the following paramters (click on link above for more details):
q
- The query containting text to synthesizevoice
- The voice name. For voices and styles available, see GET voices.style
- (optional) The style to speak inspeed
- (optional) the speed the text is spokenpitch
- (optional) the pitch of the audiogain
- (optional) the gain of the audioThe Promise returns the final response, with the body containing the audio stream of the synthesized text.
See examples/synthesize-speech.js
for an example.
Starts an interactive conversation with your Wit app.
Full conversational interactions:
Use !converse
to send an audio request from the microphone using Composer.
Enter any text input to send a text request using Composer.
One-off natural language requests:
Use !speech
to send an audio request from the microphone.
Use !message <your message>
to send a text request.
Example:
const {interactive} = require('node-wit');
interactive(client);
See the docs for more information.
The default (recommended, latest) API version is set in config.js
.
On May 13th, 2020, the GET /message
API was updated to reflect the new data model: intents, traits and entities are now distinct.
You can target a specific version by passing the apiVersion
parameter when creating the Wit
object.
{
"text": "hello",
"intents": [
{
"id": "1353535345345",
"name": "greet",
"confidence": 0.9753
}
],
"entities": [],
"traits": []
}
WIT_TOKEN=XXX npm test
, where XXX is the Server Access TokenThe license for node-wit can be found in LICENSE file in the root directory of this source tree.
Our terms of use can be found at https://opensource.facebook.com/legal/terms.
Use of Wit.ai services fall under the terms of use found here: https://wit.ai/terms.
Our privacy policy can be found at https://opensource.facebook.com/legal/privacy.
The privacy policy for the Wit.ai service can be found at https://wit.ai/privacy.