📜 ⬆️ ⬇️

VC bot on the knee, or how to please people February 14

Of course, each of us loves gifts, but most of all we love the wishes that accompany them. And, until recently, we did not have the opportunity to pleasantly surprise a person with warm words until the idea came to mind: what if we give people the opportunity to exchange valentines (on February 14, on the nose, after all) without leaving the framework of the usual way chat - social network chat?

Word for word, and here it is - a ready-made business plan for creating an atmosphere of the Valentine's Day holiday! Make people happy?


For an idea on the moon!


Of course, first of all it was necessary to draw up a plan of action and describe the idea. Due to the fact that people have become more shy, and are not always ready to trust anyone, the choice fell on writing a fashionable and convenient bot for VK, which has a familiar interface, will always be at hand, and the anonymity of the messages will be fixed in the program code.
Unfortunately, in addition to all the above advantages, the communities have two disadvantages: low bandwidth (number of messages per second) and a ban on starting a dialogue with a random user (the user must be the first to express his consent to receive messages on behalf of the community).

So, we have a user who is ready to send a valentine to his soulmate, a recipient and an intermediary in the form of a chat bot. The interaction scheme is simple, the bot should: suggest sending → know the recipient → save until February 14 → deliver → repeat.

But this, of course, was not enough for the gluttonous imagination of the mind, and, in order to increase the activity and involvement of the audience, a scheme of a different interaction was considered, allowing people to meet inside the system, sending messages to random people. These “random valentines” can be appreciated, and if the valentines of two people like each other, we give them the opportunity to talk.

Disclaimer


This project was created purely on enthusiasm by two people, a programmer (a lesson) (me) and an ideological inspirer (Stepan sadfun Popov), does not carry the purpose of teaching you something (although it can help you understand the essence of the universe), only tell an interesting story of creation and follow the path from idea to naive implementation.



Learning to talk with VK


After going through 3-4 different libraries for the “NodeJS VK API” request, I realized that there are no simple and functional libraries that allow using promises, as well as working through execute. Remember one of the problems described above? Yes, the execution of requests to VKontakte indirectly, but in batches of 25, allows us to increase the capacity at times .


Comparative ability of various methods of requests to VK

Therefore, it was decided to write something of their own, reliable and not very crutch decision, based on the node-fetch library.

In order not to give all the code here, just describe the logic of the work.

Since requests from the program need to be packaged into one execute, they are collected in a LIFO queue, and then sent if:


Requests from the queue are assembled into the VK Script code, which (in this implementation) looks like this:

var returnables = []; returnables[0] = API.messages.send({"message": "Привет, Хабр!", "peer_id": 1, "random_id": 561427}); returnables[1] = API.users.get({"user_ids": 1, "fields": "sex"}); return returnables; 

After executing the code with a request to VK, the result of each request (at the index returnables ) is returned to its callback, beautifully wrapped in Promise. The effectiveness of the approach is off the scale - the more users (and, therefore, more requests to VKontakte to send), the lower the response delay. In order to keep the limit under control, there is a counter in the dispatch system that shows the number of remaining requests for that second, which allows you to quickly process a surge of visitors.

Learning to listen to VK


There are two ways to receive notifications about events from VKontakte: Longpoll-requests and Callback-server . Your server is convenient because it does not require special gestures to use, and also allows you to receive missed notifications (for example, when you restart the server). Such a server can be written in several lines using the native http.Server class.

All this creates an ideal ecosystem for the logic of the program, which is convenient to use.

Everything ever ends


Since the user is free to do anything in the chat, and we are not able to stop him, we need to restrict him to a reasonable framework. The state machine perfectly copes with this, setting every possible transition inside the system, and using buttons (the keyboard parameter in messages.send) will make using the bot as simple as one touch of the screen.

Here is a diagram of user interaction with the bot:


All this turns into a set of states ("Main Menu", "Valentine's Input" and so on), the transitions between which are set and transmitted in the buttons, or are known initially and do not change.


By the way, about the buttons. Their color range is non-variable ( 4 colors for all occasions), but it is the buttons that make keeping the number of user errors to a minimum. On their basis, you can build absolutely any non-linear system, which is why they are used everywhere. And in this project too.

But you need to understand: if you are aiming at a large audience reach, you should consider another way of interaction, because someone may have an old application ( VK for iPad , for example, has not been updated for a very long time, I will not lie, but it seems more than a year and keyboard support is not there). And it happens (yes, it happens, I checked) that people, not understanding that buttons can be pressed, simply rewrite their contents (and then the parameter of the payload button, of course, is not transmitted, and everything can break).

It should be noted that not everything is as smooth as described in the diagram, and sometimes funny cases occur. For example, the VKontakte link detection system incorrectly handled users whose short links began with id , and cut it off. The surprise of the people who met with this bug cannot be described, because they wrote Valentine Valentine, but it turned out that Olezhka.
What Olezhka ????

Accidents are not accidental


So, if everything is clear with ordinary valentines, there is a recipient and a sender, then how to bring two strangers together? We need to exchange them with valentines, and if people like each other's valentines, they should be introduced! Here it is, the formula of love!

Although it sounds pathetic, but it works, and it becomes more interesting for people to work with the bot - they will get a positive response if there is at least one person (which, of course, the bot will take care of).

This results in a big plus - the more people take advantage of this opportunity, the more chances they will get to know each other, and consequently, this, like a game, keeps a person in the chat. It is worth admitting that the session of an average user is ~ 7 minutes, here there is the potential to tighten a person by 10-15 just for the sake of this feature.

The message sending itself made a separate scheme, since there are several options for using this bot, the user can lose sight of something, and the bot reminds him of this neatly.


It is interesting how the second problem was solved, with the inability of the community to be the first to initiate a dialogue with the user: now this problem is solved with such a message.
But I have a condition! For the recipient to receive your valentine, he must also write to me. Your task now is to persuade him to write me something. This is done so that you do not have the fear of talking to him!
In general, the traditional “No, bug, but feature!”

Fireworks at the end


Actually, that's all! If you do not take into account some difficulties in understanding the logic of the VK API, as well as confirming the account with one of the providers, everything went too smoothly.


Sample chat with bot

The bot works and will please people with valentines, making them happy. This is done to help people be kinder to each other. Rate it all you can personally in the community Valentinych - vk.com/verylovebot , if you wish. Thanks for attention!

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