M
M
maqstein2019-10-13 17:47:15
Python
maqstein, 2019-10-13 17:47:15

What should be the architecture of the telegram bot project?

I am writing a bot in a cart,
I want to make such an architecture
5da336b71d40e629338801.png
for the handlers to work, I need a client object that is located in connections.
but the problem is that when importing the "client" object in the handlers, each time a client instance is created and an error is thrown because of this (there can be only one client).
how it is possible to import object that it was not created each time?
because shove everything in one file is not good

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
luchanos, 2020-05-16
@luchanos

Create some class, call Resources for example, and write there everything that you will need to use in your project - clients to the database, queues and other stuff in the form of its attributes. When raising your application, create an instance of this class and import it for all modules, throw it into the necessary handlers - it turns out you created everything for yourself once and now you just use it.
In short, something like this:
you make a resource class:
class Resources:
def __init__(self, tg_client):
self.tg_client = tg_client
when you raise the application, create an instance of it and pass your tg_client:
resources = Resources(tg_client=tg_client)
and then in any other module with a handler, simply import resources (import this instance) and use it in the handler by accessing the resources.tg_client field (you can make a client for the database, queues, and everything else in the same way)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question