Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple gadget quiz #4

Merged
merged 4 commits into from May 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Alexa.NET.Samples.sln
Expand Up @@ -12,6 +12,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProgressiveResponse", "Prog
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ListManagement", "ListManagement\ListManagement.csproj", "{DF324EBD-8ABC-4B47-A87D-60A0EA9D2A29}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleGadgetQuiz", "SimpleGadgetQuiz\SimpleGadgetQuiz.csproj", "{CE8E1609-2A8D-4809-84DF-C3FF6DB20583}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,6 +28,10 @@ Global
{DF324EBD-8ABC-4B47-A87D-60A0EA9D2A29}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF324EBD-8ABC-4B47-A87D-60A0EA9D2A29}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF324EBD-8ABC-4B47-A87D-60A0EA9D2A29}.Release|Any CPU.Build.0 = Release|Any CPU
{CE8E1609-2A8D-4809-84DF-C3FF6DB20583}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE8E1609-2A8D-4809-84DF-C3FF6DB20583}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE8E1609-2A8D-4809-84DF-C3FF6DB20583}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE8E1609-2A8D-4809-84DF-C3FF6DB20583}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
93 changes: 93 additions & 0 deletions SimpleGadgetQuiz/Function.cs
@@ -0,0 +1,93 @@
using System.Linq;
using System.Threading.Tasks;
using Alexa.NET;
using Alexa.NET.Gadgets.GameEngine;
using Alexa.NET.Gadgets.GameEngine.Requests;
using Alexa.NET.Request;
using Alexa.NET.Request.Type;
using Alexa.NET.Response;
using Alexa.NET.Response.Ssml;
using Alexa.NET.StateManagement;
using Alexa.NET.Response.Ssml.SoundLibrary;
using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace SimpleGadgetQuiz
{
public class Function
{
public Function()
{
new GadgetRequestHandler().AddToRequestConverter();
}

/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public async Task<SkillResponse> FunctionHandler(SkillRequest input, ILambdaContext context)
{
var state = new SkillState(input);

switch (input.Request)
{
case LaunchRequest _:
var rollCallResponse = ResponseBuilder.Ask("Please press the buttons you want to use for this quiz", null);
rollCallResponse.AddRollCall(20000, "first", "second");

return rollCallResponse;
case InputHandlerEventRequest inputHandler:
if (inputHandler.TryRollCallResult(out var mapping, "first", "second"))
{
state.SetSession("player1", mapping["first"]);
state.SetSession("player2", mapping["second"]);
var question = "Question One. Which is the bigger number, 3 or 5?";

var response = ResponseBuilder.Ask($"Okay then, let's begin! {question}", null,state.Session);
response.WhenFirstButtonDown(mapping, "buzzedIn", 10000);
response.Response.ShouldEndSession = null;
return response;
}

if (inputHandler.TryMapEventGadget("buzzedIn", out var player))
{
var buzzedInPlayer = player == state.GetSession<string>("player1") ? "player one" : "player two";
state.SetSession("currentAnswer", "five");
return ResponseBuilder.Ask($"Okay then {buzzedInPlayer}, what's your answer?",null, state.Session);
}

break;
case IntentRequest intent:
switch (intent.Intent.Name)
{
case BuiltInIntent.Cancel: case BuiltInIntent.Stop:
return ResponseBuilder.Empty();
case "answer":
Speech responseSpeech = await CheckAnswer(intent,state) ?
new Speech(Human.CrowdCheerMedium, new PlainText("Well done, that's correct!")) :
new Speech(Human.CrowdBoo01, new PlainText("Sorry, that's not the right answer"));

return ResponseBuilder.Tell(responseSpeech);
}

break;
}

return ResponseBuilder.Tell("Sorry - I'm not sure how to handle that");
}

private async Task<bool> CheckAnswer(IntentRequest intent, SkillState state)
{
var expected = await state.Get<string>("currentAnswer");
var slot = intent.Intent.Slots["currentAnswer"];
var resolution = slot.Resolution.Authorities.First();
return slot.Value == expected ||
(resolution.Status.Code == ResolutionStatusCode.SuccessfulMatch &&
resolution.Values.First().Value.Name == expected);
}
}
}
21 changes: 21 additions & 0 deletions SimpleGadgetQuiz/SimpleGadgetQuiz.csproj
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Alexa.NET" Version="1.5.2" />
<PackageReference Include="Alexa.NET.Gadgets" Version="1.3.5" />
<PackageReference Include="Alexa.NET.SoundLibrary" Version="1.0.2" />
<PackageReference Include="Alexa.NET.StateManagement" Version="1.0.7" />
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.1.3" />
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions SimpleGadgetQuiz/SkillDefinition.json
@@ -0,0 +1,84 @@
{
"interactionModel": {
"languageModel": {
"invocationName": "simple gadget quiz",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "answer",
"slots": [
{
"name": "currentAnswer",
"type": "allPossibleAnswers",
"samples": [
"{currentAnswer}"
]
}
],
"samples": [
"{currentAnswer}"
]
}
],
"types": [
{
"name": "allPossibleAnswers",
"values": [
{
"name": {
"value": "five"
}
},
{
"name": {
"value": "three"
}
}
]
}
]
},
"dialog": {
"intents": [
{
"name": "answer",
"confirmationRequired": false,
"prompts": {},
"slots": [
{
"name": "currentAnswer",
"type": "allPossibleAnswers",
"confirmationRequired": false,
"elicitationRequired": true,
"prompts": {
"elicitation": "Elicit.Slot.430391336347.819966933357"
}
}
]
}
]
},
"prompts": [
{
"id": "Elicit.Slot.430391336347.819966933357",
"variations": [
{
"type": "PlainText",
"value": "okay then, so what's your answer?"
}
]
}
]
}
}