Coder Social home page Coder Social logo

node-wit-bot-test's Introduction

Wit Node.js SDK npm

node-wit is the Node.js SDK for Wit.ai.

Install

In your Node.js project, run:

npm install --save node-wit

Quickstart

Run in your terminal:

node examples/template.js <your_token>

See examples folder for more examples.

API

Version change

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 setting the env variable WIT_API_VERSION.

{
  "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"
}

Overview

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

  • message - the Wit message API
  • converse - the low-level Wit converse API
  • runActions - a higher-level method to the Wit converse API
  • interactive - starts an interactive conversation with your bot

Wit class

The Wit constructor takes the following parameters:

  • token - the access token of your Wit instance
  • actions - the object with your actions
  • logger - (optional) the object handling the logging.

The actions object has action names as properties, and action implementations as values. You need to provide at least an implementation for the special actions say, merge and error.

A minimal actions object looks like this:

const actions = {
  say(sessionId, context, message, cb) {
    console.log(message);
    cb();
  },
  merge(sessionId, context, entities, message, cb) {
    cb(context);
  },
  error(sessionId, context, error) {
    console.log(error.message);
  },
};

A custom action takes the following parameters:

  • sessionId - a unique identifier describing the user session
  • context - the object representing the session state
  • cb(context) - a callback function to fire at the end of your action with the updated context.

Example:

const Wit = require('node-wit').Wit;
const client = new Wit(token, actions);

The logger object should implement the methods debug, log, warn and error. All methods take a single parameter message.

For convenience, we provide a Logger, taking a log level parameter (provided as logLevels). The following levels are defined: DEBUG, LOG, WARN, ERROR.

Example:

const Logger = require('node-wit').Logger;
const levels = require('node-wit').logLevels;
const Wit = require('node-wit').Wit;

const logger = new Logger(levels.DEBUG);
const client = new Wit(token, actions, logger);

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
  • cb(error, data) - a callback function with the JSON response

Example:

const context = {};
client.message('what is the weather in London?', context, (error, data) => {
  if (error) {
    console.log('Oops! Got an error: ' + error);
  } else {
    console.log('Yay, got Wit.ai response: ' + JSON.stringify(data));
  }
});

runActions

A higher-level method to the Wit converse API.

Takes the following parameters:

  • sessionId - a unique identifier describing the user session
  • message - the text received from the user
  • context - the object representing the session state
  • cb(error, context) - a callback function with the updated context
  • maxSteps - (optional) the maximum number of actions to execute (defaults to 5)

Example:

const session = 'my-user-session-42';
const context0 = {};
client.runActions(session, 'what is the weather in London?', context0, (e, context1) => {
  if (e) {
    console.log('Oops! Got an error: ' + e);
    return;
  }
  console.log('The session state is now: ' + JSON.stringify(context1));
  client.runActions(session, 'and in Brussels?', context1, (e, context2) => {
    if (e) {
      console.log('Oops! Got an error: ' + e);
      return;
    }
    console.log('The session state is now: ' + JSON.stringify(context2));
  });
});

converse

The low-level Wit converse API.

Takes the following parameters:

  • sessionId - a unique identifier describing the user session
  • message - the text received from the user
  • context - the object representing the session state
  • cb(error, data) - a callback function with the JSON response

Example:

client.converse('my-user-session-42', 'what is the weather in London?', {}, (error, data) => {
  if (error) {
    console.log('Oops! Got an error: ' + error);
  } else {
    console.log('Yay, got Wit.ai response: ' + JSON.stringify(data));
  }
});

interactive

Starts an interactive conversation with your bot.

Example:

client.interactive();

See the docs for more information.

Messenger integration example

This quickstart assumes that you have:

Install dependencies

npm install body-parser express node-fetch

Download and install ngrok

From here.

Run ngrok

./ngrok http 8445

This will provide your_ngrok_domain (the Forwarding line).

Run the example

export WIT_TOKEN=your_access_token
export FB_PAGE_ID=your_page_id
export FB_PAGE_TOKEN=your_page_token
export FB_VERIFY_TOKEN=any_token
node examples/messenger.js

Subscribe your page to Messenger Webhooks

Using your FB_VERIFY_TOKEN and https://<your_ngrok_domain>/fb as callback URL.

See the Messenger Platform docs.

Talk to your bot on Messenger!

node-wit-bot-test's People

Contributors

amowu avatar blandinw avatar diegorbaquero avatar duffmeister avatar glavin001 avatar gleuch avatar jedireza avatar keyvanakbary avatar patapizza avatar ptab avatar ush189 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.