-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (96 loc) · 3.42 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
const lex = require('./LexResponses');
const Lex = new lex();
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = async (event) => {
if (event.currentIntent && event.currentIntent.confirmationStatus) {
let confirmationStatus = event.currentIntent.confirmationStatus;
if (confirmationStatus == "Denied") {
console.log('got denied status');
let message = `Hope you have a good day`
return Lex.close({ message })
}
if (confirmationStatus == 'Confirmed') {
console.log('got confirmed status');
}
}
return handleProductFind(event);
}
const handleProductFind = event => {
let { slots } = event.currentIntent;
let { symptoms, dosage, medicines,age} = slots;
if (itemNumber) return getItem(slots);
if (!symptoms) {
let message = 'what is your problem?';
let intentName = 'medica';
let slotToElicit = 'symptoms';
return Lex.elicitSlot({ message, intentName, slotToElicit, slots })
}
if (!medicinetypes) {
let message = `What type of medicine do you need?`;
let intentName = 'medica';
let slotToElicit = 'medicines';
return Lex.elicitSlot({ message, intentName, slotToElicit, slots })
}
if (!age) {
let message = 'what is your age?';
let intentName = 'medica';
let slotToElicit = 'dosage';
return Lex.elicitSlot({ message, intentName, slotToElicit, slots })
}
if (!age === 'teen') {
let message = 'I prefer a moderate dosage';
let intentName = 'medica';
let slotToElicit = 'age';
return Lex.elicitSlot({ message, intentName, slotToElicit, slots })
}
if (!age === 'old') {
let message = 'I prefer a heavy dosage daily';
let intentName = 'medica';
let slotToElicit = 'age';
return Lex.elicitSlot({ message, intentName, slotToElicit, slots })
}
if (!age === 'child') {
let message = 'I prefer a less dosage daily';
let intentName = 'medica';
let slotToElicit = 'age';
return Lex.elicitSlot({ message, intentName, slotToElicit, slots })
}
return getItem(slots);
}
const getItem = async slots => {
console.log('slots', slots);
let { itemNumber, symptoms, medicinetypes, age} = slots;
let stock = await getStock();
let matching = stock.find(item =>
itemNumber === item.itemNumber ||
symptoms == item.symptoms &&
medicinetypes == item.medicinetypes &&
age == item.age &&
(item.symptoms == symptoms || item.medicinetype != 'medicinename'));
if (!matching) {
let message = `Unfortunately we couldn't find the item you were looking for`;
return Lex.close({ message })
}
if (matching.stock = name) {
let message = `I prefer you this medicine`;
let intentName = 'medica';
slots = { symptoms: null, medicinetypes: null, age: null, itemNumber: null };
return Lex.confirmIntent({ intentName, slots, message })
}
}
const getStock = () => {
var params = {
Bucket: 'mjmedical',
Key: `stock.json`
};
return new Promise((resolve, reject) => {
s3.getObject(params, function(err, data) {
if (err) { // an error occurred
reject(err)
} else { // successful response
resolve(JSON.parse(data.Body).stock)
}
});
})
}