I need to build a bot which can keep a small talk conversation in Slack and be smart enough to understand topics about programming. In the end the bot should able to give links to documents from the internal storage, or web resources using non-predefined answers. I’ve got no experience in bot development, but whatever platform is used it should be easy to be extended with more features and replaced with another platform if needed. So that any solutions for dummies obviously should be thrown away. It’s because they provides no-coding experience along with strong coupling to the platform.
Bot Platforms & Theory
Keeping in mind the first aspect about platform flexibility, the second aspect was an ability to start development quickly and achieve desired solution. Search over web gave me several guidelines and tutorials from which I found the most reliable for me now is Dialogflow. This is the former API.ai and now it’s acquired by Google. Also there are several others well-known platforms which provides same functionalities. Maybe later I’ll take a look close on each of them.
- Dialogflow provided by Google
- Watson Conversation provided by IBM
- Microsoft Bot platform with LUIS
- Amazon Lex
Bot’s architecture looks pretty simple and all of the platforms above use the same approach.
Channel is the way of communication with users. It may be Slack, FB Messenger, Skype or another application. The Application on the side of the channel needs to handle events to track incoming messages. It may require special permissions. The Connector is the service which you don’t need to write if you’re using a bot platform. When you finish with integration configuration the App will interact with the Connector in order to answer to the user. The Connector in its turn will use described linguistic logic and, if needed, will send requests to external services via web hooks. Your business service is an external one and you can send back results which the bot will send to user. Linguistic logic relies on rules and Machine Learning to recognize users speech. This recognition subsystem helps to extract important parameters to conduct request to the external web services.
Intents, Entities & Dialogs
The Linguistic logic consists of such definitions as intents, entities and dialogs. They altogether help to design a conversation flow.
Intents are used to catch some user topic. In the beginning of a conversation user writes a sentence which can be associated by the bot with some predefined intent. Bot uses ML and speech recognition to do it properly. So the user can use natural language while writing or speaking.
For example, user might want to buy a ball. He could start the conversation with the following phrases “I want to buy a ball” or “I want a ball”. The intent which is supposed to catch this topic should have user’s phrase “I want a ball”. During ML training the bot will catch this and other similar phrases and will try to react somehow.
Entities are used to extract important data from user input. There are 3 types of entities in Dialogflow: system, developer and user.
For example, user can write “I want to buy a big ball”. The word “big” determines a size of the ball. The size may be also small or middle. This entity will be a parameter for your web service later, if you want to use it. If you define this entity the bot will understand also other phrase like “I want a small ball” and associate ball’s size with correct parameter.
Dialogs may be linear and non-linear. Linear dialog is intended to collect data for some action with predefined number of parameters, and it goes in one direction. For example, it may be a conversation about booking a hotel, where user would need to specify number of people, dates and other additional services. In the non-liner dialog the conversation topic can move away from the original, go back and forth. As an example, it may be a dialog to find out a restaurant which matches the best to user specifications which bot gathers during the conversation.
Simple Conversation in Dialogflow
Here I put an example of building a very simple linear dialog. No web hooks, only question and answers. Let’s take a spaceship as a subject for the bot. User wants to choose a spaceship for his adventure. We need to define parameters of the ship, what user can say and what the bot bit should ask to understand the user.
Quick description of the spaceship will give clues about which entities and parameters should be created. Any spaceship can be defined with the following properties: class, shield, armor, number of turrets and speed. Lets create 4 entities to describe all of them except the number of turrets. This number will be represented by system entity. The example of how @class entity might look like is on the picture.
You will need only one intent for this kind of conversation. Basically, intent should be triggered by some user sentence and the bot will try to fill out parameters which you specify. Because it uses natural language processing algorithms you don;t need to specify each sentence you’re going to catch. Your examples should cover common patterns of what user might say.
For example, user may start the conversation by saying the following phrases (or similar):
- i want fast cruiser with steel armor
- i need a battleship with fast speed
- i need a spaceship
These sentences will help the bot to understand other sentences which are not described. For example “give me a spaceship” also will initiate the conversation. Two of the above sentences contain definitions of parameters (“fast”, “steel”, “battleship”). It means those parameters will be automatically extracted from the sentences. Since we need all parameters to be defined, they should be specified in the Action section as required parameters.
Prompts column contains questions which bot will ask if corresponding parameter is not yet extracted from the conversation. The bot keep asking user until it gets all parameters initialized. After that he can perform the request to external web service passing all values.
If you need to allow user to cancel the conversation in the middle you need to add cancellation follow-up intent. In such a case the user will be able to say “stop it” to stop the conversation.
This is the simple conversation. Later I’ll try to add information about web hooks and non-linear conversations.