On March 11-12, the
Hackathon Yandex.Money passed, in which we, the Happy Santa team from St. Petersburg, took part. There is already
a separate article about the winners. Our team is not there, so we decided to tell the Habrasoobshchestvu about their project and solutions that we used.

')
How did you choose the idea?In 24 hours, the not so simple task was to be solved: the organizers of the Hackathon asked to suggest interesting ideas and unusual cases that go beyond the “selling” bots (spoiler: just such a bot eventually won).
We decided to make the seller and the answering machine bot not because of a lack of ideas, but because we work in eCommerce ourselves and are well acquainted with the problems and many unobvious requirements for the business logic of stores. Assessing our strength, we decided to make a bot that makes purchases, informs about the status of the order, answers the popular questions of users and collects feedback. In addition, it was necessary to create a warehouse with reservation of goods and a CRM for orders and couriers in the minimum working condition. To make everything even more interesting, we decided to support two types of bots:
More simple for Telegram, which responds to static commands.
More difficult for VKontakte, which responds to the usual human language and is more like a real person.
How did we do it?This is not the first Hackathon for us, once we even took second place on the Hackathon VKontakte. And as we already knew, the most important task at such events is to decompose tasks well (in fact, it is beautiful to present the idea and impress the judges, but this is for whom and what is more interesting). First of all, we broke all the tasks and tried to isolate them as much as possible, so that each of us could code independently of each other. To summarize, we have three categories of tasks:
Web + integration with Yandex.Money.
Transport level - receiving and sending messages to instant messengers.
Brain - making decisions about the reaction to the message.
Such a partition allowed us to independently implement the selected tasks and to distract ourselves to the minimum, except then to coordinate the interaction between these layers.
Stack.Hackathon is not a place where there is time to learn something new, so we chose our usual stack: PHP 5, MySQL, RabbitMQ, Redis. There was a desire to try PHP7, but decided not to risk it. RabbitMQ was chosen as the glue between our conditional layers, which allowed us to separate the code base. MySQL is a repository of our orders and their statuses. Redis - caching, blocking.
The problems we are facing.The main problems were at the transport level. We have previously done bots for VK and Telegram and were familiar with the features, disadvantages and advantages of each of the platforms. Messages were downloaded from instant messengers via Long Pool, which greatly facilitated the development, moreover, it was implemented in VK and Telegram almost the same. For reliability, we have provided an algorithm for automatically restarting the process of receiving messages from the API in case of fatal errors and resuming messages that were sent when the bot was offline. On VKontakte, sometimes there is a problem with sending messages through the API: you can’t send a message the first time, so you have to repeat sending. This is where RabbitMQ helped with the support of deferred messages (ttl + dead-letter). The same method was applied in Telegram. They have a similar problem: periodically error 502 begins to come to any request. This is treated by time or by re-issuing the bot token (but not always).
As we wrote above, it was decided to make two versions of the bot. Telegram has a convenient implementation of the bot menu, the command system worked well there, so the bot was strictly guided by them and interacted with the user strictly through commands and menus.
In the “smart” version, the bot could ask standard questions that customers usually ask support: about payment, delivery method, size, vacancies. But how to understand what the user wants? There was little time, and I wanted to make it pretty cool, so we decided to improve the accuracy of the bot by completeness. The “smart” bot responded to messages that contained only one question containing from three to ten words. In this way, we weed out normal messages from users and complex sets of questions. There remained only clear areas of topics to which we could give a clear and correct answer. If the bot did not know what to answer or the message was eliminated, then we sent one of the ten standard answers about the fact that the bot is smart, but not very. We added ten different answer options in order not to fly away randomly in spam. What pleased me, this approach worked well, and if the buyer did not specifically try to “break” the bot, he could successfully familiarize himself with the terms of delivery, payment and place an order.
Problem number one : how to understand where the questions in the message and how to find them there? To begin with, we split the text into sentences, and then we looked at each sentence separately.
Signs of interrogative sentences were fairly simple:
1. Question mark
2. One of the interrogative words (“where” / ”when” / ...),
3. Union "a" at the beginning of the sentence.
Problem number two : correcting mistakes in words. To solve this problem, we used
pspell . He suggested too many options for correction, so we filtered them using the Levinstein distance: replacement weight = 1, removal / addition weight = 2, choosing any option from options with lower weight.
Problem number three : how can one understand what it is about? There was no time for an uber cool solution using linguistic analysis and machine learning, so we were just looking for supporting words. But, as you know, in Russian words have many different forms. Another technology from Yandex came to the rescue -
Mystem . Mystem is a separate program for the morphological analysis of the Russian text. It allows you to do a lot of cool things, up to determine the shape of a word, but we were interested only in one thing - bringing the word into its normal form.
What is interesting, some people when communicating with the bot tried to adapt to it. They did not try to ask him questions, as they would have done with an ordinary person. And they tried to make his life easier and sent him specific words “delivery / payment”, and he was waiting for questions from them. So the future, apparently, is for combined bots that can communicate and understand commands.
How did it all work in life?They promised to judge by five criteria: mass character, realization, convenience, human-like character, originality. And it was separately stated that the bot should really work, but as it turned out, this condition was not obligatory for victory.
It seemed to us that the best presentation would allow the judges and participants to make a real purchase through a bot, chat with him, pay for this purchase, choose a delivery method, and then get the goods by courier directly to Hackathon. As it turned out, we overestimated the interest of people. None of the judges made a purchase and did not feel for themselves how the bot works and why we did something really interesting. Moreover, we specially prepared wooden icons with the Yandex.Money logo to motivate as many people as possible to make a purchase.
The bot is working now, so you can try it yourself:
santa12.tkvk.com/hsstoretelegram.me/hsstorebotIn the screenshot you can see an example of how the ordering in VKontakte took place. The user selects the product he likes, clicks "Buy". At this point, a dialogue with the community is created, and the corresponding product is reserved for 5 minutes at the warehouse. At this stage, the bot can collect any information about the user: name, address, other additional fields. We only needed a name. Delivery was carried out in two ways: shipment at own expense from our table or the courier. Payment went through Yandex.Money. If the payment has passed, the order is assigned a unique code that is sent to the user so that the courier can transfer the goods to the correct recipient. By the way, the courier made an appointment to meet on the territory of the Hackathon in the same chatik, and after transferring the goods, he changed the order status to CRM. At this point, the user received a message from the bot that the order was delivered and an offer to leave the wishes for the service.

Unfortunately, we could not win. But anyway, thanks to Yandex for the event and all the participants. And especially those who used the bot and bought badges. It was nice to see your surprise on how the bot worked, and on our delivery service. By the way, if you are from St. Petersburg, then we still have a few badges and you can still buy them for a ruble.
Afterword.Finally, I would like to note that we were the only team on the Hackathon that worked with the VKontakte API. There was another good team with a bot for Slack, and the rest of the polls worked with the Telegram API. We were even hinted that the judges most likely do not have pages on VKontakte, and they will not be able to evaluate the work of the “smart” bot. Of course, Telegram has a good APIC for bots, but bots are still made for users. But with this they still have problems. So we advise everyone not to forget about VKontakte, all the more so that they very actively began to work on an API for community messages, products and will soon add a lot of new and cool features.