A wrapper class for the Wit Webservice (https://wit.ai/docs/http/20160330 ).
import request from 'request-promise';
const baseURL = 'https://api.wit.ai';
const apiVersion = '20160330';
const intentEndpoint = '/message';
const messageEndpoint = '/messages/';
class WitDriver {
constructor(options) {
const {
id,
serverToken
} = options;
if (!id || !serverToken) {
throw new Error(
'WitDriver could not be instantiated. Missing required options.'
);
}
this.queryURL = `${baseURL}${intentEndpoint}?v=${apiVersion}`;
this.messageInfoURL = messageId =>
`${baseURL}${messageEndpoint}${messageId}?v=${apiVersion}`;
this.requestHeaders = {
Authorization: `Bearer ${serverToken}`,
Accept: `application/vnd.wit.${apiVersion}+json`
};
}
Returns a Promise that returns the parsed object from the webservice. See https://wit.ai/docs/http/20160330#get-intent-via-text-link
query(text = '', verbose = false, n = 1) {
const url = `${this.queryURL}&q=${encodeURIComponent(text)}&verbose=${verbose}&n=${n}`;
return request({
url,
headers: this.requestHeaders
}).then(body => JSON.parse(body));
}
Returns a Promise that returns the parsed object from the webservice. See: https://wit.ai/docs/http/20160330#get-message-link
getMessage(id) {
const url = this.messageInfoURL(id);
return request({
url,
headers: this.requestHeaders
}).then(body => JSON.parse(body));
}
}
const options = {
id: 'YOUR_WIT_ID',
serverToken: 'YOUR_WIT_TOKEN'
};
const wit = new WitDriver(options);
luis.query('Hi')
.then(result => {
const {
_text,
outcomes
} = result;
console.log(_text); // 'Hi'
console.log(outcomes.length); // 1
});
See test/wit.js for more examples.
Gets the first entityName found.
Returns an entity object or null
const getEntity = (outcomes, entityName) => {
try {
return outcomes.entities[entityName][0];
} catch (e) {
return null;
}
};
WitDriver.getEntity = getEntity;
Gets all entities of name entityName.
Returns an array of entities or null
const getEntities = (outcomes, entityName) => {
try {
return outcomes.entities[entityName];
} catch (e) {
return null;
}
};
WitDriver.getEntities = getEntities;
Returns the value of the entity you look for or null
WitDriver.getEntityValue = (outcomes, entityName) => {
try {
return getEntity(outcomes, entityName).value;
} catch (e) {
return null;
}
};
Returns the metadata of the entity you look for or null
WitDriver.getEntityMeta = entity => {
try {
return JSON.parse(entity.metadata);
} catch (e) {
return null;
}
};
export { WitDriver };