Building the City Cinema Twitter Bot

Remember that Amazon Alexa Skill that I built for City Cinema? Well it’s now generally available: if you have an Echo device anywhere in the world, you can enable the “City Cinema” skill.

My usual coding practice when hacking away at things like this is to write horribly-organized spaghetti code to see whether things are going to work at all, and to understand the underlying patterns, and then to loop back and create more elegant, reusable code once I’ve got some more insight. I’ve now done the loop-back for the code that powers the City Cinema schedule, building a PHP class that allows me to do things like this:

<?php
$c = new CityCinema('whatsPlaying', '2017-03-15);
$message = $c->speakable;
?>

(which will set $message equal to a speakable version of the City Cinema schedule for March 15, 2017).

The great thing about reusable code is that it’s, well, reusable. Meaning that when I went to explore the now-Google-owned API.ai platform for developing conversational interfaces, I could use the same PHP class to act as the back-end webhook for that too, with almost no modifications.

API.ai is a considerably more sophisticated platform than Amazon’s Alexa Skills Kit: coding for API.ai seems a lot less like coding for ELIZA and more like using a tool that’s custom-tailored for this task.

So, for example, the UI for creating a new “intent” looks like this in API.ai:

Screen shot of the API.ai intent creation screen.

Rather than needing to spell out, in JSON, all of the various utterances that I want to glue to my business logic, I simply specify some sample phrases, highlight where the entities that are required appear in them (connecting “Tuesday” to the @sys.date entity, for example), and API.ai takes it from there; even though I didn’t specify it in my examples, there’s enough to be derived from these that I can say “Ask City Cinema what’s on tap for Wednesday” and it will figure out what I mean.

One of the nicest features of API.ai is that it supports multiple conversational integrations from the same setup, and these are very easy to deploy.

For example, once I had the Google Actions integration working (to allow me to support Google Home and Google Assistant), I was able to roll out a Twitter bot in about 5 minutes; the only change required was to my back-end webhook, where I changed “do you want to hear a description” to “do you want a description” when the source was Twitter.

As a result, if you send a Twitter Direct Message to @city_cinema, you can now ask it the same questions you can ask Alexa and Google, like “what’s playing tonight?” or “what’s running on Sunday?” And I’ve added a couple of conveniences too, like “what’s your location?” and “how much are tickets?” Here’s what it looks like in action:

 

Screen shot of the City Cinema Twitter "bot" in action showing a dialog where I ask it questions and it responds.

Give it a try!

Comments