By the end of this tutorial, you will be able to build a fully functional movie bot! It will able to make movie recommendations based on several criteria. We’re using Recast.AI platform to build the bot and The Movie Database for information on movies.
Here’s a demo chat with Movie Bot:
Need to see it to believe it? That’s wise.
WHAT ARE WE BUILDING TODAY?
Interacting with third party APIs (such as The Movie Database) allows for much more interesting use cases that simple QnA chatbots. With Bot Skills, we added the option to call webhooks directly from the builder, which makes it even easier.
Today’s bot requires several steps:
- Extracting key pieces of information in a sentence
- Building the bot flow (triggers, requirements, actions)
- Creating and connecting a bot API able to fetch data from The Movie Database
You’ll need a Recast.AI account, Node.JS and potentially Ngrok for testing.
Before we jump in, please check this guide instead if you are looking for a guide detailing the creation of your first bot.
This tutorial covers Node.JS only. If you would rather code with Python, check this tutorial covering a similar use case.
Let’s get to it!
I/ EXTRACTING KEY INFO FROM A SENTENCE
Intents are helpful to determine the overall meaning of a sentence. For our use case, knowing that the user wants to watch something is not enough.
We need to know what the users want to watch.
Entities are designed to solve this problem: they extract key information in a sentence.
Intents make you understand that you have to do something.Entities help you actually do something.
Let’s imagine you are a telco company providing phone and internet access, and your bot has an intent that understands when people are complaining about an outage:
The entities extracted will help understand what is going wrong, where and since when.
For our movie bot, we will try to extract 3 key pieces of information:
- What the user wants to watch (movie vs TV show)
- What genre they are looking for
- In which language
USING GOLD ENTITIES
To help you speed up your development, Recast.AI extracts several entities by default : Dates, locations, phone numbers… An exhaustive list is available here.
Language entity will be helpful:
See the little star next to the entity name? It differentiates a gold entity from a custom one.
We will use it to fulfill our third requirement: the movie language.
CREATING CUSTOM ENTITIES
We will create custom entities to extract the information we need. As with intents, training is very important: the more examples you add to your bot, the more accurate it gets.
Training your entities can happen through multiple intents. Entities are independent of intents.
For our movie bot, we only need one intent,
discover-movies, and 3 entities:
movieto identify that the user wants to watch a movie
tvfor the same purpose but with TV shows
Open the intent
discover-movies and add expressions. Make sure to cover every possibility, this means a healthy mix of expressions with:
- No entities at all: “My boyfriend wants to watch something tonight”
- One entity: “I want to watch a movie”
- Many entities: “Can you recommend me some French drama TV shows?”
To tag your expressions, select the text you want to tag and type your entity name:
You should add many more examples: 15 would be nice, but a production-ready bot would require at least 40 examples to perform well.
You can see here that “French” was detected as a nationality, not a language, because that’s what it is in this context. When building the bot flow, we’ll make sure to check for these two entities.
II/ BUILDING YOUR BOT FLOW
Since we just need to make sure all our criteria are filled before calling a Node.JS API, the build part will be rather simple.
We will just need one skill, let’s call it
You can find an example of a configured skill here.
We want to trigger this skill if the intent @discover is present:
This tab helps you collect data before moving to Actions.We want to make sure the user specifies a medium, a genre, and a language before moving on:
The requirements will be checked one by one. They can all be fulfilled on the first message, for example if the user says I want to watch a crime movie in English, then the Actions will be triggered immediately.
For each Requirement, you can choose to send a message if it is complete or if it is missing.
Sending messages when a requirement is complete can make your bot more lively: A crime movie? I love them too!, but are almost mandatory when the requirement is missing: You need to ask your users to fill what you need to know.
For example, I send quick replies with suggested genres if #genre is missing:
Once you have setup questions for the 3 groups of entities, go to the
Once the requirements are fulfilled, we want to call our API to actually perform the search.
CALL WEHBOOK action. You can either type a full URL (eg:
https://mydomainname.com/discover-movies), or a relative url (
/discover-movies). Recast will use the parameter
Bot base URL in you bot settings when you type a relative URL.
Next, add an action
RESET to empty the memory once the call has been made.
If you don’t have a public server, or if you want to test your bot during development, ngrok is a very handy tool: It creates a public URL for you and forwards requests to your computer.
Once you installed it, run
And copy the
Forwarding URL (https://XXX.ngrok.io). All requests made to these URL will be forwarded to the port 5000 of your computer.
All your bot needs now is its API to get your movies!
III/ CREATING THE MOVIE BOT API
The NodeJS part of this bot is fairly simple: It will behave as an HTTP proxy between Recast.AI and The Movie Database.
When your application receives a request from Recast, it sends a search query to the Movie Database with the criteria of your user and formats the JSON answer to the Recast’s message format.
Start by scaffolding your project:
You will need a token to use the Movie Database API, go here to generate one, and fill your
Let’s create an Express application to handle the requests from Recast:
We asked Recast to send a POST request to
/discover-movies when a user has filled his search criterias.
The main goal of our controller is to pick and format the preferences from the memory to send them to the Movie Database’s API:
There are two functions here that we have not declared yet:
getGenreId because the Movie Database can’t search for a specific genre based on its English name, but rather on a custom number.
Here is how to translate a genre name to its id:
Now that we have extracted and formatted all the filters of the request, we need to send the request to the Movie Database and format the answer:
Start your application by running:
All being well, you should see:
App started on port 5000
Movie recommendation, weather, health, traffic… With third-party APIs, everything is possible! Now that you’re familiar with the workflow, we can’t wait to hear from you about what you’re building!
🤖 Happy bot building 🤖
Have an issue with your bot? Is something unclear in this tutorial? Feel free to comment or join our Slack to discuss it.