-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
187 lines (173 loc) · 5.75 KB
/
test.js
File metadata and controls
187 lines (173 loc) · 5.75 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
'use strict';
const Alexa = require('alexa-sdk');
const request = require('request');
const APP_ID = 'amzn1.ask.skill.d19a5a3e-d8fb-4036-bf6b-90045a45d122';
const SKILL_NAME = 'Food Buddy';
const HELP_MESSAGE = 'Welcome to Food Buddy! you can use this skill to place an order';
const HELP_REPROMPT = 'How Can I help you Today?';
const STOP_MESSAGE = 'Goodbye Folks!';
const Intro = HELP_MESSAGE;
const handlers = {
'LaunchRequest': function () {
this.emit('Introduce');
},
'Introduce': function () {
const speechOutput = Intro;
this.response.cardRenderer(SKILL_NAME, speechOutput);
this.response.speak(speechOutput);
this.emit(':responseReady');
},
'GetMenuIntent': function () {
const request = require('request');
const url = `https://9nfmj2dq1f.execute-api.ap-south-1.amazonaws.com/Development/menu/get-all`;
let speechOutput = "Items on today's menu are : ";
var self=this;
new Promise(function(resolve, reject) {
request.get(url, (error, response, body) => {
let responseObj = JSON.parse(body);
if (!error)
{
resolve([responseObj,self]);
}
else{
reject(error);
}
});
}).then(function(a) {
let responseObj=a[0];
let self=a[1];
let arindex = responseObj.Menu_ITEMS.length;
let i;
for (i=0;i<arindex;i++) {
speechOutput = speechOutput + responseObj.Menu_ITEMS[i].ItemName;
speechOutput = speechOutput + " priced at rupees " + responseObj.Menu_ITEMS[i].Price;
if (i < (arindex-2)){
speechOutput = speechOutput + ", ";
}
else if (i == arindex-2){
speechOutput = speechOutput + " and ";
}
else if (i == arindex-1){
speechOutput = speechOutput + ".";
}
}
self.response.cardRenderer(SKILL_NAME, speechOutput);
self.response.speak(speechOutput);
self.emit(':responseReady');
}).catch();
},
'PlaceOrderIntent': function () {
const request = require('request');
const url1 = 'https://9nfmj2dq1f.execute-api.ap-south-1.amazonaws.com/Development/orders/add-order';
let speechOutput = "Order placed for ";
let filledSlots = delegateSlotCollection.call(this, function(event) {
let result = false;
let slots = event.request.intent.slots;
if(slots.menuItem.value) {
result = true;
}
return result;
});
if (!filledSlots) {
return;
}
let slotValues = getSlotValues(filledSlots);
speechOutput = speechOutput + slotValues.menuItem.resolved;
let item1=slotValues.menuItem.resolved;
let data1={
"IDNo":"1111",
"OrderDate": formatDate(new Date()),
"OrderedItems":item1
};
request.post({
url: url1,
body: JSON.stringify(data1)
}, function(error, response, body){
console.log(body);
});
this.response.cardRenderer(SKILL_NAME, speechOutput);
this.response.speak(speechOutput);
this.emit(':responseReady');
},
'ViewOrderIntent': function () {
},
'AMAZON.HelpIntent': function () {
const speechOutput = HELP_MESSAGE;
const reprompt = HELP_REPROMPT;
this.response.speak(speechOutput).listen(reprompt);
this.emit(':responseReady');
},
'AMAZON.CancelIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
'AMAZON.StopIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
};
function getSlotValues (filledSlots) {
let slotValues = {};
Object.keys(filledSlots).forEach(function(item) {
var name = filledSlots[item].name;
if(filledSlots[item]&&
filledSlots[item].resolutions &&
filledSlots[item].resolutions.resolutionsPerAuthority[0] &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code ) {
switch (filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
case "ER_SUCCESS_MATCH":
slotValues[name] = {
"synonym": filledSlots[item].value,
"resolved": filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.name,
"isValidated": true
};
break;
case "ER_SUCCESS_NO_MATCH":
slotValues[name] = {
"synonym": filledSlots[item].value,
"resolved": filledSlots[item].value,
"isValidated":false
};
break;
}
} else {
slotValues[name] = {
"synonym": filledSlots[item].value,
"resolved": filledSlots[item].value,
"isValidated": false
};
}
},this);
return slotValues;
}
function delegateSlotCollection(func) {
if(func) {
if (func(this.event)) {
this.event.request.dialogState = "COMPLETED";
return this.event.request.intent.slots;
}
}
if (this.event.request.dialogState === "STARTED") {
var updatedIntent = this.event.request.intent;
this.emit(":delegate", updatedIntent);
} else if (this.event.request.dialogState !== "COMPLETED") {
this.emit(":delegate", updatedIntent);
} else {
return this.event.request.intent.slots;
}
return null;
}
function formatDate(date) {
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return day + '-' + monthNames[monthIndex] + '-' + year.toString().slice(2,4);
}
exports.handler = function (event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};