Skip to content

Instantly share code, notes, and snippets.

@LucioMSP
Last active April 24, 2020 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucioMSP/090b49385d2cbeef6cf4e00bbdd4c0df to your computer and use it in GitHub Desktop.
Save LucioMSP/090b49385d2cbeef6cf4e00bbdd4c0df to your computer and use it in GitHub Desktop.
Alexa Skill with Net Core
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
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.SystemTextJson.LambdaJsonSerializer))]
namespace NumberGame_AWSLambda
{
public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(string input, ILambdaContext context)
{
return input?.ToUpper();
}
}
}
using System;
using Alexa.NET;
using Newtonsoft.Json;
using Alexa.NET.Request;
using Amazon.Lambda.Core;
using Alexa.NET.Response;
using Alexa.NET.Request.Type;
using System.Collections.Generic;
using System.Threading.Tasks;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]
namespace NumberGame_AWSLambda
{
public class Function
{
public async Task<string> FunctionHandler(object inputObject, ILambdaContext context)
{
var input = JsonConvert.DeserializeObject<SkillRequest>(inputObject.ToString());
SkillResponse tell = null;
ILambdaLogger log = context.Logger;
log.LogLine($"Skill Request Object:" + JsonConvert.SerializeObject(input));
Session session = input.Session;
if (session.Attributes == null)
session.Attributes = new Dictionary<string, object>();
Type requestType = input.GetRequestType();
if (input.GetRequestType() == typeof(LaunchRequest))
{
string speech = "Welcome! Say new game to start";
Reprompt rp = new Reprompt("Say new game to start");
tell = ResponseBuilder.Tell(speech, session);
tell.Response.ShouldEndSession = false;
}
else if (input.GetRequestType() == typeof(SessionEndedRequest))
{
tell = ResponseBuilder.Tell("Goodbye!");
}
else if (input.GetRequestType() == typeof(IntentRequest))
{
var intentRequest = (IntentRequest)input.Request;
switch (intentRequest.Intent.Name)
{
case "AMAZON.CancelIntent":
case "AMAZON.StopIntent":
tell = ResponseBuilder.Tell("Goodbye!");
break;
case "AMAZON.HelpIntent":
{
Reprompt rp = new Reprompt("What's next?");
tell = ResponseBuilder.Ask("Here's some help. What's next?", rp, session);
}
break;
case "NewGameIntent":
{
session.Attributes["num_guesses"] = 0;
Random rnd = new Random();
Int32 magicNumber = rnd.Next(1, 10);
session.Attributes["magic_number"] = magicNumber;
string next = "Guess a number betwen 1 and 10";
Reprompt rp = new Reprompt(next);
tell = ResponseBuilder.Ask(next, rp, session);
}
break;
case "AnswerIntent":
{
// check answer
string userString = intentRequest.Intent.Slots["Number"].Value;
Int32 userInt = 0;
Int32.TryParse(userString, out userInt);
bool correct = (userInt == (Int32)(long)session.Attributes["magic_number"]);
Int32 numTries = (Int32)(long)session.Attributes["num_guesses"] + 1;
string speech = "";
if (correct)
{
speech = "Correct! You guessed it in " + numTries.ToString() + " tries. Say new game to play again, or stop to exit. ";
session.Attributes["num_guesses"] = 0;
}
else
{
speech = "Nope, guess again.";
session.Attributes["num_guesses"] = numTries;
}
Reprompt rp = new Reprompt("speech");
tell = ResponseBuilder.Ask(speech, rp, session);
}
break;
default:
{
log.LogLine($"Unknown intent: " + intentRequest.Intent.Name);
string speech = "I didn't understand - try again?";
Reprompt rp = new Reprompt(speech);
tell = ResponseBuilder.Ask(speech, rp, session);
}
break;
}
}
/*return JsonConvert.SerializeObject(tell, Formatting.None, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}).Replace("\u0022", String.Empty).Trim();*/
var x = JsonConvert.SerializeObject(tell, Formatting.None, new JsonSerializerSettings());
return x;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment