Introduction
In this tutorial, we will look at how to create a simple date-time skill in Alexa using Python. For this skill, the only prerequisites required are an Alexa developer account and some basic understanding of Python. Also, we have created a post on how to create a custom multiplication skill in Alexa. You can view the post by clicking here.
Creating the Date-Time Skill
The first step is to log in to your Alexa developer account. Following this, click on the Create Skill option.
Next, you’ll have to configure some basic settings of your skill as follows:
Step 1: Give the skill a name. For eg. date time skill
Step 2: Select the language in which you want to build the skill. In our case, we choose English (US).
Step 3 : Choose the Custom model for our skill.
Step 4 : Select Alexa-hosted (Python) as the backend resource for our skill.
Step 5: Click on Create Skill.
In doing so, your custom skill will be created.
First, click the Skill Invocation Name option. There, you would be able to modify the skill invocation name.
For our skill, we need two custom intents – PresentTime and PresentDay. In order to add the custom intent, click on the Add Intent option.
Now, we need to add sample utterances to each intent.
The PresentTime intent is created in order to display the current time. We need to add some sample utterances which would map to this intent. For eg. “tell me the current time”, “give me the present time”, “what is the time now” etc.
The PresentDay intent is created in order to display the present date. We need to add some sample utterances. For eg. “what is the date today”, “give me today’s date” etc.
Now that we have created our intents, we need to build our model. Click the Build Model option at the top.
Coding the Date-Time Skill
So, we have successfully completed building the model. Now, let’s start coding our model. For this, click on the Code option at the top. Next, open the lambda_function.py file which is the main code file.
First, let’s start by modifying Alexa’s response when a skill is launched. This can be done in the LaunchRequestHandler class
Next, we need to add two classes for our two custom intents, namely PresentTimeIntentHandler() and PresentDayIntentHandler(). The code for the two classes is as follows
from datetime import datetime
class PresentTimeIntentHandler(AbstractRequestHandler):
"""Handler for Present Time Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return ask_utils.is_intent_name("PresentTime")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
now = datetime.now()
present_time = now.strftime("%H:%M:%S")
speak_output = "The present time is " + present_time
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 PresentDayIntentHandler(AbstractRequestHandler):
"""Handler for Present Day Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return ask_utils.is_intent_name("PresentDay")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
now = datetime.now()
present_day = now.strftime("%B %d, %Y")
speak_output = "Today's date is " + present_day
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
)
Here, we use the datetime class in Python to get the present time and date.
Also, we created a post explaining some of the basic methods used in the code. Click here to give it a quick read.
Subsequently, 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(PresentTimeIntentHandler())
sb.add_request_handler(PresentDayIntentHandler())
As a result, the coding part is complete. Therefore, let’s deploy our model. So, click on the Deploy option.
Testing the Model
Now that we have deployed the model, let’s test it out. Click on the Test option at the top. Enable skill testing to Development mode.
Conclusion
We have successfully built and deployed our custom model. On testing it, we have found that our custom intents are working properly. Hope that this tutorial was informative and worth your time.
Happy Learning!