Flask-Ask Introduction
Flask-Ask is a new python framework for rapid Alexa Skill development created by Alexa Champion John Wheeler. Flask-Ask is a Flask extension that makes building the voice user interfaces with the Alexa Skill Kit easy and fun.
Create the Skill
pip install flask-ask
Next we will create a new file called game.py
import logging
from random import randint
from flask import Flask, render_template
from flask_ask import Ask, statement, question, session
app = Flask(__name__)
ask = Ask(app, "/")
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
@ask.launch
def new_game():
welcome_msg = render_template('welcome')
return question(welcome_msg)
@ask.intent("YesIntent")
def next_round():
numbers = [randint(0, 9) for _ in range(3)]
round_msg = render_template('round', numbers=numbers)
session.attributes['numbers'] = numbers[::-1] # reverse
return question(round_msg)
@ask.intent("AnswerIntent", convert={'first': int, 'second': int, 'third': int})
def answer(first, second, third):
winning_numbers = session.attributes['numbers']
if [first, second, third] == winning_numbers:
msg = render_template('win')
else:
msg = render_template('lose')
return statement(msg)
if __name__ == '__main__':
app.run(debug=True)
Now create templates.yaml file in the same location as game.py because Flask-Ask let you separates the code and speech with templates.
welcome: Welcome to memory game. I'm going to say three numbers for you to repeat backwards. Ready?
round: Can you repeat the numbers {{ numbers|join(", ") }} backwards?
win: Good job!
lose: Sorry, that's the wrong answer.
Now the Skill is ready to run:
python game.py
A development server launches on http://127.0.0.1:5000/, and the skill is almost ready to configure in Amazon's Developer Portal. The skill can be run behind a public HTTPS server or AWS Lambda function. Here we are going to use HTTPS server with ngrok. Download ngrok client for your operating system here. Unzip, open terminal and cd into the location and enter:
./ngrok http 5000
ngrok displays a status message similar to the one below. Note: The status message you see will be different.
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Session Expires 5 hours, 34 minutes
Version 2.2.8
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://850f662c.ngrok.io -> localhost:5000
Forwarding https://850f662c.ngrok.io-> localhost:5000
CONFIGURE THE SKILL
Logged into Amazon Developer Account, select alexa, Click "Add a New Skill" button.
Skill Information Setting:
1. Leave the "Skill Type" set to "Custom Interaction Model"
2. Enter "Memory Game" (without quotes) for both the "Name" and "Invocation Name" fields.
3. Intent section, click "Add Intent" and enter IntentName
4. Copy the utterances below into the "Sample Utterances" field:
YesIntent yes
YesIntent sure
AnswerIntent {first} {second} {third}
AnswerIntent {first} {second} and {third}
Configuration settings:
1. Make sure the HTTPS radio button is selected for the "Endpoint" field
2. Enter the HTTPS endpoint from ngrok into the textfield
3. Don't bother with "Account Linking"
Test the Skill :)
Now, it's time to test your skill with the following interaction sequence with your Alexa-enabled device. If you need a test tool, try EchoSim.io.