forked from smazon/chatbotTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
executable file
·195 lines (188 loc) · 6.72 KB
/
bot.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* This file contains all of the web and hybrid functions for interacting with
* Ana and the Watson Conversation service. When API calls are not needed, the
* functions also do basic messaging between the client and the server.
*
*
*/
var watson = require('watson-developer-cloud');
var CONVERSATION_NAME = "Conversation-Demo"; // conversation name goes here.
var fs = require('fs');
// load local VCAP configuration
var appEnv = null;
var conversationWorkspace, conversation;
// =====================================
// CREATE THE SERVICE WRAPPER ==========
// =====================================
// Create the service wrapper
conversation = watson.conversation({
url: "https://gateway.watsonplatform.net/conversation/api"
, username: "<username>"
, password: "<password>"
, version_date: '2017-04-10'
, version: 'v1'
});
// check if the workspace ID is specified in the environment
conversationWorkspace = "<workspace_id>";
// if not, look it up by name or create one
// Allow clients to interact
var chatbot = {
sendMessage: function (req, callback) {
// var owner = req.user.username;
buildContextObject(req, function (err, params) {
if (err) {
console.log("Error in building the parameters object: ", err);
return callback(err);
}
if (params.message) {
var conv = req.body.context.conversation_id;
var context = req.body.context;
var res = {
intents: []
, entities: []
, input: req.body.text
, output: {
text: params.message
}
, context: context
};
// chatLogs(owner, conv, res, () => {
// return
callback(null, res);
// });
}
else if (params) {
// Send message to the conversation service with the current context
conversation.message(params, function (err, data) {
if (err) {
console.log("Error in sending message: ", err);
return callback(err);
}else{
var conv = data.context.conversation_id;
console.log("Got response from Ana: ", JSON.stringify(data));
// if (data.context.system.dialog_turn_counter > 1) {
// chatLogs(owner, conv, data, () => {
// return callback(null, data);
// });
// }
// else {
return callback(null, data);
// }
}
});
}
});
}
};
// ===============================================
// LOG MANAGEMENT FOR USER INPUT FOR ANA =========
// ===============================================
function chatLogs(owner, conversation, response, callback) {
console.log("Response object is: ", response);
// Blank log file to parse down the response object
var logFile = {
inputText: ''
, responseText: ''
, entities: {}
, intents: {}
, };
logFile.inputText = response.input.text;
logFile.responseText = response.output.text;
logFile.entities = response.entities;
logFile.intents = response.intents;
logFile.date = new Date();
var date = new Date();
var doc = {};
Logs.find({
selector: {
'conversation': conversation
}
}, function (err, result) {
if (err) {
console.log("Couldn't find logs.");
callback(null);
}
else {
doc = result.docs[0];
if (result.docs.length === 0) {
console.log("No log. Creating new one.");
doc = {
owner: owner
, date: date
, conversation: conversation
, lastContext: response.context
, logs: []
};
doc.logs.push(logFile);
Logs.insert(doc, function (err, body) {
if (err) {
console.log("There was an error creating the log: ", err);
}
else {
console.log("Log successfull created: ", body);
}
callback(null);
});
}
else {
doc.lastContext = response.context;
doc.logs.push(logFile);
Logs.insert(doc, function (err, body) {
if (err) {
console.log("There was an error updating the log: ", err);
}
else {
console.log("Log successfull updated: ", body);
}
callback(null);
});
}
}
});
}
// ===============================================
// UTILITY FUNCTIONS FOR CHATBOT AND LOGS ========
// ===============================================
/**
* @summary Form the parameter object to be sent to the service
*
* Update the context object based on the user state in the conversation and
* the existence of variables.
*
* @function buildContextObject
* @param {Object} req - Req by user sent in POST with session and user message
*/
function buildContextObject(req, callback) {
var message = req.body.text;
// var userTime = req.body.user_time;
var context;
if (!message) {
message = '';
}
// Null out the parameter object to start building
var params = {
workspace_id: conversationWorkspace
, input: {}
, context: {}
};
if (req.body.context) {
context = req.body.context;
params.context = context;
}
else {
context = '';
}
// Set parameters for payload to Watson Conversation
params.input = {
text: message // User defined text to be sent to service
};
// This is the first message, add the user's name and get their healthcare object
// if ((!message || message === '') && !context) {
// params.context = {
// fname: req.user.fname
// , lname: req.user.lname
// };
// }
return callback(null, params);
}
module.exports = chatbot;