Alexa Meets Slack

HarmonizeHQ
Chatbots Magazine
Published in
7 min readAug 4, 2017

--

Alexa’s vessel — Amazon Echo

Surely by now you have tried a few Slack bots. Those nifty little add ons simplify workflows, alert you on critical incidents, manage your presence and basically make Slack, Slack++. It’s hard to imagine a functional workplace without Slack and its army of bots that do your bidding. On the home front, this new toy, Alexa (in the form of its vessel, Echo), is now present in more than 10 million households where it lets people order stuff, get news digests, play music and is taking us all closer to the future where voice is the main user interface.

We at Anaek, are excited by how technologies used in ones personal and work lives are slowly but surely converging. To this end we envision plenty of voice powered apps at work. This post talks at length about a cute little hack that is a tiny step towards that. In this tutorial I build a skill for Alexa called Slacker, that allows one to update their Slack status by talking to Alexa.

Getting started with Alexa skills

There are plenty of hello world tutorials on writing your first Alexa skill. Most of them involve running a simple view on a thin web server and then some configurations on AWS console. This post is going to be a lot more detailed than that.

Before we get into it, here are three terms you will encounter in this post regularly.

1. Intents — Specific actions that identify the key operations to be performed
2. Utterances — Trigger words/phrases that when spoken, trigger specific intents
3. Slots — Think of these as variables that hold user provided values in a spoken phrase

That out of the way, let’s get on with putting together our Alexa skill. I am going to put focus on the Alexa skill aspect of this hack and just cruise over the details of the Slack integration. If you want to learn more getting started with Slack apps and bots, you can do that by reading this post.

Registration and stuff

Before we get cracking with the code, we’ll need to do a few administrative things. Head to the AWS developer console and create a new account. If you already use AWS for any of it’s services, you can login with the same account. If you have a registered echo device then use the same credentials to login here as it will make it easy to test out the skill later.

Once logged in, click on Alexa and then select Alexa Skills Kit. In this page, click on the Add a New Skill button to start creating a new skill. Select Custom Interaction and provide a name to your Skill, I have called it Slacker. You’ll also need to provide an invocation name, something that brings this skill into action, like when you say ‘Alexa, open Slacker’, Alexa will wake up and start executing your skill matching the invocation name “Slacker”. You just need type Slacker. It is assumed that words like run, open and ask will be used by the person interacting with the app. Next, click on Save. This is all we need for now. We will come back and finish the rest of this later.

Amazon Dev Console

Designing the skill

Let’s design the skill in terms of Intents, Utterances and Slots. For the sake of simplicity, let’s start with two intents below.

Launch
Technically, this is the intent to just bring up the Skill so you can start interacting with it. Any skill you write will have at least this intent. A simple hello world skill might just respond to the launch intent. We will not stop at that. When launched, Slacker will greet the user and ask the user what she wants posted as their Slack status.

PostUpdateToSlack
This intent starts by asking the user what she wants posted on Slack, receives the response, posts it to Slack and then terminates itself after responding to the user.

The following utterances will be accepted and understood as a response to this intent.

{update}
post {update}
post {update} to Slack
post {update} on Slack

The part inside the curly braces ‘update’ is the Slot. When Slackers asks you what to post and you respond with “post dancing in Hawaii to Slack”, the part “dancing in Hawaii” will be the value of the Slot.

The code

Now on to my favorite part. I will use Django 1.9 on Python 3 as the web framework to run our skill. There is a very nice django library, not surprisingly, called Django Alexa that is a great way to build an Alexa skill. It also has a bunch of utilities that allows us to build the Interaction model and Slots that we will have to register on the Amazon dev console for the skill we previously registered.

I am going to assume a basic working knowledge of Django in this tutorial. We will begin by installing django-alexa in our virtualenv. Add the following to the requirements.txt

git+https://github.com/pycontribs/django-alexa.git

This is to ensure we build the latest django-alexa from the repo. After adding this, update your requirements by

pip install -r requirements.txt

and then add the django-alexa app in your INSTALLED_APPS in settings.py.

INSTALLED_APPS = [..
‘django_alexa’

]

We will also need to add alexa urls to our urls.py

urlpatterns = [..
url(r’^’, include(‘django_alexa.urls’))
]

Now let’s start coding our skill. We will add a file alexa.py anywhere in our django app.

In the code above, we have defined the two intents we designed previously. LaunchRequest simply responds to the user and the PostUpdateToSlack accepts the “Update” slot and posts it on Slack. You will notice the definition of LaunchRequest and PostUpdateToSlack are slightly different. The PostUpdateToSlack intent accepts an additional parameter called update. This is the slot value uttered by the user when this intent is invoked. The LaunchRequest simply responds to the user asking for what to post. The PostUpdateToSlack, accepts the user slot input and uses the Slack api (I told you I am going to cruise over this part) to post the same message on a specific channel. You could also use the status update api to set the status for the specific user.

Now let us run our Django server and see if the intents we have developed are working. Django-alexa is basically creating a new view for every intent and all these views by default are available at /alexa/ask/. Point your browser to localhost:8000/alexa/ask/. You should see a TemplateNotFound error. Don’t worry about that.

Finishing the skill

Now, let’s go back to the Amazon developer console. Click on Alexa and then Alexa Skills Kit and look for your registered skill. Keep this page open, we’re going to come back to this real soon.

On this page, copy the Application Id for the skill and set it up as an environment variable for your Django app.

export ALEXA_APP_ID_SLACKER=app_id

Now run the following on your django console

python manage.py alexa SLACKER

This will spit out a bunch of stuff.

#### SCHEMAS FOR SLACKER ####
{
“intents”: [
{
“intent”: “LaunchRequest”,
“slots”: []
},
{
“intent”: “PostUpdateToSlack”,
“slots”: [
{
“name”: “update”,
“type”: “STATUS_LIST”
}
]
}
]
}

STATUS_LIST:
busy
away from keyboard
in a meeting
working remotely

LaunchRequest launch
LaunchRequest start
LaunchRequest run
LaunchRequest begin
LaunchRequest open
PostUpdateToSlack {update}
PostUpdateToSlack post {update}
PostUpdateToSlack post {update} to slack
PostUpdateToSlack post {update} on slack

#####################################

This has two things we are concerned with. Copy the part after “SCHEMAS FOR SLACKER”. Now let’s go back to the Amazon dev console we had opened before and paste this in the text area under “Intent Schema”. Copy the part under “STATUS_LIST”. Enter STATUS_LIST under Custom Slot Type and then paste the statuses in the text area under it. Now copy the rest of the part (from PostUpdateToSlack {update} to the ###) and paste it under “Sample Utterances” and click “Next”.

On this page, we are going to hook our Django server to the skill setting on Amazon dev console. Keep this page open, we will come back to it in just a moment. We need to allow our local development server to be accessible by Amazon Alexa servers. One way to achieve that is by using ngrok that allows you to offer external client a secure way to access your development server. Just install ngrok and then

./ngrok http 8000

This will give you a set of Forwarding urls that you can share with the external client. Copy the https url.

Now let’s go back to page on the Amazon dev console (or click on Configuration in the sidebar) and select the HTTPS option under Endpoint and click whichever region is geographically closer to you. Enter the https url you copied from ngrok here, appended by /alexa/ask/, like

https://xxxxxx.ngrok.io/alexa/ask/

Testing your skill

Now click on “Next”. At this point your skill is ready and there are two ways to test it out.

If you have an echo device with you, you will find the Skill in “Your Skills” section of the Alexa app. You can install that Skill and try it out by speaking “open Slacker”. If all goes well you should be able to interact with your skill and post one of the status updates to the specified channel. You will be able to view the web calls coming in to your web server.

The alternate way to test out your skill is by going to the “Test” section from the sidebar of your Amazon dev console. Here you will be able to type in the text words that to see how an actual echo device will respond. Just type in the words under the Service Simulator section.

That’s all folks! A simple Alexa powered Slack integration that lets you post updates to Slack. Have any more cool ideas around Slack and Alexa, share them in the comments.

Checkout what we do at Anaek, when not tinkering around.

--

--