Skip to the content

(5) Let's get talking ... First Steps with Alexa

So, onto Alexa itself, no way we can get to control the train until we have a good handle on how to program that voice from the black tube (or black ‘tub’, if you have an Echo)! Working with Alexa isn't tricky, you do need some programming experience and the programming language of choice is a variant of JavaScript called Node.js but other languages like Python are supported. Oooh by the way, this episode of the blog is rather techie, if you are here from the model railway community and haven't programming background please bear with me. I will pay homage to trains, I promise.

Right, to the business. The following comes from a few days tinkering and with the benefit of an excellent Alexa workshop I attended in Cambridge. The workshops are free and I certainly recommend it if you intend to get into Alexa and Co. Workshops are currently held in Cambridge and London, hopefully they will come to Edinburgh one day too.

For this article we are going to create a simple Alexa skill, along the lines of a the popular 'tell me a fact' type of requests. There are templates and examples to do this, it's not rocket science but will help us get a handle on terminology and to learn how things fit together. In software development, that is so often half the battle, especially when you are dealing with interconnected systems. Ok, so what fact to do, well I suppose there is the railway theme here so let’s do 'Railway Facts'. We will create skill that will speak a random interesting snippet when you ask Alexa 'tell me a railway fact'.

There are two components to creating an Alexa skill, we need to define the dialogue within the Alexa development console then this connects to an 'end point' which processes the dialogue and issues responses. The skills defined in the development console act as the ears and interpret what you are saying whereas the endpoint is the brain and responds accordingly. In our case the brain is simply returning a random fact but could be as simple or complicated as you wish.

I'm not intending to delve into the nitty gritty details here, there are excellent tutorials at the Amazon site, I will provide some useful links at the end of the article (or attend a workshop, like said strongly recommend). Let’s however kick off with the ears, the skill defined in the Alexa Development Console.

First thing is to create a skill, then in the skill I need to tell it what the invocation name will be. This is phrase that will use used after the 'Alexa Ask ...' and must be two words or more. A useful snippet learned at the workshop is that you can't use single words, since they are reserved for trade names. Our innovation name is, of course, 'Train Fact'.

Train Fact Alexa invocation name as shown in the console

The simplest skill contains one of more 'utterances'. These are the words that tell Alexa what to do. Alexa is really rather clever and uses what are called 'Slots' to fill in details for variable values, like when you ask 'Alexa, set a timer for seven minutes', the 'seven' is a slot as it can take any value. You wouldn't create an utterance for every single possible timer value, that would be stupid! For our purposes here we are not using slots, just simple utterances such as 'tell me a fact' and 'tell me a train fact'. See the illustration below for those that we have chosen to use. We could add more, but these are more than enough for the example.

Some example utterances that will be used to illicit a response to a train fact

Now to introduce the 'end point'. End Point is just a fancy way of referring to somewhere that processes the utterances and for this exercise we will be using Amazon's own built-in endpoint where the logic for returning facts is handled by functions called 'Lambdas'. These are created in another Amazon console designed for this purpose.

Within the lambda are all the responses. I have been a bit cheeky and taken some random facts off Wikipedia. Well, it it's good enough for journalists, who do it all the time when they want to pretend to me knowledgeable, it's good enough for me. And this skill will not be going public, just for me to use and demo.

I will spare you the details but see illustration that see that we simply enter the answers in an array (for the non techies, an array is list a just of things) and the Lambda code picks one at random whenever it gets a request from the skill. There is some simple 'wiring we need to do, the skill needs to know where these Lambdas live, and a couple of small things that I will conveniently sweep under the carpet.

And so here we are. Now all we need is to test. We can test within the console, watch the video attached to see it working on my very own Alexa. Thank you for reading and hope to see you soon, for the next exciting episode when we'll start getting into the nitty gritty of interfacing with the railway.


Alexa Train Skill Demo from Peter Thurston on Vimeo.


comments powered by Disqus

Contact us for more information

Get in touch for a friendly chat to see how we can help, or simply to ask a question without any obligation.