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

Parameters

    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`
        };
    }

Methods

query

Parameters
return

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));
    }

getMessage

Parameters
return

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));
    }
}

Examples

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.

Static Methods

getEntity

Gets the first entityName found.

Parameters
return

Returns an entity object or null

const getEntity = (outcomes, entityName) => {
    try {
        return outcomes.entities[entityName][0];
    } catch (e) {
        return null;
    }
};

WitDriver.getEntity = getEntity;

getEntities

Gets all entities of name entityName.

Parameters
return

Returns an array of entities or null

const getEntities = (outcomes, entityName) => {
    try {
        return outcomes.entities[entityName];
    } catch (e) {
        return null;
    }
};

WitDriver.getEntities = getEntities;

getEntityValue

Parameters
return

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;
    }
};

getEntityMeta

Parameters
return

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 };
h