-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
263 lines (215 loc) · 9.39 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
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
/**
Copyright 2014-2015 Amazon.com, Inc. or its affiliates. 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. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file 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 AlexaSkill = require('./AlexaSkill');
var Firebase = require('firebase');
var Async = require('async');
var cardsets;
var correctAnswer;
var counter = 0;
var whichCard, whichAnswerCorrect;
var APP_ID = undefined; //replace with 'amzn1.echo-sdk-ams.app.[your-unique-value-here]';
var Quizlexa = function () {
AlexaSkill.call(this, APP_ID);
};
// Extend AlexaSkill
Quizlexa.prototype = Object.create(AlexaSkill.prototype);
Quizlexa.prototype.constructor = Quizlexa;
Quizlexa.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
var speechText = "Welcome to Quizlexa. You can ask for a card from any of the sets that you've added on your web application.";
// If the user either does not reply to the welcome message or says something that is not
// understood, they will be prompted again with this text.
var repromptText = "For instructions on what you can say, please say help me.";
response.ask(speechText, repromptText);
};
Quizlexa.prototype.intentHandlers = {
"GetCardSet": function (intent, session, response) {
var cardSlot = intent.slots.CardStack,
cardSetName;
if (cardSlot && cardSlot.value) {
cardSetName = cardSlot.value.toLowerCase();
}
console.log("DEFINING CARDSETS");
cardsets = new Firebase('https://quizlet.firebaseio.com/' + cardSetName + '/');
console.log("GOING TO START CALLBACK STUFF");
function setCardset(callback) {
console.log("SETTING CARDSET");
return cardsets.once("value").then(function (data) {
var cardset = data.val();
callback(null, cardset);
});
}
function generateQuestion(cardset, callback) {
console.log("CALLBACK FUNCTION CALLED")
console.log(cardset.length)
var title, term, choiceA, choiceB;
whichCard = getRandomInt(0, cardset.length);
whichAnswerCorrect = getRandomInt(0, 100);
if (whichAnswerCorrect % 2 == 0) {
choiceA = cardset[whichCard].definition;
choiceB = cardset[((whichCard + 1) > (cardset.length - 1)) ? 0 : (whichCard + 1)].definition;
correctAnswer = "a";
}
else if (whichAnswerCorrect % 2 == 1) {
choiceA = cardset[((whichCard + 1) > (cardset.length - 1)) ? 0 : (whichCard + 1)].definition; //if i add and it exceeds the array limit, go down to zero.
choiceB = cardset[whichCard].definition;
correctAnswer = "b";
} else {
choiceA = "I AM ERROR.";
choiceB = "I AM BAGU."
correctAnswer = "a";
}
correctAnswer = (whichAnswerCorrect % 2 == 0) ? "a" : "b";
console.log("ANSWER SET");
title = cardset[whichCard].title;
term = cardset[whichCard].term;
questionOutput = {
speech: "The name of the cardset is: " + title + ". The front of the card says: " + term + ". What's probably on the other side? Is it A: " + choiceA + " or B: " + choiceB + ". If you're unsure of your answer, say I don't know.",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
repromptQuestion = {
speech: "Again, the front says " + term + " and the choices are A: " + choiceA + " or B: " + choiceB,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
console.log("ASKING QUESTION NOT ERROR");
response.ask(questionOutput, repromptQuestion);
callback();
}
var questionOutput, repromptQuestion;
if (cardsetTypes.indexOf(cardSetName) != -1) {
Async.waterfall([
setCardset,
generateQuestion
], function () {
console.log('done')
})
}
else {
var speech;
if (cardSetName) {
speech = "I'm sorry, I couldn't find a card set called " + cardSetName + ". Please check the web app to ensure that you have added a set of that name. What else can I help you with?";
} else {
speech = "I'm sorry, I couldn't find a card set. Please check the web app to ensure that you have added a set. What else can I help you with?";
}
questionOutput = {
speech: speech,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
repromptQuestion = {
speech: "What else can I help with?",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
console.log("ASKING ERROR QUESTION");
response.ask(questionOutput, repromptQuestion);
}
},
"PickAnswer": function (intent, session, response) {
var answerSlot = intent.slots.Answer,
currentChoice;
if (answerSlot && answerSlot.value) {
currentChoice = answerSlot.value;
}
console.log(currentChoice);
correctAnswer = (whichAnswerCorrect % 2 == 0) ? "a" : "b";
var continuePrompt, repromptContinue;
if (currentChoice == correctAnswer) {
continuePrompt = {
speech: "Correct! Nice Job. Want to continue?",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
}
repromptContinue = {
speech: "Do you want to continue?",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
}
response.ask(continuePrompt, repromptContinue);
}
else if (currentChoice.toLowerCase() == "i don't know") {
continuePrompt = {
speech: "Oh. That's too bad. You should've studied more. The correct answer was " + correctAnswer + ". Do you want to continue?",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
}
repromptContinue = {
speech: "Do you want to continue?",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
}
response.ask(continuePrompt, repromptContinue);
}
else {
continuePrompt = {
speech: "Correct! Nice Job. Do you want to continue?",
// speech: "Nope! You were incorrect. Do you want to continue?",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
}
repromptContinue = {
speech: "Do you want to continue?",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
}
response.ask(continuePrompt, repromptContinue);
}
},
"AMAZON.YesIntent": function (intent, session, response) {
var speechOutput = {
speech: "Okay. You can ask me for a card from any stack you have added using the web app.",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
}
var repromptText = {
speech: "Okay. You can ask me for a card from any stack you have added using the web app.",
type: AlexaSkill.speechOutputType.PLAIN_TEXT
}
response.ask(speechOutput, repromptText);
},
"AMAZON.NoIntent": function (intent, session, response) {
var speechOutput = "Thanks for using Quizlexa.";
response.tell(speechOutput);
},
"AMAZON.StopIntent": function (intent, session, response) {
var speechOutput = "Thanks for using Quizlexa.";
response.tell(speechOutput);
},
"AMAZON.CancelIntent": function (intent, session, response) {
var speechOutput = "Thanks for using Quizlexa.";
response.tell(speechOutput);
},
"AMAZON.HelpIntent": function (intent, session, response) {
var speechText = "You can ask for a card from a card set that you have added using the web app... Now, what can I help you with?";
var repromptText = "You can ask for a card from a card set that you have added using the web app... Now, what can I help you with?";
var speechOutput = {
speech: speechText,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
var repromptOutput = {
speech: repromptText,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
response.ask(speechOutput, repromptOutput);
}
};
function getRandomInt(min, max) { //max value is excluded
return Math.floor(Math.random() * (max - min)) + min;
}
var cardsetTypes =
[
'science',
'chemistry',
'biology',
'physics',
'philosophy',
'social studies',
'world history',
'US history',
'math',
'geometry',
'algebra',
'calculus',
'literature',
'general',
'hackathon'
]
exports.handler = function (event, context) {
var quizlexa = new Quizlexa();
quizlexa.execute(event, context);
};