Introduction
In this tutorial, we will be focusing on how to create a custom multiplication skill in Alexa using Python. If you’re completely new to Alexa skills, you can get a brief idea about it by clicking here. Subsequently, we have posted regarding account creation in the Alexa developer console and creating a basic Hello World skill in Alexa. You can check those posts out if you’re interested.
Creating the Multiplication Skill
First, log in to your Alexa developer account and go to the developer console. Click on Create Skill.
Following this, type in the skill name as multiplication skill. Choose the primary language as English (US). Subsequently, choose the model as Custom and the method to host backend resources as Alexa-hosted (Python).
Finally, click on Create Skill.
Intents and Sample Utterances
Next, we need to make 2 custom intents for our multiplication skill. Click on Add Intent. Then, type the intent name as SingleMultiplication and click on the Create Custom Intent option. Similarly, create an intent named MultiplicationTable.
In our skill, we are using the SingleMultiplication intent to perform multiplication between two numbers. The MultiplicationTable intent will be used to give the multiplication tables of numbers.
Next, click on the intents to add sample utterances. Sample utterances are those statements that can be expected from the user. We can add a few such utterances for each of our intents
So, for SingleMultiplication Intent, we need to create two slots as the user will ask to multiply between two numbers. Therefore, create two slots and name them as first_number and second_number respectively. Choose the slot type as AMAZON.NUMBER.
Next, we need to add some sample utterances. For eg. “tell me {first_number} times {second_number}”, “give me {first_number} times {second_number}”, “{first_number} times {second_number}”, etc. Note that the slot names should be given within curly brackets.
Similarly, we need to do for MultiplicationTable Intent.
So, for MultiplicationTable Intent, we need one slot as the user will ask for the table of a number. Create a slot and name it as “number”. Choose the slot type as AMAZON.NUMBER.
Following this, let’s add some utterances. For eg. “tell me the table of {number}”, “give me the table of {number}”, “table of {number}”, etc.
Next, click on the Build Model option at the top and wait for some time. Once the build is completed, let’s start with the coding section.
Coding the Multiplication Skill
So far, we have completed building our model. Note that the model needs to be re-built if you make any new changes. Now let’s look at the coding part
First, click on the Code option. Next, open the lambda_function.py file. This is our main code file.
The LaunchRequestHandler class handles the launch requests for a skill. We can modify the response that we get after launching a skill
We need to add 2 classes for our custom intents. The code is as follows
class SingleMultiplicationIntentHandler(AbstractRequestHandler):
"""Handler for Single Multiplication Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return ask_utils.is_intent_name("SingleMultiplication")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
f_num = handler_input.request_envelope.request.intent.slots['first_number']
f_num = int(f_num.value)
s_num = handler_input.request_envelope.request.intent.slots['second_number']
s_num = int(s_num.value)
p = str((f_num)*(s_num))
speak_output = "The product will be " + p
return (
handler_input.response_builder
.speak(speak_output)
# .ask("add a reprompt if you want to keep the session open for the user to respond")
.response
)
class MultiplicationTableIntentHandler(AbstractRequestHandler):
"""Handler for Multiplication Table Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return ask_utils.is_intent_name("MultiplicationTable")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
num = handler_input.request_envelope.request.intent.slots['number']
num = int(num.value)
speak_output = str(num) + " times 0 equals 0"
x = range(1,11)
for n in x:
p = " ," + str(num) + " times " + str(n) + " equals " + str(num*n)
speak_output += p
return (
handler_input.response_builder
.speak(speak_output)
# .ask("add a reprompt if you want to keep the session open for the user to respond")
.response
)
Some of the basic methods used in the code were explained in a previous post. So, you can give it a quick read by clicking here.
Next, using the skill builder object, we need to add request handlers for our custom intents. The code for it is as follows
sb.add_request_handler(SingleMultiplicationIntentHandler())
sb.add_request_handler(MultiplicationTableIntentHandler())
After making these changes, click on the Save option. Subsequently, click on the Deploy option.
Now, let’s test our model!
Testing the Model
So, in order to test the model, click on the Test option at the top. Next, enable skill testing to Development mode. Now, start giving the commands as follows
Conclusion
So, we have successfully built our custom multiplication skill in Alexa using Python. Hope that this tutorial was informative as well as enjoyable.
Happy Learning!