Alexa, When's the Bus?

I got an Amazon Echo for my birthday (from my husband, who took romantic to a new level when he liked my present so much he bought me an Amazon dot a week later so he could use the echo elsewhere in the house!), which is a new gadget for us. Of course I started asking her questions that she couldn't answer ... and you can write your own "skills" so of course I sat down to browse the documentation and ended up creating a working skill for her :) It was a fun process but there were lots of unfamiliar parts to it so I thought I'd blog what I did in case anyone else wants to try out creating skills as well, and in case I ever want to remember some of the stuff I know now!

Create the Skill

Once your Alexa echo/dot is working, visit the developer portal and create a new skill. I hadn't used Amazon Lambda before but if you choose this option for your skill, it integrates well with Alexa and the security side is pretty much done for you so I picked this option for my first skill. Lambda is a serverless technology, and I wrote my skill in Javascript. When you're developing a skill, it's available on your account only (so all the devices in the house are attached to my amazon account) until you publish it - which I probably won't until I've removed the hardcoded URL for the bus stop nearest to my house!

Write Some Code

With the skill created, it's time to write some code! I started with this detailed introduction post from Brian Donohue which includes sample code and instructions that got me to the point of being able to shout "Tell Lorna ping" at Alexa (my skill is called "Lorna") and have her reply "ping ack". At this point I have both the lambda function and the Skill itself in place, and I needed to make it do something useful.

What I wanted to do: get a list of the imminent buses from the stop near my house. It's really helpful to know if you should run or if there's no bus for 10 minutes and you have time to also put the towels in the washing machine or talk to the cat or whatever. I know that 10 minutes waiting for a bus seems reasonable in many situations but I live near a main route into a European city so my standards are different :)

By watching what happens when I request the bus stop information on my phone, I was able to get a URL to a specific webpage with the current departures on it. This is a pretty fragile setup; I scrape data from a web page that wasn't intended for this purpose and which might vanish at any moment, using a regex that really relies on no formatting changes in the page. However it does work and I'm comfortable with this for a hobby/personal project.

If you're looking for info on Leeds buses, try this: http://yorkshire.acisconnect.com/Text/WebDisplay.aspx?stopRef=[the bus stop number that also works with the SMS system]. It also has info on which buses have low floors, plus any extra announcements.

Update The Skill

Once I was able to make the web request and parse the results using my local node toolchain, I then added that functionality into my lambda script. I found that publishing the skill was a painful process (check for further posts on Amazon lambda because I'm changing my workflow and may have better advice soon) because I had to:

  1. Put the code into a web-based text editor (you can upload zip files to s3 and do something but I don't have this sorted out yet)
  2. Go Actions -> "Publish a new version"
  3. Go Triggers and set up a new trigger linking the Alexa Skills Kit to my function
  4. Go back to the Alexa Developer Console, click Configuration and update the version number at the end of the Lambda URN field

I'm also not particularly expert with Javascript yet so that didn't help at all! To debug problems with your Alexa skill, click the monitoring tab and then over on the right hand side there is a "View Logs in Cloudwatch" link which I found extremely helpful!

Creating Alexa Skills

I hadn't really intended to hack with the echo; it's a fairly complete product in itself and I normally prefer much more open source or at least less proprietary systems. However, there's one in my office and I had a itch to scratch! You can ask her "Alexa, how's my commute?" but that seems to give road traffic information rather than the public transport I was looking for. I found this was a very approachable project despite being a bit of a beginner with Javascript, so if you're considering creating a skill yourself, I say go for it! It took less time and fewer tears than I expected!

Tips, tricks, showing off your own skills: all welcome in the comments box.

Edit: A few people asked me about my actual skill code, so it's available on GitHub if you want it (you need to find a bus stop URL if you want to actually use the skill), as I say based heavily on the sample code from Brian.

One thought on “Alexa, When's the Bus?

  1. Pingback: Slight tangent, voice skills! - Level 13 Studios

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.