Skip to main content
Tom Casavant Tom Casavant

Superpower Generator: Programming an Alexa skill with Reddit RSS Feeds

Over the past year, I've been programming Alexa skills* after I learned how to do it back in November. However, I've neglected to write a post on how to make one...until now.

*The one I'm creating in this post is this one. I have also made the following:

First off, I'll be doing this in a virtual environment on my Windows OS. So, if you want to follow along you'll have to set up Python, followed by Virtualenv (https://virtualenv.pypa.io/en/stable/userguide/).

Next we'll install the libraries needed for this project. Open up a command line terminal and create a virtual environment (call "virtualenv venv" in your desired directory) and activate the environment (call "venv\Scripts\activate"). Install the following using pip:

And now we can start programming...

As always, we'll start with all the import statements

from flask_ask import Ask, statement, question from flask import Flask, render_template import feedparser import random

Next, we need to create the flask app:

app = Flask(name) ask = Ask(app, "/")

A library we imported earlier, flask-ask, allows us to create the intents for the Alexa skill. There are a few intents that Amazon requires for their Alexa skills: Launch, Fallback, Cancel, Stop, and Help. So, we'll implement those first and get them out of the way.

@ask.launch def new_ask(): """Returns a welcome message when skill is turned on""" welcome = "Welcome to superpower generator! Ask for help, or just ask for a superpower." return question(welcome)

@ask.intent("AMAZON.CancelIntent") def cancel(): """Alerts the user that you are exiting the app""" return statement("See you later!")

@ask.intent("AMAZON.StopIntent") def stop(): """Alerts the user that the skill is stopping""" return statement("Goodbye, this skill is shutting down")

@ask.intent("AMAZON.HelpIntent") def helpme(): """Returns help commands for Alexa Skill""" return question("Use this skill by saying Alexa give me a superpower. What would you like to do?")

Keep in mind that when flask-ask returns a 'statement' then Alexa will say the statement and exit the app. When flask-ask returns a 'question' then following a statement Alexa will turn on it's microphone and wait for more user input.

Finally, we will create our main function to get a superpower from https://www.reddit.com/r/shittysuperpowers.

@ask.intent("SuperpowerIntent") def getPower(): """Gives a user a superpower from r/shittysuperpowers""" d = feedparser.parse("https://www.reddit.com/r/shittysuperpowers/.rss?limit=100") selection = random.randint(0,len(d['entries'])-1) return statement(d['entries'][selection]['title'])

if name == 'main': app.run(debug=True)

Basically, this function takes the reddit rss feed from this subreddit (an RSS feed can be generated by adding '/.rss' to the end of any reddit address) and extracts all the entries from the sub. It generates a random number from 0 to the number of entries in the feed (minus one). It then returns the title of the random post as a statement.

Now that we've written the code, we have to set up the alexa skill in the developer console at https://developer.amazon.com/alexa. Go to that web address and click "Alexa Skills Kit", and then click "Start a Skill". Finally, click "Create Skill".

Give your skill a name go into the Invocation section and give your skill an invocation name (or the name the user will say to activate your skill).

Head to the intents section of the developer console. Click the add button next to intents and create one called "SuperpowerIntent". Then type in some sample utterances such as the following:

Now we're going to head back over into our command prompt. Install a library called "zappa" using 'pip install zappa' (https://github.com/Miserlou/Zappa). Zappa will be used to deploy the skill on AWS. After installing it run the following commands:

"zappa init" (Follow through that process to create your zappa, I usually just choose the default selections for each option)
"zappa deploy dev" (This puts the skill on AWS, it will spit out a link to the skill when it finished running)

if you ever need to update your skill instead of using the above command use "zappa update dev"

Now that you have your url, go back to the developer console and select 'Endpoint' from the tabs. SELECT HTTPS (NOT AWS LAMBDA) and in the default region enter the url you were given by zappa. Save and go over into the testing grounds. If your skill works as expected go over into the launch tab and fill out all the information for your skill. (NOTE: When I tried to publish this skill the first time it was rejected because of the bad word in r/shittysuperpowers, I guess they have strict rules about that so you should watch out for it).

That's it, after a day or two your skill should either be certified or they will email you a list of reasons why they didn't certify it yet.

Webmentions

These are webmentions via the IndieWeb and webmention.io. Mention this post from your site: