71 lines
1.9 KiB
Python
71 lines
1.9 KiB
Python
import asyncio
|
|
import queue
|
|
from .tgbot import TgBot
|
|
|
|
class TgBotUpdater:
|
|
is_run = False
|
|
app = None
|
|
tgbot_class = None
|
|
my_queue = queue.Queue()
|
|
|
|
return_values = dict()
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def __init__(self) -> None:
|
|
self.loop = asyncio.get_event_loop()
|
|
|
|
try:
|
|
self.tgbot_class = TgBot()
|
|
except Exception as e:
|
|
self.logger.error(e)
|
|
|
|
|
|
async def _set_webhook(self):
|
|
await self.tgbot_class.set_webhook()
|
|
|
|
async def _start_app(self):
|
|
await self.tgbot_class.start_app()
|
|
|
|
async def _set_hadlers(self):
|
|
await self.tgbot_class.set_handlers()
|
|
|
|
async def _run_func(self):
|
|
from .tgbot import TgBot
|
|
|
|
while hasattr(TgBot, "app"):
|
|
# self.logger.info(f"check updates in {await TgBot.app.bot.get_webhook_info()}")
|
|
if not self.my_queue.empty():
|
|
item = self.my_queue.get()
|
|
if (
|
|
isinstance(item, dict)
|
|
and "name" in item
|
|
and item["name"].startswith("admin_")
|
|
):
|
|
await self.tgbot_class.admin_action(item["name"], item["queryset"])
|
|
else:
|
|
try:
|
|
await TgBot.app.process_update(item)
|
|
except Exception as e:
|
|
print(f"Error in tg thread {e}")
|
|
await TgBot.app.process_update(item)
|
|
self.my_queue.task_done()
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
async def main(self):
|
|
await asyncio.gather(
|
|
self._set_webhook(),
|
|
self._set_hadlers(),
|
|
self._start_app(),
|
|
self._run_func(),
|
|
)
|
|
|
|
def run_func(self):
|
|
asyncio.set_event_loop(self.loop)
|
|
self.loop.run_until_complete(self.main())
|
|
self.loop.close()
|
|
|
|
tg_bot_updater_instance = TgBotUpdater() |