Closed jeromepacman closed 1 year ago
Hello,
As you may have seen, although it is a single project, there are two programs running at the same time, one is the BOT which runs in a terminal with python manage.py start_bot
and the other is the Django project with python manage.py runserver
.
Both runs share some routines and libraries but they are not connected to each other, except for the running_bot_data.json
file using the RunningBot
class which only reads and writes that file which contains Pyrogram's "session_string
" so that it can be used in the other process (Django) to send messages.
All the BOT routines are hosted in bot/ and its smart plugins in bot_plugins/ and are executed directly from bot/management/commands/start_bot.
When you are running Django the first thing you check is the BotConfig(AppConfig)
class in bot/apps and that is where the Pyrogram client is initialized in the ready()
function Please check: AppConfig.ready()
Having said that...
To put it into production you must use uvicorn/gunicorn in the django python manage.py runserver
process as django deploys are normally done.
But the python manage.py start_bot
process runs on its own thread and is not accessed from the outside except by Telegram's routines in bot/plugins.
Technically, Django does not require the use of Telegram (Pyrogram) other than the "session_string
" as I mentioned before and that is only for sending messages from Django to Telegram (via Pyrogram).
On the other hand the BOT itself connects to Telegram via MTProto and remains active until it is shut down.
I hope I have explained how it works.
Thanks for writing
thanks for your details & therefore your time Visionario. I appreciate. Ok, I understood your words.
Currently I have django running under mod_wsgi on Apache Centos. So as I need to manage another thread for the bot , It would be more appropriate to use gunicorn/uvicorn because my problem is how I can make the bot work the same way. In dev, all is ok, I’ve extended the TelegramUser model, added more apps in order to get a deeper Telegram moderation group bot, with the Django dashboard admin as monitoring.
By typing both manage.py commands it runs smoothly. In prod, Django part is ok, even if Django server is not running, the ORM works indeed but I still have to run the bot from a terminal (which is not persistent, obv).
However, I tried a deploy on Heroku, with something like this: "web : gunicorn app.settings & python manage.py start_bot" & It works. (similar deploy failed on fly.io, btw) "manage.py" is an admin command that is not intended for prod, afaik. Well, I certainly still have a lot to learn about deployment beyond the classic wsgi.py entry point. :)
I hesitated for a long time to write to you but I would really like to know what direction you are taking to put the bot with Django in production & run the conf simultaneously.
Are you doing WSGI with Gunigorn?. dockerizing?. I
'm on it for days with little sleep )
Sorry to bother you with this, I think the idea of attaching the Django ORM to the Telegram bot is great. I'm working on this too, but I'm new to MTProto.