Read this document in Español
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.
Alexa.NET also serves as a base foundation for a set of further Alexa skill development extensions from Steven Pears:
- Management GitHub / NuGet
- In-skill Pricing GitHub / NuGet
- Messaging GitHub / NuGet
- Gadgets GitHub / NuGet
- Customer and Person Profile API GitHub / NuGet
- Settings API GitHub / NuGet
- APL Support GitHub / NuGet
- Reminders API GitHub / NuGet
- Proactive Events API GitHub / NuGet
- CanFulfillIntent Request Support GitHub / NuGet
- Response Assertions GitHub / NuGet
- SkillFlow support (experimental)
- Timers API GitHub / NuGet
- Web API for Games GitHub / NuGet
- Shopping Kit GitHub / NuGet
- Conversations API (Beta) GitHub / NuGet
- Pin Confirmation (Beta) GitHub / NuGet
Regardless of your architecture, your function for Alexa will be accepting a SkillRequest and returning a SkillResponse. The deserialization of the incoming request into a SkillRequest object will depend on your framework.
public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
{
// your function logic goes here
return new SkillResponse("OK");
}
Use the Amazon.Lambda.Serialization.Json
package. The default may be different depending on how you created your project.
In your project file:
<Project Sdk="Microsoft.NET.Sdk">
<!-- ... -->
<ItemGroup>
<PackageReference Include="Alexa.NET" Version="1.15.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="1.2.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.8.0" />
</ItemGroup>
</Project>
In any .cs file:
// 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))]
- Contributors
- Request Types
- Responses
- Session Variables
- Responses Without Helpers
- Progressive Responses
Alexa will send different types of requests depending on the events you should respond to. Below are all of the types of requests:
AccountLinkSkillEventRequest
AudioPlayerRequest
DisplayElementSelectedRequest
IntentRequest
LaunchRequest
PermissionSkillEventRequest
PlaybackControllerRequest
SessionEndedRequest
SkillEventRequest
SystemExceptionRequest
This request is used for linking Alexa to another account. The request will come with the access token needed to interact with the connected service. These events are only sent if they have been subscribed to.
var accountLinkReq = input.Request as AccountLinkSkillEventRequest;
var accessToken = accountLinkReq.AccessToken;
Audio Player Requests will be sent when a skill is supposed to play audio, or if an audio state change has occured on the device.
// do some audio response stuff
var audioRequest = input.Request as AudioPlayerRequest;
if (audioRequest.AudioRequestType == AudioRequestType.PlaybackNearlyFinished)
{
// queue up another audio file
}
Each AudioPlayerRequest
will also come with a request type to describe the state change:
PlaybackStarted
PlaybackFinished
PlaybackStopped
PlaybackNearlyFinished
PlaybackFailed
Display Element Selected Requests will be sent when a skill has a GUI, and one of the buttons were selected by the user. This request comes with a token that will tell you which GUI element was selected.
var elemSelReq = input.Request as DisplayElementSelectedRequest;
var buttonID = elemSelReq.Token;
This is the type that will likely be used most often. IntentRequest will also come with an Intent
object and a DialogState
of either STARTED
, IN_PROGRESS
or COMPLETED
Each intent is defined by the name configured in the Alexa Developer Console. If you have included slots in your intent, they will be included in this object, along with a confirmation status.
var intentRequest = input.Request as IntentRequest;
// check the name to determine what you should do
if (intentRequest.Intent.Name.Equals("MyIntentName"))
{
if(intentRequest.DialogState.Equals("COMPLETED"))
{
// get the slots
var firstValue = intentRequest.Intent.Slots["FirstSlot"].Value;
}
}
This type of request is sent when your skill is opened with no intents triggered. You should respond and expect an IntentRequest
to follow.
if(input.Request is LaunchRequest)
{
return ResponseBuilder.Ask("How can I help you today?");
}
This event is sent when a customer grants or revokes permissions. This request will include a SkillEventPermissions
object with the included permission changes. These events are only sent if they have been subscribed to.
var permissionReq = input.Request as PermissionSkillEventRequest;
var firstPermission = permissionReq.Body.AcceptedPermissions[0];
This event is sent to control playback for an audio player skill.
var playbackReq = input.Request as PlaybackControllerRequest;
switch(playbackReq.PlaybackRequestType)
{
case PlaybackControllerRequestType.Next:
break;
case PlaybackControllerRequestType.Pause:
break;
case PlaybackControllerRequestType.Play:
break;
case PlaybackControllerRequestType.Previous:
break;
}
This event is sent if the user requests to exit, their response takes too long or an error has occured on the device.
var sessEndReq = input.Request as SessionEndedRequest;
switch(sessEndReq.Reason)
{
case Reason.UserInitiated:
break;
case Reason.Error:
break;
case Reason.ExceededMaxReprompts:
break;
}