49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
from asgiref.sync import async_to_sync
|
|
from django.apps import AppConfig
|
|
import asyncio
|
|
import threading
|
|
import queue
|
|
import time
|
|
|
|
|
|
class TgBotClass(AppConfig):
|
|
default_auto_field = "django.db.models.BigAutoField"
|
|
name = "tgbot"
|
|
is_run = False
|
|
app = None
|
|
|
|
update_queue = None
|
|
my_queue = queue.Queue()
|
|
|
|
# @async_to_sync
|
|
async def init_bot(self):
|
|
from django.conf import settings
|
|
from .tgbot import TgBotApp
|
|
|
|
tgbot = TgBotApp()
|
|
app = await tgbot.init_tg()
|
|
await tgbot.set_webhook(f"https://{settings.TGBOT['base_url']}/api/tgbot/")
|
|
return app
|
|
|
|
def updater(self=None):
|
|
while True:
|
|
if not TgBotClass.my_queue.empty():
|
|
# Здесь нужно добавить код для обработки очереди
|
|
item = TgBotClass.my_queue.get()
|
|
print("Беру дело из очереди")
|
|
async_to_sync(TgBotClass.app.process_update, force_new_loop=True)(item)
|
|
TgBotClass.my_queue.task_done()
|
|
time.sleep(1) # Ждем 1 секунду перед следующей итерацией
|
|
|
|
def ready(self):
|
|
import os
|
|
|
|
if os.environ.get("RUN_MAIN", None) != "true":
|
|
return
|
|
if TgBotClass.is_run:
|
|
return
|
|
TgBotClass.is_run = True
|
|
TgBotClass.app = async_to_sync(self.init_bot, force_new_loop=True)()
|
|
|
|
print(TgBotClass.update_queue)
|