Alexa.NET is a helper library for working with Alexa skill requests/responses in C#. Whether you are using the AWS Lambda service or hosting your own service on your server, this library aims just to make working with the Alexa API more natural for a C# developer using a strongly-typed object model.
Here are some simple examples of how to use this library assuming the default signature of the AWS Lambda C# function:
public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
{
// your function logic goes here
}
You most likely are going to want to get the type of request to know if it was the default launch, an intent, or maybe an audio request.
// check what type of a request it is like an IntentRequest or a LaunchRequest
var requestType = input.GetRequestType();
if (requestType == typeof(IntentRequest))
{
// do some intent-based stuff
}
else if (requestType == typeof(Alexa.NET.Request.Type.LaunchRequest))
{
// default launch path executed
}
else if (requestType == typeof(AudioPlayerRequest))
{
// do some audio response stuff
}
Once you know it is an IntentRequest you probably want to know which one (name) and perhaps pull out parameters (slots):
// do some intent-based stuff
var intentRequest = input.Request as IntentRequest;
// check the name to determine what you should do
if (intentRequest.Intent.Name.Equals("MyIntentName"))
{
// get the slots
var firstValue = intentRequest.Intent.Slots["FirstSlot"].Value;
}
Once you know it is an AudioPlayerRequest, you have to determine which one (playback started, finished, stopped, failed) and respond accordingly.
// do some audio response stuff
var audioRequest = input.Request as AudioPlayerRequest;
// these are events sent when the audio state has changed on the device
// determine what exactly happened
if (audioRequest.AudioRequestType == AudioRequestType.PlaybackNearlyFinished)
{
// queue up another audio file
}
There are various types of responses you can build and this library provides a helper function to build them up. A simple one of having Alexa tell the user something using SSML may look like this:
// build the speech response
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "Today is <say-as interpret-as=\"date\">????0922</say-as>.<break strength=\"x-strong\"/>I hope you have a good day.";
// create the response using the ResponseBuilder
var finalResponse = ResponseBuilder.Tell(speech);
return finalResponse;
In your response you can also have a 'Card' response, which presents UI to the Alexa companion app for the registered user. Cards presently are simple and contain basically titles and plain text (no HTML :-(). To create a response with cards, you can use the ResponseBuilder:
// create the speech response - cards still need a voice response
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "Today is <say-as interpret-as=\"date\">????0922</say-as>.";
// create the card response
var finalResponse = ResponseBuilder.TellWithCard(speech, "Your Card Title", "Your card content text goes here, no HTML formatting honored");
return finalResponse;
If you want to reprompt the user, use the Ask helpers
// create the speech response
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "Today is <say-as interpret-as=\"date\">????0922</say-as>.";
// create the speech reprompt
var repromptMessage = new Alexa.NET.Response.PlainTextOutputSpeech();
repromptMessage.Text = "Would you like to know what tomorrow is?";
// create the reprompt
var repromptBody = new Alexa.NET.Response.Reprompt();
repromptBody.OutputSpeech = repromptMessage;
// create the response
var finalResponse = ResponseBuilder.Ask(speech, repromptBody);
return finalResponse;
If your skill is registered as an audio player, you can send directives (instructions to play, enqueue, or stop an audio stream).
// create the speech response - you most likely will still have this
string audioUrl = "http://mydomain.com/myaudiofile.mp3";
string audioToken = "a token to describe the audio file";
var audioResponse = ResponseBuilder.AudioPlayerPlay(PlayBehavior.ReplaceAll, audioUrl, audioToken);
return audioResponse
If you do not want to use the helper Tell/Ask functions for the simple structure you
can build up the response manually using the Response
and some IOutputSpeech
objects.
// create the speech response - you most likely will still have this
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "Today is <say-as interpret-as=\"date\">????0922</say-as>.";
// create the response
var responseBody = new Alexa.NET.Response.ResponseBody();
responseBody.OutputSpeech = speech;
responseBody.ShouldEndSession = true;
var skillResponse = new Alexa.NET.Response.SkillResponse();
skillResponse.Response = responseBody;
skillResponse.Version = "1.0";
return skillResponse;
To add reprompt to the response you just need to include that as well.
// create the speech response - you most likely will still have this
var speech = new Alexa.NET.Response.SsmlOutputSpeech();
speech.Ssml = "Today is <say-as interpret-as=\"date\">????0922</say-as>.";
// create the reprompt speech
var repromptMessage = new Alexa.NET.Response.PlainTextOutputSpeech();
repromptMessage.Text = "Would you like to know what tomorrow is?";
// create the reprompt object
var repromptBody = new Alexa.NET.Response.Reprompt();
repromptBody.OutputSpeech = repromptMessage;
// create the response
var responseBody = new Alexa.NET.Response.ResponseBody();
responseBody.OutputSpeech = speech;
responseBody.ShouldEndSession = false; // this triggers the reprompt
responseBody.Reprompt = repromptBody;
var skillResponse = new Alexa.NET.Response.SkillResponse();
skillResponse.Response = responseBody;
skillResponse.Version = "1.0";
return skillResponse;