This repository was archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathbotkit-middleware-witai.js
More file actions
67 lines (57 loc) · 2.02 KB
/
botkit-middleware-witai.js
File metadata and controls
67 lines (57 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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) {
if (!config || !config.token) {
throw new Error('No wit.ai API token specified');
}
if (!config.minimum_confidence) {
config.minimum_confidence = 0.5;
}
var client = new Wit(config.token, actions);
var middleware = {};
middleware.receive = function(bot, message, next) {
// Only parse messages of type text and mention the bot (or when the bot is a web based bot where it's the only thing to talk to).
// Otherwise it would send every single message to wit (probably don't want that).
if (message.text && (bot._controller._config.adapter.name === 'Web Adapter' || message.text.indexOf(bot.identity.id) > -1)) {
client.message(message.text, function(error, data) {
if (error) {
next(error);
} else {
message.entities = data.entities;
next();
}
});
} else if (message.attachments) {
message.intents = [];
next();
} else {
next();
}
};
middleware.hears = function(tests, message) {
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.entities.intent[i].value == tests[t] &&
message.entities.intent[i].confidence >= config.minimum_confidence) {
return true;
}
}
}
}
return false;
};
return middleware;
};