Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from RobinJayaswal/pr/1
Browse files Browse the repository at this point in the history
Pr/1
  • Loading branch information
RobinJayaswal authored Aug 23, 2016
2 parents 6d33017 + fe976f9 commit cd8ecf4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 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.4",
"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.3.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.
48 changes: 31 additions & 17 deletions src/botkit-middleware-witai.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
var wit = require('node-wit');
var Wit = require('node-wit').Wit;

// not used at the moment
var actions = {
say: function(sessionId, context, message, cb) {
console.log(message);
cb();
},
merge: function(sessionId, context, entities, message, cb) {
cb(context);
},
error: function(sessionId, context, error) {
console.log(error.message);
}
};

module.exports = function(config) {

Expand All @@ -10,34 +24,36 @@ module.exports = function(config) {
config.minimum_confidence = 0.5;
}

var client = new Wit(config.token, actions);

var middleware = {};

middleware.receive = function(bot, message, next) {
if (message.text) {
wit.captureTextIntent(config.token, message.text, function(err, res) {
if (err) {
next(err);
// Only parse messages of type text and mention the bot.
// Otherwise it would send every single message to wit (probably don't want that).
if (message.text && message.text.indexOf(bot.identity.id) > -1) {
client.message(message.text, function(error, data) {
if (error) {
next(error);
} else {
console.log(JSON.stringify(res));
message.intents = res.outcomes;
message.entities = data.entities;
next();
}
});
}
else if (message.attachments) {
} else if (message.attachments) {
message.intents = [];
next();
} else {
next();
}

};

middleware.hears = function(tests, message) {

if (message.intents) {
for (var i = 0; i < message.intents.length; i++) {
if (message.entities && 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] &&
message.entities.intent[i].confidence >= config.minimum_confidence) {
return true;
}
}
Expand All @@ -47,7 +63,5 @@ module.exports = function(config) {
return false;
};


return middleware;

};

0 comments on commit cd8ecf4

Please sign in to comment.