📜 ⬆️ ⬇️

Application for the multiplatform bot framework

image

Chatbots tightly entered our information life, all social networks and messengers respecting themselves have bots support, they provide a convenient API for their development and use, everything for the convenience of users and programmers. Now, to create a bot in Telegram it is not even necessary to know any programming language. There are services that allow you to design a bot using the web interface. And this is really good, but quite recently I ran into a problem and was surprised that in all this wave of popularity of chatbots, which, moreover, are now not just a cool feature, but also an income generating tool - there is practically no answer.

And the question is: what if I want to create more than one bot for one service? For example, I know that Central Asia is sitting in Vkontakte and Telegram, how can I write with minimal effort to write bots that work with people both there and there? If suddenly I (for example, as the owner of an online store) also want to use the bots to work with the audience already on Facebook, will it really be necessary to develop a new bot from scratch and rewrite all the logic under it or try to penetrate into the new API or library. And I tried to find the answer under the cut, I ask to the table.

In fact, there are solutions, and there are a lot of them, but they all boil down to the fact that you have to host your bot with a service that also provides a platform. That is, as a boxed solution is not obtained. Perhaps I am wrong and have not fully studied what actually is, but the fact that the first link in Google does not lead to a solution is already alarming. There is also a Botman . This is a really powerful full-fledged developing opensource library with a bunch of chips and comprehensive documentation. And it is written in PHP. I have nothing against PHP , in any case I do not admit it publicly, so as not to breed unnecessary holivar, but I don’t want to develop it at all. I decided to look for something similar to Python, besides for Python there are a lot of libraries that allow you to work with the Telegram Bot API. And I did not find anything, so soon I came to the conclusion that it was worth writing myself. (Maybe someone knows the appropriate solutions for multi-platform bots with an open system, I will be glad to any information)
')
Here, by the way, another problem is brewing. I absolutely can not imagine where else you can share your work with the audience in the form of an article except on Habré. I would be very happy to help in this matter

The name Botovod came by itself.

In general, I will give a small code using Botovod, and explain it briefly.
Spoiler header
from botovod import Botovod, Message, utils @utils.convert_to_text def handler_message(agent, chat, text): message = Message() message.text = "  " agent.send_message(chat, message) @utils.convert_to_images def handler_images(agent, chat, images): message = Message() message.text = "  " agent.send_message(chat, message) def handler_echo(agent, chat, message): agent.send_message(chat, message) settings = [ { "name": "telegram", "agent": "botovod.agents.telegram", "settings": {"token": "462755273:AzBZBQ7AAnqFEXqZ_P8Z-qvCddmjqTVAYPI", "method": "polling"} }, ] botovod = Botovod(settings) botovod.add_handler(handler_message) botovod.add_handler(handler_images) botovod.add_handler(handler_echo) botovod.start() 



Here we define 3 handlers for incoming messages, each sending something in response. The first: “I received a message,” the second: “I see a photo,” the third will simply send back what I received. Then we create the bot manager (Botovod) and pass it the settings for the bots, in the given one for the bot in Telegram. Then we add message handlers in sequence. If a message arrives at the bot, it will go to the first processor that agrees to accept it. For example, if an audio was sent to us, the first handler will try to accept it first, but will refuse, since this is not text. Then he will accept the second one, but he will also refuse, since this is not a picture, then he will accept the third one, which has no restrictions - thus he will send the audio in the same way. Restrictions are imposed as decorators from the utils module.

Now I will try to tell in detail what is what:

There is a bot manager - an object of the Botovod class - the bot name is transferred to it in the constructor (each has its own unique one), the agent class that will process it and the settings for the bot. Also handlers are added to the bot manager in turn. If a message arrives at the bot, the manager will check them one by one until the handler that does not throw the NotPassed exception is found. The first added handler is checked first, the last, respectively, last. If you are going to use the webbooks, then the bot manager can be connected to the web server. To do this, the bot manager has a listen method that takes the bot name, headers, and request body. Next, it passes this data to the agent's parser, which returns the generated message, after this message is pushed by handlers. In response, the listen method returns the dictionary {"status": any_code, "headers": dict (), "body": "any_text"}, where in headers are the response headers, and in body - the response body. Sometimes the messenger / social network requires the server to return an object, so I think this behavior is convenient.

I will give an example for the Vkontakte bot under the name “vk-bot”, and Botovod will connect to the Django server

 def view(request): response = manager.listen("vk-bot", request.headers, request.text) return HttpResponse(status = response["status"], headers = response["headers"], response["body"]) 


The generated message is an object of the Message class. It includes the following fields: text - the text of the message; images - list with images; audios - a list with audio files; videos - list with video; documents - list with documents; locations - a list with places on the map; raw - the raw message body or additional information to it (the dictionary).

Also, each list of the Message object (images, audios, videos, documents) contains special objects whose classes are inherited from Attachment. The class Attachment by default has the url and file_path methods in it, in which the agent parsers usually put information about the received file. The list of locations contains Location objects, in the constructor to which you need to transfer longitude and latitude (longitude and latitude).

Below is an example of constructing messages in handlers.

 def handler(agent, chat, message): out = botovod.Message() image = botovod.Image() image.file_path = "/tmp/1.png" location = botovod.Location() out.images.append(image) out.text = " ,   " agent.send_message(chat, out) 


This is all that is in it, but I hope that this solution will be interesting to someone and someone will want to help with advice, comment, an idea, and maybe their own participation in the development. Thank you all for reading!

Github link

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


All Articles