Skip to content

Commit

Permalink
Added UserEvent handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffblankenburg committed Mar 5, 2021
1 parent 2540591 commit 8f891db
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 30 deletions.
8 changes: 4 additions & 4 deletions .ask/ask-states.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"default": {
"skillId": "amzn1.ask.skill.c63b121f-6049-4af8-9a4b-c6ad4778436a",
"skillMetadata": {
"lastDeployHash": "OKnM9s3jDNvbmtDk+dFUCRldItM="
"lastDeployHash": "uAUnQs9evl8eK4POqAyWvzRKmiE="
},
"code": {
"default": {
"lastDeployHash": "bl6HerZJ1D7tC8cOLd57lWT41KQ="
"lastDeployHash": "iSncsGyZa//YKsWayUxvXE3iua4="
}
},
"skillInfrastructure": {
Expand All @@ -17,8 +17,8 @@
"default": {
"lambda": {
"arn": "arn:aws:lambda:us-east-1:679180307287:function:TKOTrivia",
"lastModified": "2021-03-04T15:58:46.019+0000",
"revisionId": "881d5808-3fb9-48c0-92ff-eb95717e2f0f"
"lastModified": "2021-03-05T16:16:58.162+0000",
"revisionId": "baed70f8-9e88-4336-be2d-65d4b289c0bb"
},
"iamRole": "arn:aws:iam::679180307287:role/service-role/TKOTrivia-role-ynqzu5aj"
}
Expand Down
18 changes: 18 additions & 0 deletions lambda/achievement/CategoryCorrectCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const achievements = require("../data/achievements");
const getSpecificAchievement = require("../data/getSpecificAchievement");

async function CategoryCorrectCount(user, locale) {
const count = user.fields.CorrectCount;
const userId = user.fields.RecordId;
switch (count) {
case 1: return await getSpecificAchievement(achievements.CORRECT_ANSWERS_1, userId, locale);
case 5: return await getSpecificAchievement(achievements.CORRECT_ANSWERS_5, userId, locale);
case 10: return await getSpecificAchievement(achievements.CORRECT_ANSWERS_10, userId, locale);
case 25: return await getSpecificAchievement(achievements.CORRECT_ANSWERS_25, userId, locale);
case 50: return await getSpecificAchievement(achievements.CORRECT_ANSWERS_50, userId, locale);
case 100: return await getSpecificAchievement(achievements.CORRECT_ANSWERS_100, userId, locale);
default: return undefined;
}
}

module.exports = CategoryCorrectCount;
2 changes: 2 additions & 0 deletions lambda/achievement/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const AchievementCount = require("./AchievementCount");
const AnswerCount = require("./AnswerCount");
const CategoryCorrectCount = require("./CategoryCorrectCount");
const CorrectCount = require("./CorrectCount");
const SessionCount = require("./SessionCount");

module.exports = {
AchievementCount,
AnswerCount,
CategoryCorrectCount,
CorrectCount,
SessionCount
}
42 changes: 21 additions & 21 deletions lambda/apl/category_list_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,102 @@
"properties": {
"categories": [
{
"id": "art_and_stage",
"id": "recPlSUbmTneu7Bgk",
"source": "art_and_stage",
"name": "Art & Stage"
},
{
"id": "business_world",
"id": "rec59rQIzQoGa5J8f",
"source": "business_world",
"name": "Business"
"name": "Business World"
},
{
"id": "design",
"id": "recjlO0SAwy8jgUxh",
"source": "design",
"name": "Design"
},
{
"id": "film",
"id": "recyVJQXr4Ot362Un",
"source": "film",
"name": "Film"
},
{
"id": "food_and_drink",
"id": "recI6OQJSAbt9lfgk",
"source": "food_and_drink",
"name": "Food & Drink"
},
{
"id": "geography",
"id": "recySs74Y1TQ3CndJ",
"source": "geography",
"name": "Geography"
},
{
"id": "history",
"id": "recN73hdlt8vX8npV",
"source": "history",
"name": "History"
},
{
"id": "language",
"id": "recSjn8wl4vQfd1iD",
"source": "language",
"name": "Language"
},
{
"id": "literature",
"id": "recC0MmXiYFnwTJ0R",
"source": "literature",
"name": "Literature"
},
{
"id": "miscellaneous",
"id": "recGbtycCkGDlRaJI",
"source": "miscellaneous",
"name": "Miscellaneous"
},
{
"id": "music",
"id": "recbTxp5Zv8BAumlC",
"source": "music",
"name": "Music"
},
{
"id": "nature",
"id": "recpSc9Mr9tJjVmwY",
"source": "nature",
"name": "Nature"
},
{
"id": "people",
"id": "recc6ZrRIAMKVYOgY",
"source": "people",
"name": "People"
},
{
"id": "politics",
"id": "recoynCHkQBAOLXOc",
"source": "politics",
"name": "Politics"
},
{
"id": "science",
"id": "recMLWe8hXdlsWjNn",
"source": "science",
"name": "Science"
},
{
"id": "sports_and_games",
"id": "reca6CPDiAaqkvQPB",
"source": "sports_and_games",
"name": "Sports & Games"
},
{
"id": "technology",
"id": "recYo21mtcYqk0WrG",
"source": "technology",
"name": "Technology"
},
{
"id": "tradition_and_beliefs",
"id": "recNqo3O25J4rjd0B",
"source": "tradition_and_beliefs",
"name": "Tradition & Beliefs"
},
{
"id": "tv_and_radio",
"id": "recuvqDTuHALaaQic",
"source": "tv_and_radio",
"name": "TV & Radio"
},
{
"id": "united_states",
"id": "recrLqIHeQNU90Nry",
"source": "united_states",
"name": "United States"
}
Expand Down
21 changes: 21 additions & 0 deletions lambda/data/getSpecificCategory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const helper = require("../helper");
const fetch = require("node-fetch");
const keys = require("../keys");

async function getSpecificCategory(categoryId) {
const url = `https://api.airtable.com/v0/${keys.airtable_base_data}/Category?api_key=${keys.airtable_api_key}&filterByFormula=AND(RecordId%3D"${encodeURIComponent(categoryId)}")`;
const options = {
method: "GET",
};
//console.log(`FULL PATH ${url}`);

return fetch(url, options)
.then((res) => res.json())
.then((r) => {
//console.log({r});
const item = helper.getRandomItem(r.records);
return item;
});
}

module.exports = getSpecificCategory;
2 changes: 1 addition & 1 deletion lambda/data/getUserByUserId.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fetch = require("node-fetch");
const keys = require("../keys");

async function getUserByUserId(userId) {
const url = `https://api.airtable.com/v0/${keys.airtable_base_data}/User?api_key=${keys.airtable_api_key}&filterByFormula=AND(AlexaUserId%3D"${encodeURIComponent(userId)}")&fields%5B%5D=RecordId&fields%5B%5D=AlexaUserId&fields%5B%5D=QuestionCount&fields%5B%5D=Achievement&fields%5B%5D=AnswerCount&fields%5B%5D=CorrectCount&fields%5B%5D=AnsweredPercentage&fields%5B%5D=CorrectPercentage&fields%5B%5D=SessionCount&fields%5B%5D=FirstSession&fields%5B%5D=AchievementCount&fields%5B%5D=PersistedCategory`;
const url = `https://api.airtable.com/v0/${keys.airtable_base_data}/User?api_key=${keys.airtable_api_key}&filterByFormula=AND(AlexaUserId%3D"${encodeURIComponent(userId)}")&fields%5B%5D=RecordId&fields%5B%5D=AlexaUserId&fields%5B%5D=QuestionCount&fields%5B%5D=Achievement&fields%5B%5D=AnswerCount&fields%5B%5D=CorrectCount&fields%5B%5D=AnsweredPercentage&fields%5B%5D=CorrectPercentage&fields%5B%5D=SessionCount&fields%5B%5D=FirstSession&fields%5B%5D=AchievementCount&fields%5B%5D=PersistedCategory&fields%5B%5D=ArtStageCorrectCount&fields%5B%5D=ArtStageQuestionCount&fields%5B%5D=ArtStageCorrectPercentage`;
//console.log(`FULL PATH ${url}`);
const options = { method: "GET" };

Expand Down
2 changes: 2 additions & 0 deletions lambda/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const getCategoryList = require("./getCategoryList");
const getRandomQuestion = require("./getRandomQuestion");
const getRandomSpeech = require("./getRandomSpeech");
const getSpecificAchievement = require("./getSpecificAchievement");
const getSpecificCategory = require("./getSpecificCategory");
const getSpecificQuestion = require("./getSpecificQuestion");
const getSpecificQuiz = require("./getSpecificQuiz");
const getUserByUserId = require("./getUserByUserId");
Expand All @@ -32,6 +33,7 @@ module.exports = {
getRandomQuestion,
getRandomSpeech,
getSpecificAchievement,
getSpecificCategory,
getSpecificQuestion,
getSpecificQuiz,
getUserByUserId,
Expand Down
4 changes: 2 additions & 2 deletions lambda/handlers/AnswerIntent.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function AnswerIntent(handlerInput) {

sessionAttributes.user.fields.AnswerCount++;

const achievementSpeech = await data.getAchievementSpeech(sessionAttributes.user, helper.getLocale(handlerInput));
const achievementSpeech = await data.getAchievementSpeech(sessionAttributes.user, helper.getLocale(handlerInput), question.fields.Category[0]);

if (speakOutput === undefined) {
speakOutput = [outcomeSound, speechcon, answer, answerNote, achievementSpeech].join(" ");
Expand Down Expand Up @@ -96,7 +96,7 @@ async function AnswerIntent(handlerInput) {

return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.reprompt(actionQuery)
.addDirective(clearDirective)
.getResponse();

Expand Down
1 change: 1 addition & 0 deletions lambda/handlers/ErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const helper = require("../helper");

async function ErrorHandler(handlerInput, error) {
console.log(`~~~~ Error handled: ${error.stack}`);
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
const stack = await data.putError(sessionAttributes.user.fields.RecordId, error.stack);
const speakOutput = await data.getRandomSpeech(data.speechTypes.ERROR, helper.getLocale(handlerInput));

Expand Down
10 changes: 10 additions & 0 deletions lambda/handlers/UserEventHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const data = require("../data");
const helper = require("../helper");

async function UserEventHandler(handlerInput) {
const categoryId = handlerInput.requestEnvelope.request.arguments[0];
const question = await data.getRandomQuestion(categoryId, helper.getLocale(handlerInput));
return await data.askQuestion(question, handlerInput, data);
}

module.exports = UserEventHandler;
4 changes: 3 additions & 1 deletion lambda/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const SessionEndedRequest = require(`./SessionEndedRequest`);
const SpecificQuestionIntent = require("./SpecificQuestionIntent");
const StopIntent = require(`./StopIntent`);
const SubscriptionIntent = require("./SubscriptionIntent");
const UserEventHandler = require("./UserEventHandler");

module.exports = {
AnswerIntent,
Expand All @@ -41,5 +42,6 @@ module.exports = {
SessionEndedRequest,
SpecificQuestionIntent,
StopIntent,
SubscriptionIntent
SubscriptionIntent,
UserEventHandler
}
10 changes: 10 additions & 0 deletions lambda/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ const SubscriptionHandler = {
}
};

const UserEventHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === `Alexa.Presentation.APL.UserEvent`;
},
handle(handlerInput) {
return handlers.UserEventHandler(handlerInput);
}
};

const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === `SessionEndedRequest`;
Expand Down Expand Up @@ -239,6 +248,7 @@ exports.handler = Alexa.SkillBuilders.custom()
BuyCategoryHandler,
SubscriptionHandler,
CancelSubscriptionHandler,
UserEventHandler,
BuySuccessHandler,
BuyDeclinedHandler,
CategoryListIntentHandler,
Expand Down
12 changes: 11 additions & 1 deletion skill-package/interactionModels/custom/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
}
],
"samples": [
"where is {wrong}",
"who is {wrong}",
"where is {answer}",
"who is {answer}",
"the answer is {wrong}",
"it is {wrong}",
" {wrong}",
Expand All @@ -59,6 +63,11 @@
}
],
"samples": [
"give me a trivia question",
"trivia question",
"random question",
"random trivia",
"random",
"give me a {wrong} question",
"{wrong} question",
"i want a {wrong} question",
Expand All @@ -85,6 +94,7 @@
}
],
"samples": [
"give me question {question}",
"give me number {question}"
]
},
Expand Down Expand Up @@ -405,5 +415,5 @@
]
}
},
"version": "41"
"version": "50"
}

0 comments on commit 8f891db

Please sign in to comment.