-
Notifications
You must be signed in to change notification settings - Fork 0
/
LF2.js
377 lines (355 loc) · 12.1 KB
/
LF2.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
var sqs = new AWS.SQS();
var https = require('https');
var ses = new AWS.SES({ region: "us-east-1" });
//var async = require('asyncawait/async');
//var await = require('asyncawait/await');
//console.log("Hi");
var data_got;
//const ddbv3 = new DocumentClient();
// var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
//const { curl } = rvequire('node-libcurl');
//const curlobj = new curl();
// async function ddb(restaurant_id){
// return new Promise((resolve,reject) => {
// try{
// var docClient = new AWS.DynamoDB.DocumentClient();
// var params2 = {
// TableName : "yelp-restaurants",
// KeyConditionExpression:"id = :id1",
// ExpressionAttributeValues: {
// ":id1": { S : String(restaurant_id) }
// //":id1" : String(restaurant_id)
// },
// // ExpressionAttributeNames:{'#id':'id' }
// //ProjectionExpression: 'display_phone,'
// };
// docClient.query(params2,function(d)
// {
// console.log(d);
// });
// }
// catch(err)
// {
// console.log("error in ddb2");
// }
// } );
// }
function httprequest(cuisine) {
//console.log(cuisine);
return new Promise((resolve, reject) => {
const options = {
host: 'search-yelp-restaurantid-cuisine-ncxoslfjfg4blt6q7atwdtbsoi.us-east-1.es.amazonaws.com',
//path: '/restaurants/Restaurant/_search?q=cuisine:japanese&size=100',
path:'/restaurants/Restaurant/_search?q=cuisine:'+ cuisine+ '&size=1000',
port: 443,
method: 'GET',
// auth : {
// username: 'assignment1',
// password: 'Assignment1$'
// }
headers: {
'Authorization': 'Basic ' + new Buffer('assignment1' + ':' + 'Assignment1$').toString('base64')
}
};
const req = https.request(options, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch(e) {
reject(e);
}
resolve(body);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
});
}
exports.handler = async (event) => {
var params = {
"QueueUrl":"https://sqs.us-east-1.amazonaws.com/592360254578/DiningSuggestionsIntentInfo.fifo",
"MaxNumberOfMessages":1
};
//receive message
var received_data;//array of objs
var sqshandle = await sqs.receiveMessage(params, function(err, data) {
if (err) {
console.log(err, err.stack);
}// an error occurred
else {
received_data = data;
//console.log(data); // successful response
}
}).promise();
console.log(sqshandle);
// return {
// "data":received_data
// };
if(received_data !== undefined)
var message_arr = received_data["Messages"];
//receive one message everytime
if(message_arr)
var message = JSON.parse(message_arr[0]["Body"]);
// return {
// "message":JSON.parse(message)
// };
var user_selected_cuisine;
if(message){
user_selected_cuisine = message["Cuisine"];
}
console.log(message);
//import {DocumentClient} from "@aws-sdk/client-dynamodb";
// return {
// user_selected_cuisine
// };
//get a random restaurant from elastic search based on user selected cuisine
// var received_message;
// await http.request("https://search-yelp-restaurantid-cuisine-ncxoslfjfg4blt6q7atwdtbsoi.us-east-1.es.amazonaws.com/restaurants/Restaurant/_search?q=cuisine:japanese",(res) => {
// const { statusCode } = res;
// // if(statusCode == 200)
// // {
// //success
// res.on('end',function(mes)
// {
// received_message = mes;
// });
// // res.on('error',function(err)
// // {
// // console.log("Error elastic search");
// // });
// // }
// });
// console.log(received_message);
//need to delete message from the queue
//start - 1
return httprequest(user_selected_cuisine).then((data) => {
const response = {
statusCode: 200,
body: JSON.stringify(data),
};
var body = JSON.parse(response["body"]);
var array_hits = body["hits"]["hits"];
//randomly select a restaurant from the hits
var random_num = Math.floor(Math.random() * (array_hits.length));
//get restaurant id of this random restaurant
var restaurant_id = array_hits[random_num]['_source']['id'];
console.log(restaurant_id);
//query dynamoDB
// var params2 = {
// TableName : "yelp-restaurants",
// KeyConditionExpression:"id = :id1",
// ExpressionAttributeValues: {
// ":id1": { S : String(restaurant_id) }
// //":id1" : String(restaurant_id)
// },
// //ProjectionExpression: 'display_phone,'
// };
console.log(restaurant_id);
//const doc = new AWS.DynamoDB.DocumentClient();
// var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
// dynamodb.query(params,function(err,data){
// if(err)
// {
// console.log(typeof err);
// }
// else{
// console.log(typeof data);
// }
// }).promise();
// var data_restaurant;
// console.log("here");
// dynamodb.query(params,function(err,data){
// if(err)
// {
// console.log("error dynamodb method");
// //console.log(err,err.stack);
// }
// else{
// data_restaurant = data.Items;
// console.log("success dynamodb");
// //console.log(data.Items);
// }
// }).promise();
// console.log(data_restaurant);
// return data_restaurant;
//return dynamodb;
// var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
var params2 = {
TableName : "yelp-restaurants",
KeyConditionExpression:"id = :id1",
ExpressionAttributeValues: {
":id1": String(restaurant_id)//"ZrzSDDj54aUlPqn4MQMKeQ"
//{ S : 'ZrzSDDj54aUlPqn4MQMKeQ'}//String(restaurant_id) }
//":id1" : String(restaurant_id)
},
ScanIndexForward:false
//ExpressionAttributeNames:{'#id':'id' }
//ProjectionExpression: 'display_phone,'
};
//trying scan
// var params_scan = {
// TableName : 'yelp-restaurants',
// FilterExpression : 'id = :this_id',
// ExpressionAttributeValues : {':this_id' : 'ZrzSDDj54aUlPqn4MQMKeQ'}
// };
var documentClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10','region':'us-east-1' });
var received_random_restaurant_details;
//console.log(documentClient);
const promise = new Promise(function(resolve,reject){
documentClient.query(params2,function(err,data){
//console.log("resolving now");
//console.log(data);
if(err)
{
reject("Error reject");
}
else{
received_random_restaurant_details = data;
console.log(received_random_restaurant_details["Items"][0]["location"]["display_address"]);
//compose message with received details
var email_body_one = "Hi, Thank you for using the Dining Chatbot. Here is the restaurant recommendation\n";
var restaurant_name = received_random_restaurant_details["Items"][0]["name"];
var restaurant_address = received_random_restaurant_details["Items"][0]["location"]["display_address"];
var restaurant_phone = received_random_restaurant_details["Items"][0]["display_phone"];
var email_body_two = String(restaurant_name) + ", " + JSON.stringify(restaurant_address) + ", "+String(restaurant_phone);
var email_body = email_body_one+email_body_two;
console.log(email_body);
var email_address = String(message['EmailAddress']);
console.log(email_address);
//ses
var params_email = {
Destination: {
ToAddresses: [email_address],
},
Message: {
Body: {
Text: { Data: email_body },
},
Subject: { Data: "Restaurant Recommendation" },
},
Source: "[email protected]"
};
//send email
//console.log(ses);
//******
// var promise_email = ses.sendEmail(params_email,function(err,data){
// if(err)
// {
// console.log("Error while sending email");
// }
// else{
// //console.log(data);
// console.log("email was sent successfully");
// }
// }).promise();
// console.log(promise_email);
// promise_email.then(function(data){
// console.log(data);
// return
// }).catch(function(err){
// console.error(err,err.stack);
// })
//**********
const promise_email = new Promise(function(resolve,reject){
ses.sendEmail(params_email,function(err,data){
if(err)
{
reject("Error email sending reject");
console.log(err);
}
else{
//console.log(data);
resolve("email sent successfully");
console.log(data);
}
})
});
promise_email.then(function(msg)
{
console.log(msg);
//delete messages from the queue - get message handle from above
if(sqshandle.Messages)
{
var delete_param = {
QueueUrl:"https://sqs.us-east-1.amazonaws.com/592360254578/DiningSuggestionsIntentInfo.fifo",
ReceiptHandle:sqshandle.Messages[0].ReceiptHandle
};
const delete_promise = new Promise(function(resolve,reject)
{
sqs.deleteMessage(delete_param,function(err,data)
{
if(err)
{
reject(err);
console.log("error deleteing message");
//console.log(err);
}
else
{
resolve(data);
console.log("delete message successfully");
//console.log(data);
}
});
});
delete_promise.then(function(data)
{
console.log(data);
}).catch(function(err)
{
console.log(err);
});
}
//return promise;
}).catch(function(erro)
{
console.log(erro);
});
// return ses.sendEmail(params).promise()
//temp - comment - start
//resolve(received_random_restaurant_details);
//temp - comment - end
}
// });
// }).on('error',(e) => {
// console.log("error now");
// reject((e))
});
});
//temp - comment - start
return promise;
//temp - comment - end
// documentClient.scan(params_scan, function(err, data) {
// if (err) console.log(err);
// else console.log(data);
// });
// await documentClient.scan(params_scan).promise();
// }).promise();
// console.log("outside");
// docClient.query(params2).promise().then(function(obj) {
// console.log("indise");
// console.log(obj);
// },function(er){
// console.log("inside error here");
// });
//const data2 = docClient.scan(params).promise()
//console.log(ddbv3);
// await ddb('ZrzSDDj54aUlPqn4MQMKeQ');
// return {
// "test":"hey"
// };
});
//end - 1
};