This article used to be a mini ebook named “Conversational Interfaces: How to build clever Conversational Interfaces in a few hours”— a few hundred people got it for free and a couple dozen bought it at a very cheap price, but I figured Medium would be the most efficient way to reach people, so I since removed the book from Amazon.com & Kindle, updated and simplified it, and what you are about to read is the result!
Crafted by a developer, for developers, this article is written in a language better-suited for professionals with prior experience in programming that want to quickly be able to build conversational interfaces.
By the end of this book you will be able to produce and deploy chatbots that can run on Facebook Messenger, Slack or as standalone app.
I recommend reading somewhere with access to a computer so that you can practice the examples. And actually do practice the examples, it’s the best way to fixate knowledge.
It won’t be long, I promise! This article is written to be succinct and direct to the point, but there is one little thing you will need to do before we can start.
You will need to create an Amazon Web Services account, AWS is a subsidiary of Amazon.com that provides on-demand cloud computing platforms to individuals, companies and governments.
The reason why we need this is because we will be using Lex to build our conversational interfaces, and Lex is one of the many services provided by AWS.
If you are new to Lex, you can try it for free. From the date you get started with Amazon Lex, you can process up to 10,000 text requests for free for the first year, after that it costs $0.00075 per text request, what that means is that you will pay $0.38 — yes thirty-eight cents — at every five hundred times your bot is texted. Very friendly pricing, no doubts.
Yes, you read it right! If you follow this, you can build your own functional chatbot in under 15 minutes! Not the cleverest one, but ought to do it for now.
The final result is something like this:
Oh, and full disclosure: You will only type 11 lines of code to build this incredibly simple but reasonably scalable bot. Excited yet?
Log in to the AWS account you previously created and look for Lex within the services tab, should look something like this:
Click on “Create” to create a new bot, the configuration of your bot should be as follows:
I would explain all of this but we’re running against the clock here, 15 minutes, remember?!
Output voice — Amazon Lex allows you to create more than text-based bots. You can automate entire call centers with it!
Try a sample — These samples are a very good way to get acquainted with the different features you can use.
We will come back later to try these different settings, for now, let’s keep moving!
Once you created your bot, let’s create our intent. Intents are the essentially the things you bot can understand, the more intents, the more things your bot can talk about. Since this is a bot that understands politics, let’s create our “trump” intent:
Utterances are essentially the different ways you can ask the same thing. Amazon Lex will take care of small variants and typos for you. What that means is that you don’t have to worry about your bots phrasing like “what u think of trump?“, Lex will make sure the bot understands that too.
So after you put the above utterances in, let’s make sure the bot can reply to them by scrolling down and filling the responses, like so:
Boom! Save the intent and your bot is now capable of talking about politics. Well, about Trump anyways. You will notice there’s an area on the right within Amazon Lex that allows you to test your bot. To test what we have just done, you will need to “Build” your bot first, so make sure you hit that button before testing it out! The test should look something like this:
We’re going to start coding the bot now so that it works outside Amazon’s dash! But first, there are a couple things we still need to do. The first one is publishing your bot.
You can play around with your bot as much as you want, but your customers will only see these changes after you publish it. The publishing button is just beside the “build” button. You will be asked for an “alias”, an “alias” could be things like “production_environment” or “test_environment”. Let’s just name it “politicsbot” for now.
The last thing we need to do is to generate API keys for our application to access the bot.
Chop Chop! We should have only a few minutes left now. On Amazon, head to services again and look for “IAM”. Click on “Users” and add a new one. Name it “bot_user” and select the access type to “programmatic”. Like so:
Great! We’re almost there. When asked about permissions, select “attach existing policies directly” and select “AmazonLexFullAccess”.
Hit create user! Save the “Access Key ID” and “Secret Access Key” given to you as we will use it soon.
You must be thinking “There is just no way we will make it in time!”. Well, remember I said all it takes to get this working is 11 lines of code?
To run this, create a python file named “bot.py” and run it by running:
The only requirement is to have “boto3” installed, boto3 is Amazon’s Python library, install it by running
So, did you make it in time?
My goal when writing this book was to be practical and direct to the point, that’s why I started the book by cutting to the chase and already giving you a taste of what it’s like to build a chatbot.
I want to step back a little now and go back to the basics, and give you some tips on how to build conversational interfaces that are more useful and with more chances of success.
When building chatbots and conversational interfaces in general, it’s important to have a clear goal in mind. Goals are very important because it is the guiding factor for your brand (which in this case is your bot personality) and your users. There are many reasons why you would want to build a bot, from providing online support, improving online ordering experience or even simply because you want a fancy FAQ. Once you have defined your goal, you can work towards customizing the communication pattern of the bot to help you meet this goal.
One typical example:
Let’s say your bot goal is to help with online sales. Your user is asking the bot about a product that you don’t have in stock. What you might do, to align your goals with the bot communication, is to offer a similar product.
Also, don’t be afraid to use pictures!
Lex is very simple so I’m assuming that at this point you have already learnt how to do the above, if you haven’t, don’t worry we will go through this later.
Engagement is key, so every time there is a bit of information missing, instead of just saying “I don’t understand”, try to get the missing information!
Most of the above functionality won’t work with the code we wrote, which means It’s time to get past the prototyping phase and test the bot in a real world environment, like Facebook or Slack.
The next chapter goes through how you can set this up, you don’t have to set up both to continue, but you are going to need at least one of them to be able to complete the next tasks.
Making your bot available where your public is will be key to its success. Identifying the right channels will save you development time and improve your chances of succeeding.
Luckily Amazon Lex makes it very easy to publish in different channels and does a good job at making easy to maintain.
There are a few things you will need to accomplish in order to make your chatbot available on Facebook. So let’s get started!
The first thing is to create a Facebook app, you will need to have a Facebook for Developers account. Head to https://developers.facebook.com/ and get yourself one. Once logged in, hover over “My Apps” and click on “Add new app”.
Give it a name and click “Create APP ID”
You will be presented with a dashboard for your app, scroll down until you find “add a product”. This is where you can allow your app to leverage different Facebook products, in this case, let’s set up Messenger. To do so click on the “set up” button on the “Messenger”.
Repeat the same process for the “Webhooks” product.
“Messenger” should appear on the menu on the left hand side under “Products”, click on it then on “Settings”. Find where it says “Token Generation” and select the page you want your chat bot on. If you don’t have a page, you can create one easily by going to:
After selecting the page, you will given a token:
Save this as we will need it very soon.
Again on the menu to the right, head to “Settings”, just under “Dashboard” and click on “Basic”. Get your “App Secret” from there and save it too.
Now, in Lex, head to the “Channels” tab and select Facebook.
Give this new channel we are creating a name, a description and put in both the page token and the app secret. Make up a “verify token” and remember it for later.
When you click “Activate” you will be given a callback url, save that too.
The last step within Facebook is to configure the webhooks. Head to “Webhooks” under the “Products” menu.
Select “Page” on the dropdown and subscribe this topic. It will ask for the callback url and for the token you saved just before.
You are all set! You should be able to communicate with your bot through your Facebook page!
Because the app hasn’t gone through Facebook’s app approval process, you are the only who capable of using it. To make it available to others you will need to go through the approval process, which should be fairly straight forward.
Head to the app build section of slack, the address should look something like this https://api.slack.com/. Click the “start building” button, which should take you here:
Select “bot users” on the left hand side menu and click on “add bot user”, you will need to fill in a few things, like so:
Marking “always show my bot as online” will make it for a better experience for your users, as the bot will appear online even when its not being spoken to. If you leave it off, the chatbot will go online when spoken to, and go off line a few seconds later.
After adding the bot, head to “Enable Interactive Components”, again you will have to fill in a few details, for now, since we don’t have a Request URL yet, let’s just put something valid in there and come back to fix it later.
In the left menu, in Settings, choose Basic Information. Record the following application credentials: Client ID, Client Secret and Verification Token, we will need that to configure the integration on Lex.
On the “Channels” are of Lex, click on “Slack” and configure a new channel as illustrated below.
If successful you will be given a postback url and a OAuth url, keep those as we will need them to finish configuring Slack.
Back to Slack, click on the left hand side menu where it reads “OAuth and Permissions” and a a new “redirect url”, this will be the OAuth url you just copied from Lex.
Be sure to save!
While you are there, In the Scopes section, choose two permissions in the Select Permission Scopes drop-down. Filter the list with “chat:write:bot”, add it. Repeat the process for “team:read”.
Choose Save Changes.
The next step is to go back to “Interactive Components” where we placed a fake url before and replace it for the postback url given to you by Lex.
Now we need to subscribe to events. Click on “event subscriptions” on the left hand side menu, and activate it.
The configuration is more or less like the above image, add your callback url provided by Lex to the “Request URL” field, and subscribe to the “message.im” event with the bot events section.
The final step is to add it to a Slack Team and test it. To do so now, click on “Manage Distribution” on the left hand side menu and click on the “Add to Slack” button.
After going through the authorisation step, your team should contain a new user named after your bot, talk to it!
Because the app hasn’t gone through Slack’s app approval process, you will only be able to add to teams you manage. To make it available to others you will need to go through the approval process, which should be fairly straight forward.
Amazon Lex automatically monitors your chatbot, great insights can be found under the “monitoring” tab.
In the utterance table you can see the “detected” utterances and their count, that can give you visibility over what your users are using the bot for.
By understanding what your chatbot is most used for, you can prioritise its improvements, creating more intents for the areas that actually matter to your user.
If filtering by “missed” you can also identify the things your chatbot could not comprehend, and what’s more, it allows you to add the specific utterance to an existing intent. That’s useful when your user phrased a question or request it in an unexpected way.
It’s important to revisit this section from time to time, to understand your bot’s weak points and your user behaviour.
I guess the point I am trying to make is that your chatbot will never be perfect, the more functionality you add, the more people will expect of it, so it’s important to be on the lookout for quick wins and new features that can be added.
A study came up with the most frequently asked questions to bots and here’s the not so surprising list:
- Are you a robot?
- What is your name?
- How are you?
- How does it work?
- Do you like [celebrity]?
Here’s some homework! Implement intents from 1 to 3 using the knowledge you already have and come back here for 4 and 5.
The “how does it work?” intent is perfect for learning to use Response Cards, or call to action buttons, to make your bot more engaging. And the “do you like [celebrity]” is great for explaining the use of slots, we we will do these together next!
Let’s learn about response cards by implementing an intent called “HowDoesItWork”, nothing out of the ordinary to start with.
When creating response cards, it’s important to know that you can have both images and callback buttons. These buttons can trigger other intents, they have the same effect as if the user had typed something.
In our case, we have a few functionalities already built, like the ability of asking for the bot name and asking if it’s bot or not. So maybe the response message could be “I’m learning to help you” and some of the callback buttons could be:
- What is your name?
- Are you a robot?
You would set this up the same way as everything else the only difference is that when setting up the response, we’re going to enable “response card” and set up some buttons, ignoring the image, title and subtitle for now, like so:
This is how that looks like:
And after selecting one of the options:
I will let you play by yourself with adding images and the rest of the functionalities, it’s simple and straightforward. One thing to keep in mind is that they may behave differently depending on whether you are visualising it on Facebook, Slack or some other provider, so it’s important to test in all channels.
Slots are placeholders for parts of a phrase that changes. E.g:
- Do you like George Clooney?
- Do you like Leo Dicaprio?
- Do you like Jim Carrey?
You need, however, to specify what these placeholders might look like, that’s the only way Lex can validate the input, otherwise things like “Do you like flowers?” could be taken as a valid input when in reality, they are not, the bot is only prepared to express its feelings about celebrities.
Lex has a large collection of built-in slots, but you can also train it to understand your own.
For our use case, we can use the “Amazon.Artist”. Like so:
Note that I selected the slot as required, as otherwise, how could the bot know if it likes the celebrity or not? 🙂
Note that I also filled the “Prompt” section with “Like who?”.
This is what the conversation looks like under these settings:
Lex currently support two types for error handling: Clarification prompts and Hang-up phrase.
Error handlers get triggered when no intent is matched with the phrase sent by the user.
Clarification prompts are “soft” errors, in that you could say something like “Sorry, I don’t understand”, whereas a hang-up phrase is triggered when there have been a few clarification prompts triggered before hand. This is a good time to transfer to a human if critical or to educate your user on to how to best use your bot.
These can be managed within the “error handling” of Lex. There you can configure the messages and the number of clarification prompts before a hang up before.
Have you heard of Amazon Alexa?
Alexa is Amazon’s voice service and the brain behind tens of millions of devices like the Echo family of devices including Echo Show and Echo Spot. Alexa provides capabilities, or skills, that enable customers to create a more personalized experience.
The way I see Alexa Skills is that they are chatbots themselves, the only different to the one we’re building is that they use voice for input and output instead of text.
Alexa skills are created by people like you and me, what’s more, they are built off the same technology that powers Lex.
Everything we have done so far, can be transferred into an Alexa Skill with very little effort.
In fact, you can simply click on your bot in the bot list within Lex, click on the “Actions” button and select export.
Then you can simply import it into an Alexa Skill, you will need to tweak it here and there, adjust a thing or two and go through an approval process to have your skill published, but for the most part, all of the hard work is done.
Be it because you want to connect to your database, a third party or to another service you built, integrations are a key part of developing smart chatbots.
The best way to perform integrations on Lex is through Lambdas.
Lambdas can be defined as a compute service that runs your code in response to events and automatically manages the underlying compute resources for you. Serverless to its primest, in simple terms, lambdas are a way to execute code without worrying about servers.
Lambdas are also an Amazon service and its pricing is as follows: $0.20 per 1 million requests. $0.00001667 for every GB-second of compute, with every execution rounded up to the nearest 100ms. Similarly to Lex, lambdas also have a free tier, 400,000 GB-SECONDS of compute time per month.
Trust me, you won’t pay a single thing for these tests we will be running.
We will use a free Star Wars API (https://swapi.co/) in our first integration. We will create an intent that asks the bot about its favourite Star Wars character. Instead of configuring static responses, we will connect to the “people” endpoint of the Star Wars API and select a character at random.
To start with, let’s create the intent as per usual, not a lot to it, we can keep it as usual for now, except without a response. Like so:
Before we jump into coding the integration, we will need to configure a few things in Amazon. Lambdas by default do not have permission to interact with other Amazon services.
Roles are configured within the “IAM” service, search for it within the services Menu.
Select roles on the left hand side menu then click on “create”
You will be prompted with a 3 steps screen, select “Lambda” as your service and advance to set permissions.
Search for “AmazonLexFullAccess” and make sure that’s selected.
Then name your role after “lambda-lex”.
We’re ready to create the Lambda function now. Search for Lambda within the services tab. You will be presented with something similar to this:
Find the “Create Function” button and click on it.
Select “Author from scratch” and configure it like so:
Within the function code, use the code below:
It’s a rather big code, but in reality it doesn’t do much more than intercept the intent, reach out to the Star Wars API and return a response in the format that is expected by Lex. Have a read through it, it should not be hard to understand.
Hit save and head to Lex, we will configure the last bit before we can see this in action.
Within the Star Wars intent, find the “Fulfillment” section and instead of “return parameters to client”, select the lambda function we just created.
Hit “save”, and then “Build”.
The result is something like this:
I hope you enjoyed reading this book and building chatbots as much as I enjoyed writing this book.
I truly believe that there is no limits to what you just learnt, you have the power in your hands to disrupt the customer service industry and change how entire call centres operate.
If your plan is to start a career as a Conversational Engineer, you just learnt the basics to do so. I do have a number of courses on Udemy in case this catches your interest: