extrawurst's blog

Alexa Skill written in D

• programming, dlang, and alexa

As I wrote last week in my post about Alexa I started looking into developing custom skills for Alexa.

Custom Skills for Alexa are like apps for mobile devices. Amazon makes it easy to upload the source into their Lambda service. Custom Skills are webservices - everyhing happens in the cloud. Amazon Lambda lets you choose between:

When facing the options I remembered the article I read on the dlang forums about how to use a D application in there. AWS Lambda Functions in D

Why D then?

So why chose D over the alternatives ?

Blazing fast ? Yes this is meant in two ways: 1. fast compile time 2. fast execution time

The dlang was designed to be fast - written by a experienced C++ compiler writer it was created to be compiled in a faster way. And since D is a system programming language and even shares the same compiler backend as a lot of other system languages it is equally fast as C++ and co. Why is performance especially interesting for an Alexa Skill? Because if you host it on AWS Lambda you pay per use - this means you pay per CPU/memory usage.

Template programming for the sane - D has a meta programming capability that is so strong because it allows the average programmer to go wild in it. I am using this to delegate incoming requests to the right endpoint here and to create simple to use REST interfaces to existing services like openwebif

You are not familiar with D yet? Then I have some resources for you to start:

How does it work?

steps (tl;dr):

  1. prepare nodejs wrapper
  2. start up vagrant box
  3. build linux binary
  4. bundle zip for upload to lambda
  5. upload to lambda
  6. do a test invoke of lambda function
  7. setup alexa skill in Amazon developer console
  8. talk to a D application :)

Nodejs wrapper (0.)

This is necessary since Amazon Lambda does not natively support dlang in their service. I chose nodejs because I had the basis for the wrapper already in the mentioned “D in Lambda” article.

Vagrant and bulding … (1. - 5.)

Since Amazon Lambda is powered by linux machines we have to build a Linux binary of our D application. I am a mac and windows user, so I had to find a way to build and test my code on linux. Previously I would have set up a virtual box myself but I rememberd how tedious and error prone that is and decided to give vagrant a try. This makes the setup of exactly the kind of machine I needed a piece of cake:

Vagrant.configure(2) do |config|

  config.vm.box = "centos72"

  config.vm.provision "shell", path: "src/vagrant/install.sh", env: {
    "IN_OPENWEBIF_URL" => ENV['OPENWEBIF_URL'],
    "IN_AWS_LAMBDA_NAME" => ENV['AWS_LAMBDA_NAME'],
    "AWS_REGION" => ENV['AWS_REGION'],
    "AWS_KEY_ID" => ENV['AWS_KEY_ID'],
    "AWS_KEY_SECRET" => ENV['AWS_KEY_SECRET']
  }
end

As you can see it is based on a centos machine (that apparently is closest to the amayon AMI images) and only sets a couple of environment variables that we need to access AWS. The rest of the initialization of the box is delagated to a install.sh script that is run on first boot:

Using this box I have a new dev machine up and running after 2 minutes. Then it is just a matter of running the run.sh (see on github) in the box and it will compile, bundle, upload, configure and test the lambda function to and on AWS.

Setup alexa skill (6.)

This is a whole different beast unfortunately. Amazon does not yet support cli/remote control of their Amazon developer console. This means you have to setup the skill manually in a web interface. There are lots of tutorials on how to do that though and all the information needed to fill in the fields that my skill expects are in the repository: speech assets. Fortunately Amazon acknowledged this to be a big concern by developers and claims to be working on it.

One tutorial I found very good to follow by Amazon: Build a Trivia Skill in under an Hour

Talk to a D application (7.)

So what can this skill do for me now ?

Alexa is capable of controlling my televison tuner now and reacts to sentences like these now (although it is only in german currently):

Alexa, ask telly what is currently running
Alexa, ask telly for a list of channels
Alexa, ask telly what I recorded
Alexa, mute with telly
Alexa, switch to CNN with telly
Alexa, ask telly to go to standby
Alexa, start recording with telly
Alexa, ask telly to go to sleep in 30 minutes

You can find that skill on github: alexa-openwebif

comments powered by Disqus