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!
DEBUG = True
NGROK_TEMP = "a44c-193-228-134-167.ngrok-free.app"
NGROK_TEMP = "d6af-193-228-134-167.ngrok-free.app"
ALLOWED_HOSTS = [
"localhost",
NGROK_TEMP,

View File

@ -25,15 +25,24 @@ class TgBotClass(AppConfig):
await tgbot.set_webhook(f"https://{settings.TGBOT['base_url']}/api/tgbot/")
return app
def updater(self=None):
async def some_function(self=None):
print('updater')
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)
await TgBotClass.app.process_update(item)
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):
import os
@ -44,5 +53,7 @@ class TgBotClass(AppConfig):
return
TgBotClass.is_run = True
TgBotClass.app = async_to_sync(self.init_bot, force_new_loop=True)()
print(TgBotClass.update_queue)
thread = threading.Thread(target=TgBotClass.between_callback)
thread.setDaemon(True)
thread.start()
print(TgBotClass.my_queue)

View File

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

View File

@ -24,10 +24,12 @@ class ItemViewSet(viewsets.ViewSet):
@async_to_sync
async def create(self, request):
req = json.loads(request.body)
logger.info(req)
logger.info(TgBotClass.app.handlers)
# logger.info(req)
# logger.info(TgBotClass.app.handlers)
update_item = Update.de_json(data=req, bot=TgBotClass.app.bot)
# TgBotClass.my_queue.put(update_item)
await TgBotClass.app.process_update(update_item)
# logger.info(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"})