📜 ⬆️ ⬇️

Telegram bots: to help the editor

- A synonym for the word "capricious"? - the voice of his wife came from the next room.

The cracking sound of the keyboard and the cat, taking advantage of the pause, decisively declared their miserable half-starved existence, apparently hoping that right now the masters would finally tear their ass off the chair and feed the unfortunate.

- So look at this ... how is it ...
- Yes, yes, I also forgot. Well, so tell me a synonym?
')
But alas, I am not a walking encyclopedia, and not even its sedentary version, so I did not find anything better than sending my wife to google dictionary.



- So it is a long time! I thought you would remember faster ...

The cat, Buridan's donkey, was throwing between me and my wife, in the end I chose to be more good (interesting - by weight or for some other parameters?), And I had no choice but to go to the kitchen to feed the unfortunate bastard. Along the way, thinking about how to bring to my wife the fact that the “bookmarks" in the browser are not intended for the general preservation of all her friends from Vcantaktik, but for memorizing the really necessary links. All previous attempts to teach her to use bookmarks ended in saving, if not friends, the music laid out by friends, but the links that I saved for her turned out to be completely buried under these rubble.

But if she learned to use Vaktaktikik and Telegram, it means ... it means she already knows how to use Telegram and Vkantaktikik! Eureka!

Someone said that bots for Telegram is easy? But now we take and try ...

How to register the user part of the new bot, you probably already know. If not, just add @BotFather to the Telegram and start a conversation with it.

@BotFather is the official bots management interface. In addition to it, requests for creating bots go around Manybot, YourBot, and a few more. Judging by the description, these are some kind of gaskets, because when working with them, you still have to contact @BotFather. I did not understand them and I will be grateful for clarifying the benefits of them.

The first problem encountered when creating a synonym-bot is the search for a dictionary of synonyms.

Most nagulennyh dictionaries are full of formidable inscriptions and signatures with a list of terrible cars, which will certainly fall on the head of the unfortunate, who dared to use the “improper” creation of the authors. Who, judging by the number of tastefully painted threats, has a “copyright in Russian” entirely, completely and without any exceptions. Moreover, the “purpose” on such resources is sometimes not defined anywhere and in any way, and the volumes of their dictionaries, as a rule, amount to a measly 5,000 ... 40,000 words.

Part of the dictionaries are alphabetical lists: the first page with the words with the letter “a”, the second with “b” and so on. Here is not something that parsing, even loading the hell of a footwoman turns into a stand-alone problem.

And, of course, no API is even mentioned.

In the end, I managed to find an adequate dictionary with the volume, as stated in the description of “591 thousand words and idioms and 2166 thousand synonymous relations”, which is also freely distributed. Its only drawback was the complete absence of the API, which was compensated by the ability to legally download the dictionary itself (albeit in a terrible format) and a stable online version.

It also seemed to me that in this particular dictionary there are too many obsolete, out-of-turn words and, on the contrary, too few modern ones. But the wife noted that in five years everyone will forget about the new-fashioned “selfies”, “cover” and “transparency”, but “surely”, “a little bit” and “capitulate” will live in the language forever, despite the tricks of linguists from the authorities.

Once again I am convinced that a good product is usually a terrible package, and behind the beautiful “le-endings” with large buttons and air fields there is an ordinary bullshit.

At this point, the already well-fed cat broke away from licking his warm analog identifiers and affirmatively squeaked as if confirming that he was not going to change the classic “meow”, despite the fact that someone might say that it is “uncooked” or “sucks” .

The second problem - most likely the names of the bots that suit you will be occupied. I tried a whole bunch of sweet short names and they all turned out to be busy and — tadaaaam — expectedly dead. As a result, it was necessary to register the name @synonim_bot, long and difficult to remember: for some reason, squatters did not get to it and it turned out to be free.


In short, the whole scheme works like this:

0. You install a webhost - you tell the Telegram API the script address on your server (HTTPS!), Which it will contact when the user writes something to your bot.


And then implement your logic. The diagram shows a shortened version of the bot logic. Synonym .



I deliberately do not cite a single line of code in order to exclude holivars and mutual cooling. Write on what you know - bot anyway.

Now with the text:

1. User writes your bot something - a command or a word.

2. API Telegram pulls your script (it already knows its URL from item 0) and sends it about the following:

{ "update_id":12345678, "message": { "message_id":1234, "from": { "id":0123456789, "first_name":"Vasya", "username":"vapupkin" }, "chat": { "id":1234567890, "first_name":"Vasya", "username":"vapupkin", "type":"private" }, "date": 1451606400, "text":"preved" } } 

3. Next, your script should figure out what was sent to it and issue an answer:

https://api.telegram.org/bot123456789:boLshoiogRomnIycOLlaideR/sendMessage?chat_id=1234567890&text=Taki%20Preved

Probably it would be more correct to rewrite it in POST in order to have a reserve in size of the text to be sent, but in my case this is also enough: after all, the messengers are not intended for Longrida.

4. API Telegram will send back something like this:

 { "ok":true, "result": { "message_id”:1234, "from": { "id”:9876543210, "first_name":"", "username":"my_bot" }, "chat": { "id": 0123456789, "first_name":"Vasya", "username":"vapupkin", "type":"private" }, "date": 1451606500, "text":"Taki Preved" } } 

I understood that with this answer I can do as I please (if not correct, correct it). Theoretically, it is possible to collect statistics based on it, but I collect it at an earlier stage: it is more convenient for me.

A few words about the differences sent by the user commands from, sorry for the pun, words.
You can prepare the standard answers of the bot to the standard questions of the user, making this farm in the form of commands using @BotFather - they will be visible to the user if he starts the line with a slash. In the bot script, it will be enough to catch these commands (the API Telegram passes them, with a forward slash), to give an answer and immediately stop the further execution of the script.



The procedure of communicating a bot with a dictionary is not of artistic value and I will leave it out with your permission. The only thing that needed to be done was to catch the absence of the result, when the dictionary did not find a synonym for the entered word.


... as well as the case when the user entered a whole phrase - after all, the dictionary does not know how to do this:


The third problem was waiting for me when real people started using the bot:


Yeah ... Modern technologies sometimes allow us to work wonders, but they completely deprive us of knowledge about fundamental things. Almost half of the requested words contained errors and typos and something had to be done about it.

With timid hope, I immediately rushed to my beloved Dadata, but I broke off: she perfectly performs her tasks and does not aim at the entire language corpus. Googling, oddly enough, did not bring any adequate results, and I quickly screwed Yandex.Speler as a temporary option. Theoretically, until he became paid and demanded a notarized copy of his passport, he could also be used somehow.

I would be grateful for the hint: a free spell checker service in Russian with an API is required.

A fourth problem was discovered here: if the word is typed without errors, Yandex.Speler does not give out anything. It is just silent as a partisan, which makes statistics a little harder.

It would be nice to somehow mark the correction of user input, without shouting about it in all Ivanovo. Because of the poor bot's html toolkit, I chose this alert option (perhaps in the future I will correct it):


I put my wife to test, asked to distribute to colleagues on the network. If the bot is necessary for someone, I will develop it.

PS: When writing a bot, the Unix cat (real character) did not starve. Neither before nor after.

Proof




References:
Bot Synonym: telegram.me/synonim_bot
Trishin Synonyms Dictionary: www.trishin.ru/left/dictionary
API Telegram: tlgrm.ru/docs/bots/api#authorizing-your-bot

Source: https://habr.com/ru/post/312886/


All Articles