
This tutorial is part of the Quick & Dirty series for developing a custom Alexa skill called My Zoo.
Estimated time: 5 – 10 minutes
No programming experience necessary
Overview
With this tutorial, you’ll code an Alexa skill’s intent on the back-end. We’ll be using Python in the Alexa Development Console (ADC).
After a My Zoo user triggers an intent by uttering a phrase like “make the sound of a lion”, Alexa interprets the phrase, finds the slot value (e.g. animal = “lion”), then invokes the CaptureAnimal intent associated with the utterance. You made all that happen in Part 2 of this tutorial series!
Now you just need to add logic to the CaptureAnimal intent, to tell Alexa what animal sound to make.
Intent logic is handled on the back-end … the code part of the skill. You already updated the code by altering the welcome message and adding prompts.
Prerequisites
- Complete Part 1 (“Hello Zoo!”)
- Complete Part 2 (Intents, Utterances, Slots)
Ready? Let’s do it!
Code the Intent
Step 1: Add CaptureAnimal Handler
To get Alexa to actually do something, we need to add an Intent Handler. This involves adding code (which I’ll give you).
A reminder about Python: formatting matters. Make sure lines are indented as shown.
- Click Code on the top menu bar.
- In the code editor window, find the line:
class HelloWorldIntentHandler(AbstractRequestHandler):
- Press enter a few times before
class HelloWorldIntentHandler
to create several blank lines. Put your cursor on one of the blank lines. - Copy/paste this code:
class CaptureAnimalIntentHandler(AbstractRequestHandler): """Handler for CaptureAnimal Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("CaptureAnimal")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response animal = handler_input.request_envelope.request.intent.slots["animal"].value sounds = { 'bat': 'screech screech', 'bee': 'buzzzz buzzzz', 'cat': 'meow meow', 'chicken': 'cluck cluck', 'cow': 'mooooo', 'dog': 'bark bark', 'human': 'hello', 'lion': 'roar', 'monkey': 'chitter chatter', 'pig': 'oink oink', 'snake': 'hiss rattle hiss', 'zebra': 'whinny' } try: sound = sounds[animal] speak_output = animal + 's say ' + sound except: speak_output = "I have not learned that animal's sound yet." prompt = "<break time='0.75s'/> Try another one!" speak_output = speak_output + prompt return ( handler_input.response_builder .speak(speak_output) .response )
Notice this block of code:
sounds = {
'bat': 'screech screech',
'bee': 'buzzzz buzzzz',
'cat': 'meow meow',
'chicken': 'cluck cluck',
'cow': 'mooooo',
'dog': 'bark bark',
'human': 'hello',
'lion': 'roar',
'monkey': 'chitter chatter',
'pig': 'oink oink',
'snake': 'hiss rattle hiss',
'zebra': 'whinny'
}
This is a database of animal sounds. It’s a small database for now, just enough to do a “proof of concept” test. We’ll be improving the zoo database later in this Tutorial series. For now, just note what animals are listed. You’ll want to test for animals that are in the database as well as animals that are not.
Step 2: Add new Intent to list of Handlers
- Scroll to bottom of the code editor, find this line of code:
sb.add_request_handler(LaunchRequestHandler)
- Add this line of code after it:
sb.add_request_handler(CaptureAnimalIntentHandler())
- Click Save.
- Click Deploy. Wait for it to finish.
Step 3: Test
- Click Test on the top menu bar.
- Enter
my zoo
Test several animals:
- an animal in your sounds database created in Step 1 (for example
bat
) - an animal not in the database (for example,
elephant
) - an animal that does not exist in this world (therefore, not a valid AMAZON.animal slot type … refer to Step 4 of Part 2)
Remember to refresh your test browser window first.

That’s it! You’re done!
Conclusion
Congratulations! You’ve successfully added the CaptureIntent intent on the back-end of an Alexa skill. Your Alexa Interaction Model is complete, and your custom Alexa skill development journey is off to a great start!
Next, I recommend you follow Amazon’s Tutorial: Build an Engaging Alexa Skill. It will lead you through many of the same concepts introduced in this series, but in more depth.
Hopefully, the My Zoo series makes the Amazon tutorial a richer experience because you now have hands-on experience, giving you better context as you study new Alexa development concepts.
The My Zoo series continues to explore Alexa Skill Kit (ASK) features, and hints & tips that make developing quality skills. Continue with the next post: Add Persistent Data on Amazon S3 to a Custom Alexa Skill.
If you have corrections or suggestions for this tutorial series, please add a comment below.
Related Resources
Alexa Skill Kit (ASK)
Tutorial: Build an Engaging Alexa Skill
Leave a Reply