-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
248 lines (187 loc) · 6.67 KB
/
app.js
File metadata and controls
248 lines (187 loc) · 6.67 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Copyright 2015 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.
*/
'use strict';
var extend = require('extend');
var Promise = require('bluebird');
//var conversation = require('./lib/conversation');
var weather = require('./lib/weather');
var NaturalLanguageUnderstanding = require('./lib/natural-language-understanding');
var cloudant = require('./lib/cloudant');
var format = require('string-template');
var pick = require('object.pick');
var counter = require('./lib/counter');
//var sendMessageToConversation = Promise.promisify(conversation.message.bind(conversation));
var getUser = Promise.promisify(cloudant.get.bind(cloudant));
var saveUser = Promise.promisify(cloudant.put.bind(cloudant));
var extractCity = Promise.promisify(NaturalLanguageUnderstanding.extractCity.bind(NaturalLanguageUnderstanding));
var getForecast = Promise.promisify(weather.forecastByGeoLocation.bind(weather));
var getGeoLocation = Promise.promisify(weather.geoLocation.bind(weather));
var express = require('express'); // app server
var bodyParser = require('body-parser'); // parser for post requests
var Conversation = require('watson-developer-cloud/conversation/v1'); // watson sdk
var app = express();
// Bootstrap application settings
app.use(express.static('./public')); // load UI from public folder
app.use(bodyParser.json());
// Create the service wrapper
var conversation = new Conversation({
username: process.env.CONVERSATION_USERNAME,
password: process.env.CONVERSATION_PASSWORD,
version_date: '2016-07-01',
path: {
workspace_id: process.env.WORKSPACE_ID
}
});
// Endpoint to be call from the client side
app.post('/api/message', function(req, res) {
var workspace = process.env.WORKSPACE_ID || '<workspace-id>';
if (!workspace || workspace === '<workspace-id>') {
return res.json({
'output': {
'text': 'The app has not been configured with a <b>WORKSPACE_ID</b> environment variable. Please refer to the ' + '<a href="https://github.com/watson-developer-cloud/conversation-simple">README</a> documentation on how to set this variable. <br>' + 'Once a workspace has been defined the intents may be imported from ' + '<a href="https://github.com/watson-developer-cloud/conversation-simple/blob/master/training/car_workspace.json">here</a> in order to get a working application.'
}
});
}
var payload = {
context: req.body.context || {},
input: req.body.input || {},
user :req.body.user
};
// Send the input to the conversation service
conversation.message(payload, function(err, data) {
if (err) {
return res.status(err.code || 500).json(err);
}
updateMessage(payload, data).then(function(message){
// console.log(message);
if(message.order){
data.output.text += message.order;
}
if(message.menu){
data.output.text+=message.menu;
}
return res.json(data);
});
});
});
/**
* Updates the response text using the intent confidence
* @param {Object} input The request to the Conversation service
* @param {Object} response The response from the Conversation service
* @return {Object} The response with the updated message
*/
function updateMessage(input, response) {
var responseText = {};
var order = {};
var usere ;
var dbUser;
var menu = {"Pizza" : ["Sausage" ,"Pepperoni" , "Spinach and Tomato" ] ,
"Dessert" : [ "Chocolate Brownie"], "Drink" : [ "Coke" ,"Pepsi"],
"salad" : ["Garden Salad" , "Ceasers Salad"] };
if (!response.output) {
response.output = {};
}
var user = input.user ;
//console.log(input);
return getUser(user).then(function(dbUser) {
if(dbUser)
{order = dbUser.order;
usere =dbUser;
;}
return order;
}).
then(function(odd){
if (response.intents && response.intents[0]) {
var intent = response.intents[0];
console.log(response.context.menu);
console.log(response.context.order);
console.log(intent.intent);
if(intent.intent == "menu" && response.context.order==undefined)
{ var d = "";
console.log("here");
if(response.context.menu!=undefined){
menu[response.context.menu].forEach(function(e){
d = d+"," + e;
});
}
else
{
d = JSON.stringify(menu);
}
delete response.context.menu;
return d ;
}
}
})
.then(function(menud){
if(menud)
{responseText["menu"] = menud;}
if (response.intents && response.intents[0]) {
var intent = response.intents[0];
if(intent.intent =="order" || response.context.order)
{
delete response.context.order;
return counter.get_cts(input.input.text);
}
}
}) .then(function(order_rec){
if(order_rec)
{ Object.keys(order_rec).forEach(function(key){
if(order&&order.hasOwnProperty(key))
{
order[key] = order[key] + order_rec[key];
}
else
{
var temp ={};
temp[key] = order_rec[key];
extend(order , temp);
}
});
}
return order;
}) .then(function(ord)
{ if(Object.keys(ord).length!=0){
responseText["order"] = JSON.stringify(order);
}
if (response.intents && response.intents[0]) {
var intent = response.intents[0];
if(intent.intent == "done")
{
order = {};
delete response.context.order;
}
}
}).
then(function(){
if(usere){
//console.log(usere);
// console.log("hereEW");
usere.order = order;
return saveUser(usere);
}
else{ //console.log("hereE");
var newuser = { _id : user , 'order': order };
return saveUser(newuser);
}
}) .then(function(saved){
//console.log("sofar");
responseText["output"]={};
responseText["output"]["text"] = response.output.text;
return responseText;
}) ; //current then
}
module.exports = app;