Before we jump into the technical discussion, Let’s clear first, What is skill fundamentally? Skills are like apps for Alexa. Skills allow users to use their voices to perform day to day life tasks. Checking news, listening to music, playing puzzles or using smart home devices, everything is possible with such skills.
In this blog, we are going to learn about how to build an Alexa Fact Skill using Python, that will help users getting facts about the Statue of Unity.
The Alexa Skills Kit SDK for Python simplifies the development of the back-end cloud service for your Alexa skill. A part of the Alexa Skills Kit, the SDK reduces the amount of code you need to write to process Alexa requests and responses and to handle other common skill tasks.
How does an Alexa Skill Works
An Alexa skill has two most important components:
- An interaction model
- Application logic
When a customer gives the command to its smart speaker, Alexa processes the speech in the context of their interaction model to determine the customer’s request. Alexa then sends the request to your skill application logic, which acts on it. You provide your application logic as a back-end cloud service.
Getting Started With Alexa Development
To follow this tutorial, you will need an Amazon Developer account. Go to Amazon Developer Services, fill out the form with the required details and complete the sign-up process.
To get started, log into the Alexa developer console with your Amazon Developer account.
Create Your Skill
- 1. Click Create Skill on the right-hand side of the console. A new page displays.
- 2. In the Skill name field, enter SOU Facts.
- 3. Set the Default Language to English (IN) or other languages if you live in a different language speaking country.
4. We are building a custom skill. Choose a model to add to your skill, select Custom.
5. Choose a method to host your skill’s backend resources, select Alexa-Hosted (Python).
- 6. At the top of the page, click Create skill.
- 7. Choose a template to add to your skill, select Hello World Skill.
- 8. At the top of the page, click Choose.
It takes a few moments for AWS to provision resources for your skill. When this process completes
9. Now go to the Alexa developer console, find your skill on the Skills tab, in the Alexa Skills list. Click Edit to continue working on your skill.
As an Alexa Python developer, you would need to know some of the Alexa skill interaction models.
Invocation name: To begin interacting with a skill, a user says the skill’s invocation name. For example, to use the SOU Facts skill, the user could say, “Alexa, open SOU facts”
Intent: Intent is an action that completes a user’s request. Intent could hold arguments called ‘slots’.
Slot value: Slots are input values provided in a user’s spoken request. These values help Alexa figure out the user’s intent.
Sample utterances: The sample utterances specify the words and phrases users can say to invoke your intents. Each intent is mapped to several utterances.
Change Invocation Name
Click the Invocation option from the left side panel and change Skill Invocation Name to s o u facts.
Create An Intent
Let’s create the FactIntent, which will return a random fact from a list to the user. Go to the Build section of your Alexa developer console. Then, click the Add button next to the Intents option from the left side panel.
With the Create custom intent option selected, set the name to FactIntent and then click the Create custom intent button.
In the next step, you have to add sample utterances. The user will speak these words to invoke the intent. These could be the phrases like “tell me a fact”, “tell me a statue of unity fact”, “give me a fact”, “give me a statue of unity fact” or “give me another fact”. Type them in a phrase and click the plus sign (+) to add as a sample utterance.
Delete HelloWorldIntent by clicking the trash icon next to the HelloWorldIntent.
Click the Save Model button on the top, and also click the Build Model button to rebuild the interaction model of your skill.
To define how your skill responds to a JSON request, you will define a handler for each intent. There are two pieces to a handler:
The can_handle() function is where you define what requests the handler responds to. The handle() function returns a response to the user. If your skill receives a request, the can_handle() function within each handler determines whether or not that handler can service the request. In this case, the user wants to launch the skill, which is a LaunchRequest.
Click the Code tab. The code editor opens the lambda_function.py file.
Import the random module by adding an import statement to the top of lambda_function.py
from ask_sdk_core.handler_input import HandlerInput from ask_sdk_model import Response import random logger = logging.getLogger(__name__) logger.setLevel(logging.INFO)
Make a list of facts in global scope.
facts = [ "the statue of unity is the world's tallest statue, with a height of 182 metres which is almost double the height of the statue of liberty, usa.", "the statue of unity was built in just 46 months.", "70000 metric tonnes of cement was used in the construction of statue of unity.", "6000 metric tonnes of structural steel and 18500 metric tonnes of reinforcement bars were used to build the statue of unity.", "the thickness of the bronze cladding on the statue is 8 mm.", "the height of the toe of the statue of unity measures 3.6 metre." ]
Delete LaunchRequestHandler and HelloWorldIntentHandler class from lambda_function.py
Next, we’ll create a new intent handler that will handle the request received from the FactIntent and that will also handle the LaunchRequest.
Write a new class FactIntentHandler
class FactIntentHandler(AbstractRequestHandler): def can_handle(self, handler_input): return (ask_utils.is_request_type("LaunchRequest")(handler_input) or ask_utils.is_intent_name("FactIntent")(handler_input)) def handle(self, handler_input): speak_output = random.choice(facts) return ( handler_input.response_builder .speak(speak_output) .ask(speak_output) .response )
This is how the editor should look at this point.
Once you’ve created a FactIntentHandler class, you need to pass it as an argument to SkillBuilder.add_request_handler. Scroll to the bottom of lambda_function.py and remove the following lines:
And add the following line:
SOU Facts skill is ready for final testing!
Click the Save and Deploy button to the save changes and head back to the Test section of the developer console.
Test Your Skill
When you ask the SOU Facts skill to tell you a fact, it should give you a different fact every time.
That’s it! You’ve successfully created your first skill as an Alexa Python developer! Let’s connect if you are developing a skill to solve problems of millions! We at Adrixus are happy to help you with Alexa Skill development.
Here is a video on how to publish your Alexa Skill: