Creating Alexa skill using AWS lambda as backend
Amazon Alexa is a virtual assistant developed by Amazon, available on a range of devices including the Amazon Echo, Echo Dot, and Echo Show. One of the key features of Alexa is the ability to use “skills” to extend its functionality and allow users to interact with it in new ways.
Creating a skill for Alexa is relatively simple and can be done using the Alexa Skills Kit (ASK), a collection of tools and resources for building Alexa skills. To get started, you will need to:
Sign up for an Amazon developer account, if you don’t have one already.
- Visit the Alexa Skills Kit developer portal and create a new skill.
- Choose a “skill type” that best suits your skill’s functionality.
- Define the “interaction model” for your skill by creating a list of “utterances” (the things users can say to Alexa to invoke your skill) and “intent” (what the skill should do when it’s invoked).
- Set up the “backend” for your skill, which is the code that runs on Amazon’s servers to handle requests and provide responses.
- Test your skill using the Alexa Skills Kit developer portal or an Alexa-enabled device.
- Submit your skill for certification by Amazon.
Dividing the whole process into 3 steps:
- Developing the skill’s functionality.
- Creating an interaction model.
- Setting up the backend.
Developing the skill’s functionality:
This step involves determining what the skill should do, and how users will interact with it. For example, a skill might allow users to listen to a podcast, play a game, or control their smart home devices. It’s important to choose a unique and useful skill that will appeal to your target audience.
Creating an interaction model:
An interaction model defines the ways in which users can interact with your skill, including what they can say to invoke it and what the skill should do in response. You will need to create a list of “utterances” (the things users can say to invoke your skill) and “intents” (what the skill should do when it’s invoked). This step involves writing sample phrases that users might say and mapping them to the intents that are defined for your skill.
Setting up the backend:
This step involves creating the code that runs on Amazon’s servers to handle requests and provide responses. The backend code can be written in a variety of languages such as python, node.js and Java. This code should handle the requests coming from Alexa and respond accordingly.
Practicle Part start from here:
Prerequisites:
- An Amazon developer account. This is required to create and configure Alexa’s skills.
- An aws (Amazon web services) account to host your skill on aws lambda
- An ask-sdk requires Python 2 (>= 2.7) or Python 3 (>= 3.6).
To check the python version run the below command in terminal:
$python –version
Adding ask-sdk to your project:
You can install it globaly but it is better to use virtual environment. virtual environment is an isolated Python environment that helps manage project dependencies and package versions.
use the below command to create virtual environment:
$python3 -m venv name_of_virtual_env
In my case i am venv using name alexa-skill-venv
$python3 -m venv alexa-skill-venv
After creating virtual environment run the below command to activate your virtual environment:
$source alexa-skill-venv/bin/activate
above command will activate the alexa-skill-venv(your venv name may be different)
Next, Install ask-sdk in the virtualenv
$pip install ask-sdk
The SDK will be installed in the alexa-skill-venv/lib/python3.8/site-packages
Your python version may be different so it could be python2.8 or python3.6 etc.
Developing the Hello world skill
create hello_world.py file in the alexa-skill folder
Next, implementing the request handlers for hello_world skill
Request Handlers: Request handlers in Alexa Skills Kit (ASK) are functions that process requests and generate responses for Alexa to fulfill user’s request. There are several types of request handlers in ASK that are used to handle different types of requests, including:
LaunchRequest Handler: This handler is invoked when the user launches the skill. It is used to provide a welcome message or instruction to the user.
IntentRequest Handler: This handler is invoked when the user invokes an intent. An intent represents a specific action that the user wants to perform, such as asking for a weather update or play music etc. IntentRequest handlers are used to process the user’s request and provide an appropriate response.
SessionEndedRequest Handler: This handler is invoked when the session ends. It is used to perform any cleanup or logging tasks that need to be done before the session is terminated.
Your code is responsible for making sure that the right request handler is used to process incoming requests and for providing a response.
- Creating a skill builder object. The skill builder object helps in adding components responsible for handling input requests and generating custom responses for your skill.
Type the following code in your hello_world.py file
Each request handler is written as a class that implements two methods of the AbstractRequestHandler class:
- can_handle : The can_handle method checks if the incoming request matches the specified intent name.
- handle : the handle method generates a response containing the “Hello, World!” message.
LaunchRequest handler
A LaunchRequest handler is a request handler in Alexa Skills Kit (ASK) that is responsible for handling requests made by users when they launch the skill. When the user opens your skill without providing a specific intent, a LaunchRequest is triggered.
In a LaunchRequest handler, you can provide a welcome message to the user or provide instructions on how to use the skill.
add the following code into your hello_world.py file, after the previous code.
The can_handle function returns True if the incoming request is a LaunchRequest.
HelloWorldIntent handler
Type the follwing code after the LaunchRequestHandler class.
In the avobe example, we define a custom request handler called HelloWorldIntentHandler that can handle requests for the “HelloWorldIntent” intent. The can_handle method checks if the incoming request matches the specified intent name, and the handle method generates a response containing the “Hello, World” message.
HelpIntentHandler:
Type the following code after the previous handler code.
In Alexa Skills Kit (ASK) development, the HelpIntentHandler is a built-in handler function that is automatically invoked when the user says something like “help” or “what can I say?” while using an Alexa skill. The purpose of the HelpIntentHandler is to provide the user with guidance on how to interact with the skill and what types of requests are supported.
CancelAndStopIntent handler:
Type the follwing code after the previous handler class.
The CancelAndStopIntent handler is a built-in handler function that is automatically invoked when the user says something like “cancel”, “stop”. while using an Alexa skill. The purpose of the CancelAndStopIntent handler is to end the user’s session with the skill gracefully.
SessionEndedRequest handler:
Type the following code after the previous handler class.
The SessionEndedRequest handler is a built-in handler function that is automatically invoked when the user’s session with the skill ends for any reason. The purpose of the SessionEndedRequest handler is to perform any necessary cleanup or logging operations and gracefully exit the skill.
Exception handlers:
Type the following code after the previous handler class.
Exception handlers are used to handle errors that occur within the skill code. There are two main types of exception handlers: generic exception handlers and request-specific exception handlers. A generic exception handler is used to catch any unhandled exceptions within the skill code.
Lambda handler:
The Lambda handler is the entry point for the function. It’s the function that AWS Lambda invokes when it runs the code.
Next, Make zip file of the folder in my case it is alexa-skill.zip
Create a new skill by following these steps:
– Log in to the Alexa Skills Kit Developer Console.
– Click the Create Skill button in the upper right.
– Enter “skill_name_of_your_choice” as your skill name and click Next.
For the model, select Custom and click Create skill.
Create a lambda function on aws.
- After creating lambda go to Add triggers section, and select Alexa as a trigger point.
- Add the skill id of which you get after creating the Alexa skill go to the end point tab you will see the skill id copy it and paste into the lambda trigger configuration.
- Next, upload your zip file by going to tab Upload from you can see in the below image.
After successful upload. Go to the Alexa skill that you created earlier.
Next, Go to the Invocation tab and set the Invocation name, as you can see in the below image.
Next, Go to the Interaction Modal tab, select Intents, and click add Intemt HelloWorldIntent
see the below image for reference.
Next, add your custom intent in create a custom intent section.
Next, click on HelloWorldIntent to some utterances which the user needs to speak to call that intent.
AMAZON.CancelIntent, AMAZON.HelpIntent, and AMAZON.StopIntent are built-in Alexa intents, you do not need to provide sample utterances for them.
Next, Once you are done editing the interaction model, dont forget to save and build the model.
Next, configure the endpoint for the skill. To do this, follow these steps:
copy the lambda function arn
paste it to the alexa skill endpoint section
Save the endpoint , go to interaction tab click save and build model.
All done.
By going to test tab in Alexa developer console you can test your skill .
You can also open the Alexa app on your phone or at https://alexa.amazon.com and see your skills listed under Your Skills.
Reference link :https://developer.amazon.com/en-US/alexa/alexa-developer-documentation-welcome