Posted 20/8/2018 by Andrew Fairlie
Voice assistants are everywhere. They're taking over. Bit by bit they're replacing us, and I for one want to be on the robot's good side when they become our overlords.
So in this post I'll be showing you how to make a little voice assistant bot powered by Craft CMS.
This is the first of a four part series that will walk you through building a simple bot and then a more complex bot with contexts and multiple intents. It will end with a summary on how to publish the bot and get it in the hands (or voices?) of your users.
Dialogflow is a service from Google that allows you to built conversational bots for Google Assistant and Amazon Echo. But it also lets you make Facebook Messenger, Twitter, and Slack bots too, so you can adapt this guide to work on pretty much any integration that Dialogflow supports.
You need to be able to serve your Craft site on a publicly accessible URL protected by a signed SSL certificate.
In part 1 to keep things simple you'll need to disable CSRF protection in your config.php to allow Dialogflow to speak to our site. So be sure to do this in a safe environment. This is not advised to be done on a real life website, and we'll re-enable it in a later part.
These instructions are for Craft 3, but can be easily adapted for Craft 2. For example, in this part the only thing you'll need to change is .one() for .first()
We're going to make a simple assistant that will return a random fact. "Tell me a random fact!"
Make a new channel called "Fact" with a text field, "fact". Add a few entries to the channel to get the ball rolling. Simple!
Dialogflow will need to speak to our website to get the information, so we'll need to make a new Twig file to reply with JSON.
We'll call this file /api/random-fact.json
First up, make your free Dialogflow account by going to the Dialogflow Console.
Dialogflow listens to the user's voice and finds the most appropriate 'intent' for it. An intent are really just different kinds of questions. For example, a cinema might have the following intents...
Firstly we need to enable the webhook. You can do this by...
Now that we have our webhook...
On Dialogflow's right hand sidebar, you can press "See how it works in Google Assistant" to test the app.
You will first need to say "Talk to my test app" and then you can say "Tell me a random fact", "Fact me up!" or any other training phrase you gave it.
Have fun and play with the machine learning. Say something like "Give me a fact, you robot scum".
For now we're in charge, so it will probably answer you, but be careful, once they take over that kind of language won't fly.
Follow us on Twitter @madebymutual to get notified as the next parts get released over the next few weeks.