Chat bots and how to build one on Alexa

Krishna Teja
spawn-ai
Published in
4 min readNov 3, 2016

A lot of people were very astonished and awed when Apple announced Siri, a voice assistance unlike any other which could understand and carry out the task for you. Now almost every leading software manufacturing hub out there has it’s own AI assistant which work just like or even better than Siri. So what is the gig all about now?

Talking to technology has taken a whole new level since Amazon has announced their voice assistant Alexa and opened up their platform for developers to build custom bots just like when Apple announced about app store for developers to create and sell apps.

Here I wanted to talk about the ease of building an Alexa skill using python which could be used as information provider to a attendee for a conference.

Prerequisites to building an Alexa skill is that, you’ll need Python installed. If you’re on a recent version of OS X or Linux, Python comes preinstalled. You may also need to install pip, which can be found here. On Windows, follow these installation instructions.

The bot uses a MySQL database to store the information about the conference and uses the following python modules:
1. Flask-Ask (https://flask-ask.readthedocs.io/en/latest/)
2. Flask
3. flask-mysql

You can install these modules by running the following command,

pip install flask-ask flask flask-mysql

Now create a file called conference.py, and use the snippet of the code I have written to build your server side logic.

__author__ = 'krishnateja'

import
logging
from flaskext.mysql import MySQL
from flask import Flask, render_template
from flask_ask import Ask, statement, question

mysql = MySQL()

app = Flask(__name__)
app.config.from_object(__name__)
app.config['MYSQL_DATABASE_USER'] = 'xxxxxxx'
app.config['MYSQL_DATABASE_PASSWORD'] = 'xxxxxxx'
app.config['MYSQL_DATABASE_DB'] = 'xxxxxxxx'
app.config['MYSQL_DATABASE_HOST'] = 'xxx.xxx.xxx.xxx'
mysql.init_app(app)

ask = Ask(app, "/")

logging.getLogger("flask_ask").setLevel(logging.DEBUG)


@ask.launch
def new_conference():
welcome_msg = render_template('welcome')
return question(welcome_msg)


@ask.intent("SpeakerIntent")
def ask_if_speaker(speaker_name):
speaker_name = speaker_name

cursor = mysql.connect().cursor()

cursor.execute(
"SELECT * from conference.talk JOIN conference.speaker "
"ON conference.talk.speaker = conference.speaker.speakerid "
"JOIN conference.slot "
"ON conference.talk.slot = conference.slot.slotid "
"WHERE conference.speaker.name LIKE '%"
+ speaker_name + "%';")

data = cursor.fetchall()
if data is None:
message = "There was no speaker by that name."
else
:
hours_start, remainder = divmod(data[13].seconds, 3600)
minutes_start, seconds_start = divmod(remainder, 60)
time_start = str(hours_start) + ":" + str(minutes_start)

hours_end, remainder = divmod(data[14].seconds, 3600)
minutes_end, seconds_start = divmod(remainder, 60)
time_end = str(hours_end) + ":" + str(minutes_end)
message = "Session that you are looking for is " + data[
2] + " which starts at " + time_start + " and ends at " + time_end + ", given by " + data[9]

return statement(message)

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

Flask-Ask lets you separate code and speech with templates. Create a file named templates.yaml in the same location as conference.py, and add the following to it:

welcome: Welcome to the Phoenix Data Conference. How can I be of help?

This is all the code that is needed to create a skill on Alexa.

Run Python Code:

python conference.py

This starts a server on localhost:8080. For registering this skill in the Amazon’s Developer Portal the skill must run behind a HTTPS server or must be hosted on AWS lambda function for which you will require to have a Amazon Web Services Account. There can be a work around for this, by generating a public HTTPS endpoint for localhost:8080 using the ngok client from https://ngrok.com/download and run,

on Unix/Mac:

./ngrok http 5000

on Windows:

ngrok.exe http 5000

Configure the Skill in Developer Portal:

Once logged into the Amazon’s Developer Portal click on Alexa and start creating a new skill.
Give your skill a name and a unique invocation name. This invocation name is used to start your skill.
To keep it simple let us assume that our speakers have American names so in our intent schema put in:

{
"intents": [
{
"intent": "SpeakerIntent",
"slots": [
{
"name": "speaker_name",
"type": "AMAZON.US_FIRST_NAME"
}
]
}
]
}

and Sample Utterances

SpeakerIntent which session is {speaker_name} presenting
SpeakerIntent what is {speaker_name} presenting
SpeakerIntent is {speaker_name} presenting

For the configuration setting:

  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”.

The rest of the settings are fairly straight forward after which you can publish you skill to you Alexa device.

You can go fairly fancy with this application and also improve it to customize it for your conference.

Here is the demo of the AI we spawned for the Phoenix Data Conference held in Phoenix,AZ this year.

NOTE: This is a little more involved conversational bot, where we maintain session variables and have different ways to go around with interacting with the user. You can contact me directly via LinkedIn to get more info on the skill or help with the idea you want to work on.

Special Thanks to John Wheeler for developing flask-ask to help us developers.

Keep practicing, keep reading, and most of all, keep iterating; it’s the later versions of bots, in my experience, that add the most value.

For a better future! PEACE!

--

--