npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wit-js

v0.0.1

Published

A modern Node wrapper for the Wit.ai API using Promises and Events

Downloads

15

Readme

Node integration for Wit.ai npm

wit-js is an alternative Node integration for Wit.ai using Promises and Events.

Install

In your Node.js project, run:

npm install --save wit-js

Quickstart

Run in your terminal:

let Wit = require('wit-js');

client = new Wit.Client({apiToken: '<your-api-token>'});

client.message('Hello!', {})
    .then((response) => {
        console.log(response.entities);
    })
    .catch((err) => {
        console.error(err);
    });

API

Overview

The Wit module provides a Client class with the following methods:

  • message - the Wit message API
  • converse - the low-level Wit converse API
  • run - a higher-level method to the Wit converse API
  • on - a simple method to bind to events fired by the client

Client class

The Client constructor requires a single options paramter, which may contain the following values:

  • apiToken - the access token of your Wit instance, required
  • apiVersion - the version of the Wit API you wish to targer, optional
  • apiRoot - the base URL for the API, useful for proxying or targeting other implementations optional

.message(...)

The Wit message API.

Takes the following parameters:

  • message - the text you want Wit.ai to extract the information from
  • context - (optional) the object representing the session state

Example:

client.message('Hello!', {})
    .then((response) => {
        console.log('Wit responded!');
        console.log(response.entities);
    })
    .catch((err) => {
        console.error(err);
    });
});

.run(...)

A higher-level method to the Wit converse API.

Takes the following parameters:

  • session - a unique identifier describing the user session
  • message - the text received from the user
  • context - the object representing the session state

This method runs recursively and acts upon actions retrieved from the API, fire the relevant events where necessary. Use the .on method of the client to subscribe to events.

Example:

client.on('msg', (data, context) => {
    console.log(`Wit said: ${data.msg}`);
});
client.run('my-session-id', 'what is the weather in London?', {});

.on(...)

Bind a listener to a Wit event. This interface is functionally identical to Node's EventEmitter.

Takes the following parameters:

  • event - the name of the event to which to bind
  • listener - the function to be called when the event is emitted

There are a total of 4 default events that will be emitted by the Client, they are:

  • merge - emitted as the first step of every bot action
  • msg - emitted when the bot engine "says" something
  • stop - the final action of the bot's "conversation", use this for any clean up
  • error - emitted if the bot engine encounters a problem

On top of these four, any actions configured within the bot engine will be emitted in the format action:<action-name>, for example action:fetch-weather.

Examples:

client.on('merge', (data, context) => {
    console.log(context);
});
client.on('action:query-database', (data, context) => {
    // maybe you are querying a mongo collection
    myCollection.find(context.query);
});

.converse(...)

The low-level Wit converse API.

Takes the following parameters:

  • session - a unique identifier describing the user session
  • message - the text received from the user
  • context - the object representing the session state

Example:


client.run('my-session-id', 'what is the weather in London?', {})
    .then((response) => {
        console.log('Wit responded!');
        console.log(response.entities);
    })
    .catch((err) => {
        console.error(err);
    });