diff --git a/.ask/ask-states.json b/.ask/ask-states.json index 9234726..6ee30e9 100644 --- a/.ask/ask-states.json +++ b/.ask/ask-states.json @@ -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": { @@ -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" } diff --git a/lambda/achievement/CategoryCorrectCount.js b/lambda/achievement/CategoryCorrectCount.js new file mode 100644 index 0000000..6812d1b --- /dev/null +++ b/lambda/achievement/CategoryCorrectCount.js @@ -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; \ No newline at end of file diff --git a/lambda/achievement/index.js b/lambda/achievement/index.js index 20f80f5..57e4238 100644 --- a/lambda/achievement/index.js +++ b/lambda/achievement/index.js @@ -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 } \ No newline at end of file diff --git a/lambda/apl/category_list_data.json b/lambda/apl/category_list_data.json index e0d4637..18efab7 100644 --- a/lambda/apl/category_list_data.json +++ b/lambda/apl/category_list_data.json @@ -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" } diff --git a/lambda/data/getSpecificCategory.js b/lambda/data/getSpecificCategory.js new file mode 100644 index 0000000..66541f3 --- /dev/null +++ b/lambda/data/getSpecificCategory.js @@ -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; diff --git a/lambda/data/getUserByUserId.js b/lambda/data/getUserByUserId.js index f73bb07..f9879e1 100644 --- a/lambda/data/getUserByUserId.js +++ b/lambda/data/getUserByUserId.js @@ -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" }; diff --git a/lambda/data/index.js b/lambda/data/index.js index 53b0e8d..b16bace 100644 --- a/lambda/data/index.js +++ b/lambda/data/index.js @@ -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"); @@ -32,6 +33,7 @@ module.exports = { getRandomQuestion, getRandomSpeech, getSpecificAchievement, + getSpecificCategory, getSpecificQuestion, getSpecificQuiz, getUserByUserId, diff --git a/lambda/handlers/AnswerIntent.js b/lambda/handlers/AnswerIntent.js index 3a4f794..3e354ca 100644 --- a/lambda/handlers/AnswerIntent.js +++ b/lambda/handlers/AnswerIntent.js @@ -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(" "); @@ -96,7 +96,7 @@ async function AnswerIntent(handlerInput) { return handlerInput.responseBuilder .speak(speakOutput) - .reprompt(speakOutput) + .reprompt(actionQuery) .addDirective(clearDirective) .getResponse(); diff --git a/lambda/handlers/ErrorHandler.js b/lambda/handlers/ErrorHandler.js index 3be3f22..b551fbd 100644 --- a/lambda/handlers/ErrorHandler.js +++ b/lambda/handlers/ErrorHandler.js @@ -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)); diff --git a/lambda/handlers/UserEventHandler.js b/lambda/handlers/UserEventHandler.js new file mode 100644 index 0000000..c1b6720 --- /dev/null +++ b/lambda/handlers/UserEventHandler.js @@ -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; \ No newline at end of file diff --git a/lambda/handlers/index.js b/lambda/handlers/index.js index 0223343..a46d404 100644 --- a/lambda/handlers/index.js +++ b/lambda/handlers/index.js @@ -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, @@ -41,5 +42,6 @@ module.exports = { SessionEndedRequest, SpecificQuestionIntent, StopIntent, - SubscriptionIntent + SubscriptionIntent, + UserEventHandler } \ No newline at end of file diff --git a/lambda/index.js b/lambda/index.js index 5eca6c8..ceed1e8 100644 --- a/lambda/index.js +++ b/lambda/index.js @@ -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`; @@ -239,6 +248,7 @@ exports.handler = Alexa.SkillBuilders.custom() BuyCategoryHandler, SubscriptionHandler, CancelSubscriptionHandler, + UserEventHandler, BuySuccessHandler, BuyDeclinedHandler, CategoryListIntentHandler, diff --git a/skill-package/interactionModels/custom/en-US.json b/skill-package/interactionModels/custom/en-US.json index 2b213aa..43ddbbf 100644 --- a/skill-package/interactionModels/custom/en-US.json +++ b/skill-package/interactionModels/custom/en-US.json @@ -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}", @@ -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", @@ -85,6 +94,7 @@ } ], "samples": [ + "give me question {question}", "give me number {question}" ] }, @@ -405,5 +415,5 @@ ] } }, - "version": "41" + "version": "50" } \ No newline at end of file