Here is the highlight of the presentation about chat bots given within TernopilJS meetup on September 26, 2016.
What Is a Chat Bot?
A bot is a programme that performs some action(s) automatically within the same interface as a user. Today chat bots are not very smart. It’s hard to ask a bot (e.g. Google Assistant or Siri) a right question to get the exact answer you need. However, the technologies are developing quickly. In particular, the artificial intelligence (AI) systems that work together with bots demonstrate quite impressive results.
Chat bots can help you to order food, play games and even disclose criminal schemes.Here are few examples of the bots you can find on Telegram:
Gif bot (@GifGifBot) - a simple programme that can send fun animated pictures to your friends.
Privat bot (@PrivatBankBot) allows to transfer money from one credit card to another within Telegram.
OpenDataBot - can find the information about any organisation in Ukraine and check whether that organisation really exists. You also can find out how many organisations are registered at the same address. OpenDataBot is used by Ukrainian government and is able to disclose criminal schemes. They analyze the data received from users and from the state register. So it’s possible to indicate the organisations that work illegally or those organisations that exist for one day only.
Recently they have launched a bot-powered gaming platform on Telegram. All the games there are written with HTML5. This means they can be easily loaded as ordinary webpages. All you have to do is address @gamee and choose the game you like. Now you can enjoy playing and compete with the other chat participants for the highest score.
Some of the popular Facebook messenger bots include:
1-800-Flowers.com - the first bot used for sales. With its help you can order the flowers and give them to someone.
TechCrunch - a bot that sends news.
WTFIT - you can send this bot a picture of any object, and it will try to interpret to you what that object is.
How do Bots Interact with Messenger Services?
According to the Server-push technology a client is connected to the server. If any action takes place on the server, it will send the result to the client. A well known example of Server-push technology is web sockets used in HTML5.
Web hooks technology consists in creating your own API (endpoint) for messenger services which they will query if you need to deliver a message.
To give the better insight into the bot’s world, let's check vuiko_bot on Telegram. This bot helps to solve the communication problem. Even within the same country people from one region may find it difficult to understand some particular words that are frequently used in another region. For example, in western Ukraine there are numerous words with the Polish origin, and they are uncommon for central and eastern parts of the country. You can type vuiko_bot any word and it will try to give you its definition. You can also type a command /random that will show a random word from the database.
The vuiko_bot application is built within Meteor framework. We use the website www.proridne.org that offers a vocabulary of dialect and rarely used Ukrainian words. He connects the app to its data with Cheerio npm package. This allows to parse the website data and work on the server in the same way as with jQuery. He also connects the app to Telegram service with Telegram-bot-API npm package.
How to Create a Bot on Telegram
First, you need to address BotFather. It will help you create a new bot and edit it’s settings. Enter a command /newbot. BotFather will offer you to choose the bot’s name which later will be visible to the Telegram users. If everything is correct, BotFather will issue you a key (token) to access the Telegram API.
It’s very easy to create such project within Meteor:
- Enter command meteor create vuiko_bot
Go to the project cd vuiko_bot to add necessary packages:
You will get the project with the following structure:
The folders “client” and “server” will contain all the frontend and backend codes respectfully. On the server side create two files called main.js and vuiko_app.js - a bot that will receive, process and send the messages.
Create the basic class called server/vuiko_app.js (here we use ECMA6 standard):
Connect the necessary libraries and constants:
Write a method that will be filling the database:
Add a connection to the Telegram API. Here you also have to enter the token issued by BotFather:
Create method for processing of the received messages. This simple example includes just two commands: sendRandomWord retrieves a random word from the database, and findWord looks for the definition of the word:
Add the method for processing of the received messages to the Meteor environment:
Create method for sending the messages:
Searching the word in the database:
Create the method for searching a random word in the database:
In the file server/main.js write the code for launching the bot:
And that’s about it!
You can access the whole vuiko-bot code on the GitHub. Feel free to add your own features to this bot. Any contributions and ideas are welcome.
If you have any questions about bots, and implementing them as a feature for your mobile marketplace platform app, we'll gladly help you with the development, consulting and support part.
Check the upcoming events at TernopilJS Facebook page and join us next meetup!TechHub