Skip to content

Instantly share code, notes, and snippets.

Created April 23, 2017 04:59
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 anonymous/a31a6e6dbe310fa65ba759efd9309675 to your computer and use it in GitHub Desktop.
Save anonymous/a31a6e6dbe310fa65ba759efd9309675 to your computer and use it in GitHub Desktop.
Sample Alexa skill built using Alexa sdk at https://www.nuget.org/packages/VisidsuTech.AlexaNet/
/// <summary>
/// Sample Alexa skill built using Alexa sdk at https://www.nuget.org/packages/VisidsuTech.AlexaNet/
/// </summary>
class SampleAlexaSkill : IAlexaSkill
{
private static Dictionary<string, Dictionary<string[], string>> slotValueConvertions = new Dictionary<string, Dictionary<string[], string>>()
{
// converts (one, first) to (one) for slot named "slot1"
{
"slot1",
new Dictionary<string[], string>(){{new[]{"one", "first"}, "one"} }
}
};
private static Dictionary<string, Func<AlexaRequest, Task<AlexaResponse>>> actions = new Dictionary<string, Func<AlexaRequest, Task<AlexaResponse>>>()
{
// calls the function "Intent1Action" for intent "Intent1"
{"Intent1", Intent1Action}
};
private static AlexaRequestExecutor requestExecutor;
/// <summary>
/// Replace with real application Id of alexa skill.
/// </summary>
private static string alexaAppId = "<replace with Alexa appId>";
/// <summary>
/// Initializes Alexa request executor.
/// </summary>
static SampleAlexaSkill()
{
var alexaAuth = new AlexaServiceAuth(alexaAppId);
var alexaSkill = new SampleAlexaSkill();
requestExecutor = new AlexaRequestExecutor(alexaAuth, alexaSkill);
}
/// <summary>
/// Executes a Http request with alexa payload. This function can be called from a http service (like azure function) to serve a response.
/// </summary>
/// <param name="request">Http request which contains alexa payload</param>
/// <returns>Http response with alexa response payload.</returns>
public static Task<HttpResponseMessage> ExecuteRequest(HttpRequestMessage request)
{
return requestExecutor.Run(request);
}
/// <summary>
/// Contains all slot value conversions, use this to normalize slot values.
/// </summary>
public Dictionary<string, Dictionary<string[], string>> SlotValueConvertions => slotValueConvertions;
public Dictionary<string, Func<AlexaRequest, Task<AlexaResponse>>> Actions => throw new NotImplementedException();
public Task<AlexaResponse> LaunchAction(AlexaRequest request)
{
return Task.FromResult<AlexaResponse>(
new AlexaResponse()
{
Details = new AlexaResponseDetails()
{
OutputSpeech = new AlexaOutputSpeech()
{
Type = AlexaOutputSpeechType.PlainText,
Text = "This is a sample alexa skill"
}
}
});
}
private static Task<AlexaResponse> Intent1Action(AlexaRequest request)
{
return Task.FromResult<AlexaResponse>(
new AlexaResponse()
{
Details = new AlexaResponseDetails()
{
OutputSpeech = new AlexaOutputSpeech()
{
Type = AlexaOutputSpeechType.PlainText,
Text = "Intent 1 was invoked"
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment