This commit is contained in:
aarizona 2024-07-04 21:33:00 +03:00
parent f89a1f365a
commit 639857b310
4 changed files with 27 additions and 14 deletions

View File

@ -27,7 +27,7 @@ SECRET_KEY = "django-insecure-ruo!wst&sb8(f9)j5u4rda-w!673lj_-c0a%gx_t@)ff*q*2ze
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = True
NGROK_TEMP = "a44c-193-228-134-167.ngrok-free.app" NGROK_TEMP = "d6af-193-228-134-167.ngrok-free.app"
ALLOWED_HOSTS = [ ALLOWED_HOSTS = [
"localhost", "localhost",
NGROK_TEMP, NGROK_TEMP,

View File

@ -25,15 +25,24 @@ class TgBotClass(AppConfig):
await tgbot.set_webhook(f"https://{settings.TGBOT['base_url']}/api/tgbot/") await tgbot.set_webhook(f"https://{settings.TGBOT['base_url']}/api/tgbot/")
return app return app
def updater(self=None): async def some_function(self=None):
print('updater')
while True: while True:
if not TgBotClass.my_queue.empty(): if not TgBotClass.my_queue.empty():
# Здесь нужно добавить код для обработки очереди
item = TgBotClass.my_queue.get() item = TgBotClass.my_queue.get()
print("Беру дело из очереди") await TgBotClass.app.process_update(item)
async_to_sync(TgBotClass.app.process_update, force_new_loop=True)(item)
TgBotClass.my_queue.task_done() TgBotClass.my_queue.task_done()
time.sleep(1) # Ждем 1 секунду перед следующей итерацией time.sleep(3)
async def some_callback():
await TgBotClass.some_function()
def between_callback():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(TgBotClass.some_callback())
loop.close()
def ready(self): def ready(self):
import os import os
@ -44,5 +53,7 @@ class TgBotClass(AppConfig):
return return
TgBotClass.is_run = True TgBotClass.is_run = True
TgBotClass.app = async_to_sync(self.init_bot, force_new_loop=True)() TgBotClass.app = async_to_sync(self.init_bot, force_new_loop=True)()
thread = threading.Thread(target=TgBotClass.between_callback)
print(TgBotClass.update_queue) thread.setDaemon(True)
thread.start()
print(TgBotClass.my_queue)

View File

@ -16,9 +16,9 @@ class TgBotApp:
async def start(self, update, context): async def start(self, update, context):
user = update.effective_user user = update.effective_user
logger.info(update) logger.info(update)
await update.message.reply_html( await update.message.reply_to_message(
rf"Hi {user.mention_html()}!", rf"Hi {user.mention_html()}!",
# reply_markup=ForceReply(selective=True), reply_markup={}
) )
async def set_webhook(self, url): async def set_webhook(self, url):

View File

@ -24,10 +24,12 @@ class ItemViewSet(viewsets.ViewSet):
@async_to_sync @async_to_sync
async def create(self, request): async def create(self, request):
req = json.loads(request.body) req = json.loads(request.body)
logger.info(req) # logger.info(req)
logger.info(TgBotClass.app.handlers) # logger.info(TgBotClass.app.handlers)
update_item = Update.de_json(data=req, bot=TgBotClass.app.bot) update_item = Update.de_json(data=req, bot=TgBotClass.app.bot)
# TgBotClass.my_queue.put(update_item) # logger.info(update_item)
await TgBotClass.app.process_update(update_item) TgBotClass.my_queue.put(update_item)
logger.info(TgBotClass.my_queue.qsize())
# await TgBotClass.app.process_update(update_item)
return Response({"result": "pass data to tgbot"}) return Response({"result": "pass data to tgbot"})