Skip to content

Commit

Permalink
Update middleware to look for intent entity
Browse files Browse the repository at this point in the history
  • Loading branch information
dfischer committed May 6, 2016
1 parent 9f7d49e commit f6117c6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
.env
.idea
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botkit-middleware-witai",
"version": "0.0.2",
"version": "0.0.3",
"description": "A middleware for using Wit.ai in a Botkit-powered bot",
"main": "src/botkit-middleware-witai.js",
"scripts": {
Expand All @@ -23,7 +23,7 @@
},
"homepage": "https://github.com/howdyai/botkit-middleware-witai#readme",
"dependencies": {
"node-wit": "^2.0.0"
"node-wit": "^3.2.2"
},
"devDependencies": {
"jscs": "^2.8.0"
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ Using the Wit hears middleware tells Botkit to look for Wit.ai intents
information, and match using this information instead of the built in
pattern matching function.

You must make a an `intent` entity in the understandings area of wit.ai
and train it to register certain expressions.

I.e "intent" -> "weather"

Expression: "What is the weather?" and that maps to the weather intent.

Unless you want to directly access the information returned by wit,
you can use this transparently by enabling bot the `receive` and `hears`
middlewares.
30 changes: 17 additions & 13 deletions src/botkit-middleware-witai.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
var wit = require('node-wit');
'use strict';
const Logger = require('node-wit').Logger;
const levels = require('node-wit').logLevels;
const Wit = require('node-wit').Wit;

const logger = new Logger(levels.DEBUG);

module.exports = function(config) {

Expand All @@ -10,30 +15,29 @@ module.exports = function(config) {
config.minimum_confidence = 0.5;
}

var middleware = {};
const client = new Wit(config.token, actions, logger);

const middleware = {};

middleware.receive = function(bot, message, next) {
if (message.text) {
wit.captureTextIntent(config.token, message.text, function(err, res) {
if (err) {
next(err);
client.message(message.text, (error, data) => {
if (error) {
next(error);
} else {
console.log(JSON.stringify(res));
message.intents = res.outcomes;
// no support for multiple outcomes, is it needed for this?
message.entities = data.outcomes[0].entities;
next();
}
});
}

};

middleware.hears = function(tests, message) {

if (message.intents) {
for (var i = 0; i < message.intents.length; i++) {
if (message.entities.intent) {
for (var i = 0; i < message.entities.intent.length; i++) {
for (var t = 0; t < tests.length; t++) {
if (message.intents[i].intent == tests[t] &&
message.intents[i].confidence >= config.minimum_confidence) {
if (message.entities.intent[i].value == tests[t]) {
return true;
}
}
Expand Down

0 comments on commit f6117c6

Please sign in to comment.