Bots in telegram: the experience, insights, tips on development

Let's get right on the case. The content of the article:
the
    the
  • then we began to write a Telegram bots. First experience.
  • the
  • create a nodejs library bot-brother to quickly write bots.
  • the
  • Parse the code svezhenanesennogo bot DeloreanBot
  • the
  • Universal guidelines for creating popular bot
  • the
  • Philosophical arguments about the monetization of bots


Went.

why we started to make bots. Matchmaker, The Weatherman, Zodiac.


Attention! The following paragraph may contain spoilers. Be careful.

All three of the bot was suitable and hang out now in the top storebot.me. Weatherman for several months steadily holds the first place ranking.

Now a brief history.

Developers do not feed bread – give only nakodit something funny stupid stuff. So it was incredibly difficult to ignore the news about the launch of Telegram bots.
The first thing you need to come up with the idea for the first bot. Reserch started with pillars of the Internet: porn and cats.
It turned out that in Arab countries porn sites are blocked, and porn in the telegram is available. This fact is multiplied by the attractiveness of the porn-bots for developers at infinity and the niche was quickly filled.
And seals Serezha (Sergei is generally speaking a developer, I current article writing) allergies.

Somewhere at the intersection of porn and cats, the idea to make a Dating-bot. Without thinking, we created Matchmaker_bot – analogue Tinder Telegram (telegram.me/matchmaker_bot).
The idea of the bot is simple and so beautiful – you upload a photo and then see pictures of other people. If you liked a certain user, and he liked you, the bot will allow you to exchange contacts and you will be able to live happily ever after until death do you part chat in telegram.

I can not share. When you first start the bot in the code there was a bug that counted dislike for like. Because of this bug I talikala a dozen brutal Iranians, who then happily began to chat with me.
Every time I saw a notice in the telegram that I received the photo, I blinked and ran to Sergei, that he has deleted the picture – everyone was waiting for indecency. Polite but the Iranians never sent nothing obscene – just images of flowers and lions. Such are romantic companions.

For the first two days of life the bot has gathered 800 unique users, has received 200 people daily audience and still the audience is growing.
At the time of writing we have 900 unique visitors a day.

We are not experts in SEO-optimization of websites and, honestly, have no idea how to make a web site that would be in two days, attracted the same audience without any special effort.

So the result is encouraging and Serge decided to quickly fill a couple of bots. As the world saw the Weatherman and Zodiac boats, which are able to give the weather forecast or the horoscope, and also send you daily forecasts for today or tomorrow at the specified time (telegram.me/weatherman_bot and telegram.me/zodiac_bot).

Serge confuse and did localization for robots in 5 languages, and for weather bot already took a paid weather API.
Multilingual, forecast accuracy and intuitive interface suddenly made the weather bot is very popular.
Now he is in the top storebot.me, occasionally getting on the location of the coolest bot in the Telegram, has over 2000 user ratings, average rating of 5 stars. Comments for full of love and adoration.


creating a nodejs library bot-brother for the rapid development of bots


The first bot was written haphazardly on the enthusiasm. But the real developer understands – if you have to do the same product many times, it starts to smell framework. Ready-made libraries or frameworks we find, therefore, Serge wrote his libo.
Telegram Bot API allows you to use http requests to send or receive text commands for a dialogue with the user.
You can add special markup variables, so that, for example, show the user a simple keyboard.


In fact, the developer thinks in terms of more advanced abstractions.

1. You share a text message from the user for command input and response to the bot request.

For example, the dialogue:
User: Bot, show me the weather!
Bot: OK, for what city?
User: Irkutsk
Bot: it's Cold in Irkutsk...

The first sentence of the dialogue is to initialize a dialogue with a user command input. And the third phrase – this is the answer to a specific question bot.

2. All work with the bot is walking on some the state machine.

I want to have the means to navigate between States (teams) to be able to do the child and the parent state, to return to the as new etc. etc. simply put – I want to have a handy tool for routing between the teams.

3. You want to have a tool for easy work with different languages.

I want to have a separate logic, separate configs for different languages. Ideally the configs of the languages should be simple in order to give the localization files to be outsourced for translation.

4. You want to arrange a session.

That is to have a set of data that is tied to a unique user of the bot.

5. I want to split the command processing into multiple stages middlewares.

Everyone loves the principles express.js :)

All these ideas are embodied in the library bot-brother. We have tried to write her detailed documentation, which is available in the repository.

the Bot for the day. The use of a bot-brother on example bot for notifications DeloreanBot.


If someone wants to skip the lyrics, touch working bot in the Telegram, and his source view on github.

For the rest, we describe the features of the implementation.
The functionality of produced code, the user enters into the bot reminders and time, the bot at the right time sends a notification.
For example, the user writes "take a Walk with the dog in an hour", the bot after an hour sends reminders. (Let's just agree that we do not claim originality of the idea). Do bot in Russian and English languages.

Writing such a bot, debugging, and interface localization for English we have is one day.

We need:
the
    the
  • to be Able to fulfill user commands;
  • the
  • to Organize localisation
  • the
  • to be Able a convenient way to determine the user's time zone;
  • the
  • to Store user data;
  • the
  • to be able to track the time when you need to send a message;
  • the
  • to be Able to parse a phrase that contains the date and time.

1. Processing of user commands

This is in General the basic functionality bot-brother. It makes no sense to duplicate here the documentation of the library. Here is a little code snippet to a common understanding. Registration of the team and the reaction to her answer usually looks like this:

the
// Register the command settings_locale
bot.command('settings_locale')
.invoke(function (ctx) {
// Our response to the challenge of team –   ask   user to choose language
return ctx.sendMessage('Select language');
})
.answer(function (ctx) {
// Handler for a user response to our question.
ctx.session.locale = ctx.answer;
ctx.setLocale(ctx.answer);
return ctx.sendMessage('answer.success').then(function () {
// When people choose a language, go right back to the last command.
return ctx.goBack();
});
})
// Won't force the user to type any text, suggest that the response options with the keyboard.
.keyboard([[
{'Russian': 'ru'},
{'English': 'en'},
],
// Add the back button to changed.
[{‘Back‘: function (ctx) { ctx.goBack(); }]



2. Organization of localization

Once we ask the person to specify the interface language, it would be nice if you work with the bot to show him the texts in the correct language.

All phrases will be stored in “key-the localized value” in yaml files. This format is useful if you want to give files to edit third-party translators. An example of a file with the English translation can be viewed here.

Code convert these files to json format and pass to the bot.

Code to transform yaml file into json research is of no interest, but if that is here.
the
// code Snippet for creating a bot
var bot = bb({
key: config.bot.key
redis: config.redis
})
// Set the bot sets of phrases in different languages.
.texts(texts.ru, {locale: 'EN'})
.texts(texts.en, {locale: 'en'})
.texts(texts.default);


Then in the code everywhere instead of text phrases will indicate the respective localization keys.

All pieces of code with the text of the message will change slightly.
This code snippet
the
.invoke(function (ctx) {
// Our response to the challenge of team – ask user to choose language
return ctx.sendMessage('Select language');
})

transformirovalsya
the
.invoke(function (ctx) {
// Our response to the challenge of team – ask user to choose language
return ctx.sendMessage('settings.locale');
})


3. The definition of the user's time zone.

If you go and ask the ordinary citizen to call your time zone, he may not understand. But almost all human beings more or less confident in the name of the city in which they are located. We use this fact.

To determine the time zone, thus, the need directly or to request the location of the user, or ask user to print the name of their town. Practice shows that this approach is good and no scares.

The name of the city translate the coordinates by Map of the http geocoder (note, you need to get the key).

Coordinates fed timezoner, which will return the user's time zone.

There are nuances – both services have limitations on the number of requests per day. Therefore:
a) is't cache requests to the geocoder for the same cities;
b) when a person enters the exact location, still run it through a geocoder, get the user's city and work further with the coordinates of the center of the city – thus to all users in Moscow it will be possible to make one request in timezoner.

The code module for determining the time zone, see SDAs.

4. Storing user data

Earlier, we noted that the bot-brother knows how long to keep data tied to a specific user. This functionality is implemented using Redis.

In more complex bots in Redis, we store only a small amount of user session data, but the bulk still have to be placed in a full database (e.g. MongoDB).

But for this bot I decided to use Redis-om.
Reading user data from the storage occurs before processing the next command, and the record back to storage after the loop processing of the next command.

5. Tracking the time in which you want to send a notification.

In our other bots you want to send messages to users at certain times of the day. On the server spinning process, which checks the time, checks the records in the database and regularly sends alerts.

But for this bot, found a more fun solution. In Redis, you can add the keys with the specified life time. When the key turns stagnant, Redis sends us an event. That's all.
As soon as we need to add a notification, we write to Redis key with the right time of life, and then just listen to events and send them notifications to users.

6. Search time or date in the phrase

There is a wonderful library chrono-node, which is able to search for phrases.
By analogy appends code for the Russian language.

We are now more or less present:
the
    the
  • How to create a bot;
  • the
  • How to set localization;
  • the
  • How to register commands and their handlers.
  • the
  • As declared keyboard with answers;
  • the
  • How to organize the storage of user data;
  • the
  • How to organize a definition of user time zone;
  • the
  • How to isolate date and time from the text.

Then we just gently to program all the commands for the bot and debug. Full version look in the same repositorio.

2 days. Flight predictable.

We published a DeLorean in storebot two days ago. Stats:
the
    the
  • 38 average rating of 4 stars;
  • the
  • Out of the top three Best New Bots, got into a thematic newsletter storebot;
  • the
  • 1500 unique users.

Universal guidelines for creating popular bot


We have already shamelessly bragged that our bots crawled to the top of the bots and for a long time sitting there.

Why is it important to have a high place in the rankings?
the
    the
  • the higher the boat, the greater the probability that somebody will accidentally stumble.
  • the
  • If a bot goes up, it can get into distribution storebot (Best New Bots, Bots Top Chart). Distribution respectively will attract you a decent amount of new users.

Of course the telegram would never publish magic algorithms, which compute the ranking of bots. However, we have on this subject there is a hypothesis that seems quite plausible.

So, your bot is most dependent on the recent assessments in storebot. If your bot has recently put a lot of positive ratings (5 stars), it is likely to go up in the rankings.
The number of uniques in the bot and the intensity of its use, it seems, does not affect the position in the ranking. Otherwise we cannot explain how our Zodiac_bot manages to overtake in the rankings, for example, super bot Yandex.Pictures :/

On the basis of this idea conclude that the only way to move the bot in the store – not to upset the target audience of the bot and make users most happy.

Now bots is a young and strange niche. A key feature of the vast majority of bots is a strange interface.
The author of the article has formed the subjective feeling that the bots are written by the developers for developers. And for some reason few people write bots for people.
If you make a bot for developers, it is unlikely that you will be able to raise it high in the rankings. Just because non-programmers will be confused by the interface and put low scores. Your cap.

In this regard, several obvious recommendations, how to make a easy bot for user-programmers.

instead of Keyboard commands

Yes, I know that command is very cool. The commands in the console, the commands in the messenger – is this not happiness?
But, surprisingly, a large part of the population does not like to drive team.
Hence recommendation # 1: to absolutely any user who does not fall into a stupor when working with your bot (even your grandma), always, at almost any stage of work with the bot, show the user the keyboard with the possible options. Team Express for geeks, let them be happy.

don't need to ask people complicated things

The idea I have already revealed in the section about the time zone definition. The less the user have to think and click with your bot, the more likely that it will not fall off.

feel free to use emoji

The bots feature a small number of expressive means to create attractive interfaces. So, if we have the ability to make a button with icon and text, it should be used.


Love negative reviews

A lot of negative reviews is not the worst situation for the product. The worst reaction – no one uses it and says nothing. Here there really is not clear what to do.
And when you have negative feedback you can:
a) to finish the bot
b) fix bugs (if bug reports)
C) write to the author of negative evaluation and to talk about the bot

We actively practice all three approaches. Serge wrote many times users, who left some strange feedback with a low rating, together they found the bug, fixing, and almost always the user has returned to deliver a high evaluation to the bot (which we know contributes to its distribution).

Digression – the most funny reviews left by users Dating bot.

But Mehregan is right, but what about the soul?..

Philosophical arguments about the monetization of bots


Theoretically, we understand that if we have an audience of 2000 people a day, it is something you can earn. But almost no understand how.
Now our headache is the growing weather needs of the bot. His audience is growing, the requests to the weather API grow and our maintenance too. When the growth will stop is unclear.

We came up with three theoretical approaches to the monetization of the bot:
    the
  1. to Make a button for donations. Plus approach is unobtrusive. Cons – most likely the income will be close to zero.
  2. the
  3. Paid subscription. Allow to use the bot for free, for example a month, then please buy a key to the bot per dollar. Pros – just don't leave in a minus. Disadvantage – can lose all your users.
  4. the
  5. advertising in the bot. Pros – chance to earn I seem to have. Cons – can severely ruin the karma.

We try to develop the approach number three, which is about advertising. Sergei wrote a platform for advertising bots adbot.io. Read the Doc and see you on the platform's website — adbot.io. It is quite new, yet it is only our bots and no advertisers. All this is pure experiment, so we call all concerned people to participate. We really want to find a deal with arbitrage, which will agree to place in our bots (at first you even for free) just to understand if viable promotional exchange for the bots.

If any of these approaches will make the bot profitable (or at least not unprofitable), then perhaps the niche is not hopeless. And if you do not allow, apparently it will die overshoes as a beautiful concept.
Article based on information from habrahabr.ru

Comments

Popular posts from this blog

Powershell and Cyrillic in the console (updated)

Active/Passive PostgreSQL Cluster, using Pacemaker, Corosync

Automatic deployment ElasticBeanstalk using Bitbucket Pipelines