Before you begin, make sure to setup your LambdaSharp CLI.
Creating a function that is invoked by an Alexa Skill requires two steps. First, an Alexa Skill must be created with an Amazon Developer account. Second, the function must have the Alexa attribute in its Sources section.
Optionally, the Alexa attribute can specify an Alexa Skill ID to restrict invocation to a specific Alexa Skill.
Module: Sample.Alexa
Description: A sample module using an Alexa skill
Items:
- Parameter: AlexaSkillID
Description: Alexa Skill ID
Default: "*"
- Function: MyFunction
Description: This function is invoked by an Alexa Skill
Memory: 128
Timeout: 30
Sources:
- Alexa: !Ref AlexaSkillIDAlexa Skill requests can be parsed using the Alexa.NET library by Tim Heuer and by deriving the function from the ALambdaFunction<T> base class.
public sealed class Function : ALambdaFunction<SkillRequest, SkillResponse> {
//--- Methods ---
public override Task InitializeAsync(LambdaConfig config)
=> Task.CompletedTask;
public override async Task<SkillResponse> ProcessMessageAsync(SkillRequest skill) {
switch(skill.Request) {
case LaunchRequest launch:
LogInfo("Launch");
break;
case IntentRequest intent:
LogInfo("Intent");
LogInfo($"Intent.Name = {intent.Intent.Name}");
break;
case SessionEndedRequest ended:
LogInfo("Session ended");
return ResponseBuilder.Empty();
}
return ResponseBuilder.Tell(new PlainTextOutputSpeech {
Text = "Hi!"
});
}
}The LambdaSharp CLI automatically creates the required permissions to allow the Alexa skill to invoke the Lambda function. The EventSourceToken attribute is omitted if the Alexa attribute is set to "*" in the module definition.
Thw following YAML shows the permission granted to the Alexa service.
FunctionAlexaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
EventSourceToken: !Ref AlexaSkillID
FunctionName: !Ref Function
Principal: alexa-appkit.amazon.com