The tech world is currently in the middle of chatbot-craze. You know, these cranky bots you find yourself talking with before you even notice? Ever wondered about the others, those who build the chatbots?
Today, you’ll jump to the other side as we detail the required steps to build your own bot. What should my bot be able to do? How will the conversation flow look like? What kind of personality should I give to my bot? What are the best channels for my bot? To learn more on such matter, check this comprehensive article addressing the enterprise methodology to build chatbots.
For today’s matters, we’ll consider that you already answered this tough and deep questions and you’re ready to B U I L D. It’s finally time to code!
To that end, we’ll use Recast.AI. If it’s your first time on the platform, I recommend you to check this starter guide to get you up to speed.
In each part of this post, we’ll improve our bot by achieving new goals to make it smarter. Here is what’s on the menu:
- Objective 1/3 — Your bot can speak on your channels (Messenger, Twitter, etc.)
- Objective 2/3 — Your bot is able to fetch external resources (API) to enrich its answers
- Objective 3/3 — Your bot is able to speak many languages
The sample code examples are made in NodeJS (min. version 7). You can find them on the dedicated public GitHub repo.
You’ll need an account on Recast.AI to follow this tutorial step by step:
What does the bot code look like?
Your bot will be linked to one or many channels: Facebook Messenger, Kik, SMS, etc…
Basically, each time a user speaks to your bot, the channel platform will trigger a call to a webhook to transmit the user message. Therefore, the webhook is the main entry point of your bot.
So, a chatbot is an API!
Here is an example code for a basic bot:
On this webhook, channels can send many pieces of information, such as:
- user input (the message),
- unique user identifier,
- “Nature” of the message: was it typed by the user, or sent by clicking a button (quick reply)?
To know more about information sent, you can check each channel documentations. For example, in Facebook Messenger:
How to connect your bot to different channels?
You can connect your bot to many channels but be careful: each channel sends its own information format. In that code example, user input was sent through the message variable in the request.body object. This is not a convention but an example. To abstract usage of many channels, we developed Bot Connector. Bot Connector transforms channels inputs to a single JSON format sent to your bot. The format is always the same even if messages come from different channels.
Different channels are available on Bot Connector: Facebook Messenger, Slack, Skype, Line, Twilio, Kik, Twitch and many others. You can check our dedicated Bot Connector documentation to learn how to use it.
Objective 1/3 completed — Your bot can speak on your channels (Messenger, Twitter, etc.)
How to get external data from your bot?
When users speak to your bot, they mostly want an answer to their questions:
- How can I go to Paris ?
- Is the nearest Five Guys opened ?
- What is the weather in London?
As you can do for other projects you can trigger a call to external APIs to get answers directly in the bot code:
- Call Google Maps API to get an itinerary
- Call Google Places API to get open times of a restaurant
- Call OpenWeatherMap API to get weather in a specific location
Then, it is the developer task to use responses of these calls to answer user questions.
This is an example in which user wants to know if a McDonald’s restaurant is opened:
In this code sample, starting from previous sample, we use Axios to make external calls to APIs. Don’t forget to import it at the beginning of the file (line 12):
You must add your Google API key at the end of the URL of your request, with the “key” parameter: https://maps.googleapis.com/XXXXX/key=YourTokenHere
There is a free plan to test Google Places API. For more information about plan and usage of Place API, you can check the Google API doc.
Objective 2/3 completed — Your bot is able to fetch external resources (API) to enrich its answers
Open your bot to the world
If bots are cool, multilingual chatbots are even better! It’s a vast topic which is covered in yet another guide. This one hour tutorial details all the necessary steps to add a new language, both on the platform and in your code. Check it for an exhaustive overview of language management on Recast.AI. I will only focus on the essentials:
Language handling is sensibly different on the two sides of your bot:
- Understanding your users queries: Recast.AI detect languages and process the input (classification at least). The user can talk to the bot in his own language.
- Replying to these queries: This part can be set directly in your bot code, so you can format the bot answers and be more precise depending on the user language.
Adding a new language is more than a simple translation
Language switching is not just about translating all the elements of a sentence, a lot of other factors can vary. For instance, the words order: in English we say “a black horse” while in French it’s “un[a] cheval[horse] noir[black]”.
Moreover, if you add a new language to your bot you must consider the impact it may have on all the data types it handles. A datetime is not formatted the same way in French and English. For example, to answer to the question “Is the McDonald’s in Champs-Elysées in Paris opened?” in English, your bot will say “Yes, it’s open until 4:00PM, you can go to eat a burger!”. But in French, your bot will say “Oui, ce restaurant est ouvert jusqu’à 16h00, tu peux aller manger un hamburger !”
Here is an overview of the languages we currently support:
Languages supported on Recast.AI
You must take care of the different specifications of each language you support to make your bot smarter and understandable by humans. We support all languages, and you can find the entire list on our documentation.
Objective 3/3 completed — Your bot is able to speak many languages
Now, your bot can understand simple sentences and retrieve external information in diverse languages. Its users are able to reach it on their usual channels. Now is the time to step back, and take a few seconds to appreciate the achievement.
Ready for your next bot upgrade? Your next goal will be to use our new Bot Builder to create the conversation flow of your bot. You can check the Bot Builder section of our documentation to start playing with it. Have fun making smart bots!
Have an issue with your bot? Is something unclear in this tutorial? Feel free to comment or join our Slack to discuss it.