Over a million developers have joined DZone.

Exploring Voice Control: Implementing Amazon Alexa Via Flask-Ask

DZone 's Guide to

Exploring Voice Control: Implementing Amazon Alexa Via Flask-Ask

Let's build a quick and easy Alexa skill using a custom HTTPS server (Flask-Ask in this example) rather than AWS Lambda to explore the ins and outs of voice-based UX.

· IoT Zone ·
Free Resource

As Amazon's "Alexa loses her voice" Super Bowl commercial reminded us, Alexa has become an increasingly integral part of our daily lives. According to a Voicebot report, the Alexa skill count has now officially passed 25,000 in the United States, and Amazon reported selling "tens of millions" Alexa-enabled devices over the holidays. In addition, late last year, Amazon announced the launch of in-skill purchases that provide developers a route to earning money from the skills they've developed. In other words, if you haven't already started thinking about how you can implement Alexa in your own technologies, now is the time to start.

At Distillery, we've been fortunate to help several clients with Alexa implementation over the past year (e.g., check out our blog on nēdl, which lets you ask Alexa to quickly search thousands of live streams worldwide). Since we like sharing the fruits of our labors in the form of how-to guidance and advice, we're also reposting the Alexa-focused blog below. Written by one of Distillery's developers, the article was first published on Distillery's blog in August 2017.

On one of our recent Distillery projects, we faced an interesting dilemma: how to implement voice control for the system we were building. After searching for an appropriate service, we ultimately settled on Alexa, Amazon's voice service.

Overview of Amazon Alexa

Unlike other representative voice technologies such as Cortana, Siri, and OK Google, Alexa provides a wide-ranging API which can be used not only on mobile devices, but also in web applications. Moreover, there are SDKs and other development solutions available for platforms and programming languages such as Java, Python, and Node.js, so it wouldn't be difficult to implement Alexa for your basic app. Besides, Alexa is an Amazon product and can be used cooperatively with other Amazon services (e.g., AWS Lambda).

Building Skills and Handling Responses via Flask-Ask

The first step is to create an Alexa skill. You can find the full walkthrough of this process here, so we won't focus on that in this blog. Instead, let's examine how an Alexa skill actually works. There are two main components of any skill: intents and utterances. Intents are the callbacks that associate human speech with an event in our system, and utterances link the intents in our system to certain boilerplate phrases and their variations.

There are two possible approaches for handling Alexa intents. First, as mentioned before, one option is to use AWS Lambda. Another option is to use a custom HTTPS server. For educational purposes, let's select the second option and use Flask-Ask. Flask-Ask is a web server based on Flask (a Python microframework) which can be used as an endpoint for Alexa's intents. It uses the same approach as that used in working with Flask routes: we put the name of the intent in the Alexa decorator and then create a method that handles the response.

def create_appointment():
   """ Initialize appointment's creation workflow; Pass to date definition """

   msg = render_template('date')
   return question(msg)

As you can see, the method returns a rendered template wrapped in a question() function. This happens because every intent handler should return one of two interaction states:

  • Question: Supposed to be the phrase that will grab the next intent
  • Statement: Supposed to be the completed phrase without any further communication

Beyond the name of the intent, a Flask-Ask decorator accepts parameters mapping. You can map it to a different variable name or to a Python data type:

# Mapping to a different variable name
@ask.intent("AMAZON.AddAction", mapping={
       'ownerName': 'object.owner.name',
def add_new_object(ownerName): 
return question(render_template(‘Hello’))

# Mapping to a Python data type
@ask.intent("CMAppointmentDateIntent", convert={'begin_date': 'date'})
def appointment_date(begin_date):
   qs = render_template('time')
   return question(qs)

Ultimately we were successful in building voice control into our app using Flask-Ask, solving the dilemma in an efficient, productive way.

Potential Issues

The possible issues we've uncovered thus far relate to the potential inability either to completely override some of the default intents, or to avoid calling up some of Alexa's built-in services.

During the development of this small application, it was noted that the more the utterances include words that are connected to default Alexa skills, the more frequently it calls the default intent instead of your custom intent. To avoid this issue, you can add unique words for every utterance in your list, thereby decreasing the chances of unintentionally calling the preinstalled intents.

A related issue involves unintentional linkage to a different Amazon service. If the utterance contains words that are included in default skill utterances or somehow connected with one of the built-in services (e.g., calendar or to-do), there's a high probability that it will call the predefined intent instead of your skill.

The last issue relates to the handling of regular human speech. One solution would be to use the AMAZON.LITERAL slot type, but this approach is pretty labor-intensive. According to the documentation, you need to specify as many "anchor" words as possible that allow this intent to be called. However, even if we set up all possible utterances, we'll face an issue with Flask-Ask. During testing, it was revealed that these phrases (which should be examined with Alexa) are not received by the Flask-Ask intent handler. Besides, it's possible to create your own slot type using the samples of available values, and this may solve the issue.


Amazon Alexa is an intriguing technology which has the potential to transform our basic approach to UX and user interfaces. Voice control is currently on the cutting edge of technology, so it's crucial for businesses to heed this trend and (where appropriate) use voice control to enhance the ways they interact with their customers. However, voice control isn't flawless, so as you make your strategic choices, it's crucial to pay close attention to the intended use of your Alexa application.

iot ,alexa skills development ,flask-ask ,python ,intents ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}