-
Notifications
You must be signed in to change notification settings - Fork 255
/
index.js
161 lines (139 loc) · 5.34 KB
/
index.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var debug = require('debug')('watson-middleware:index');
var Promise = require('bluebird');
var ConversationV1 = require('watson-developer-cloud/conversation/v1');
var watsonUtils = require('./utils');
var readContext = Promise.promisify(watsonUtils.readContext);
var updateContext = Promise.promisify(watsonUtils.updateContext);
var postMessage = Promise.promisify(watsonUtils.postMessage);
// These are initiated by Slack itself and not from the end-user. Won't send these to WCS.
var ignoreType = ['presence_change', 'reconnect_url'];
module.exports = function(config) {
if (!config) {
throw new Error('Watson Conversation config parameters absent.');
}
var middleware = {
minimum_confidence: (config.minimum_confidence || 0.75),
hear: function(patterns, message) {
if (message.watsonData && message.watsonData.intents) {
for (var p = 0; p < patterns.length; p++) {
for (var i = 0; i < message.watsonData.intents.length; i++) {
if (message.watsonData.intents[i].intent === patterns[p] &&
message.watsonData.intents[i].confidence >= middleware.minimum_confidence) {
return true;
}
}
}
}
return false;
},
before: function(message, payload, callback) {
callback(null, payload);
},
after: function(message, response, callback) {
callback(null, response);
},
interpret: function(bot, message, callback) {
var before = Promise.promisify(middleware.before);
var after = Promise.promisify(middleware.after);
if (!middleware.conversation) {
debug('Creating Conversation object with parameters: ' + JSON.stringify(config, 2, null));
middleware.conversation = new ConversationV1(config);
}
if (!message.text || ignoreType.indexOf(message.type) !== -1 || message.reply_to || message.bot_id) {
// Ignore messages initiated by Slack. Reply with dummy output object
message.watsonData = {
output: {
text: []
}
};
callback();
}
middleware.storage = bot.botkit.storage;
readContext(message.user, middleware.storage).then(function(userContext) {
var payload = {
workspace_id: config.workspace_id,
input: {
text: message.text
}
};
if (userContext) {
payload.context = userContext;
}
return payload;
}).then(function(payload) {
return before(message, payload);
}).then(function(watsonRequest) {
return postMessage(middleware.conversation, watsonRequest);
}).then(function(watsonResponse) {
return after(message, watsonResponse);
}).then(function(watsonResponse) {
message.watsonData = watsonResponse;
return updateContext(message.user, middleware.storage, watsonResponse);
}).catch(function(error) {
debug('Error: %s', JSON.stringify(error, null, 2));
}).done(function(response) {
callback(null);
});
},
receive: function(bot, message, next) {
var before = Promise.promisify(middleware.before);
var after = Promise.promisify(middleware.after);
if (!middleware.conversation) {
debug('Creating Conversation object with parameters: ' + JSON.stringify(config, null, 2));
middleware.conversation = new ConversationV1(config);
}
if (!message.text || ignoreType.indexOf(message.type) !== -1 || message.reply_to || message.bot_id) {
// Ignore messages initiated by Slack. Reply with dummy output object
message.watsonData = {
output: {
text: []
}
};
next();
}
middleware.storage = bot.botkit.storage;
readContext(message.user, middleware.storage).then(function(userContext) {
var payload = {
workspace_id: config.workspace_id,
input: {
text: message.text
}
};
if (userContext) {
payload.context = userContext;
}
return payload;
}).then(function(payload) {
return before(message, payload);
}).then(function(watsonRequest) {
return postMessage(middleware.conversation, watsonRequest);
}).then(function(watsonResponse) {
return after(message, watsonResponse);
}).then(function(watsonResponse) {
message.watsonData = watsonResponse;
return updateContext(message.user, middleware.storage, watsonResponse);
}).catch(function(error) {
debug('Error: %s', JSON.stringify(error, Object.getOwnPropertyNames(error), 2));
}).done(function(response) {
next();
});
}
};
debug('Middleware: ' + JSON.stringify(middleware, null, 2));
return middleware;
};