Intro and Skill Logic - Alexa Skills w/ Python and Flask-Ask Part 1





Welcome to an Alexa Skills tutorial for Python programmers. In this tutorial, we're going to go through the making of a quick skill for an Alexa device.

To follow along, you will need an Alexa enabled device, ngrok or an https enabled server, and an Amazon Developer Account. Do note, this account (the account you're using in the developer console) needs to be the same account that you paired your Alexa device with when setting it up.

Got all of that? Great, on to the Python! We're going to use Flask-Ask, which is a Flask-extension that will make skill creation a bit easier. To begin, open cmd.exe/a terminal and do: pip install flask, pip install flask-ask. Also, since we're going to be using unidecode, do a pip install unidecode

Now, we can begin our Python script. Since Flask-Ask is a Flask extension, the code looks just like a Flask web app might. For example, here's a super simple Flask app:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def homepage():
    return "Hi there, how ya doin?"

if __name__ == '__main__':
    app.run(debug=True)

Wherever we have app routes, these are decorating our functions and basically assigning those functions to actual web app pages in a typical Flask App. This basic app, whenever someone goes to the homepage, will simply just output "Hi there, how ya doin?" An Alexa skill, being an app itself, also works similarly to a webapp, only, instead of clicking buttons to get to new "pages," the user verbally communicates with the device and the choices a user makes will take them to a new function, or even close the app itself. You can run any Flask app locally on the development server that comes with Flask. Run the code above, and you will have a server hosted at 127.0.0.1:5000. You can visit that address in your browser.

That's all I will say on the basics of Flask, but, if you would like to learn more about Flask specifically, you can check out the Flask tutorial series. This tutorial website, for example, is built with Flask!

Now, with Flask-Ask, since it's an extension of Flask, we can actually just build the Alexa skill on top of our Flask application, or we can totally ignore a Flask application and just write code for the skill. We'll leave the homepage code there for now, but that's all we'll do for the webapp. You do not need any webapp code for the Alexa skills to work.

For this tutorial, we're going to work purely on the Flask-Ask logic, and then in the next tutorial, we'll build the Python function that will actually do something for us. This skill is going to head over to Reddit's worldnews subreddit, and grab the headlines.

To engage with Alexa, you usually say something like Alexa, start "something". This is what actually launches the skill. Our Flask-Ask code for this can be:

@ask.launch
def start_skill():
    welcome_message = 'Hello there, would you like the news?'
    return question(welcome_message)

Notice the decorator here is @ask.launch. This means, when the app is first launched, the user will wind up here. This is basically the Skill's "home page." Whatever you put in here is what the user will first encounter. For us, we're just going to start the skill, specify a welcome message, and then return a question, which is a specific type that we imported from flask-ask above (we are importing both a statement, and question). Since the introduction asks if the user wants to hear the news, then this is a question, and we're going to need input from the user to know where to go next!

Now, when it comes to interacting with a user, Alexa Skills refers to these interactions as "intents." Thus, let's consider an example where a user says "yes" when asked if they want to hear the news. We'll call this a YesIntent. Now, in our decorator, the "path" to handling the yes intent would be: @ask.intent("YesIntent"). Our function would be something like:

@ask.intent("YesIntent")
def share_headlines():
    headlines = get_headlines()
    headline_msg = 'The current world news headlines are {}'.format(headlines)
    return statement(headline_msg)

If the user says "yes," we're going to tie that answer to this function, which will grab the headlines, build a single string out of them, and then share these headlines with us. Notice that, in this case, we're returning a statement. When this statement is done, there's nowhere else for the application to go, and it will close. What if the user says "no?" We'll handle that too:

@ask.intent("NoIntent")
def no_intent():
    bye_text = 'I am not sure why you asked me to run then, but okay... bye'
    return statement(bye_text)

Now we have all of the Python logic required to work with the Alexa device. Now we actually need to create the get_headlines function, which is where we'll use Python to actually get the headlines from Reddit.

The next tutorial:






  • Intro and Skill Logic - Alexa Skills w/ Python and Flask-Ask Part 1
  • Headlines Function - Alexa Skills w/ Python and Flask-Ask Part 2
  • Testing our Skill - Alexa Skills w/ Python and Flask-Ask Part 3