This commit is contained in:
Alexander Kalinovsky
2025-01-04 12:00:12 +01:00
commit 6dbe0536ca
94 changed files with 3467 additions and 0 deletions

40
lifespan.py Normal file
View File

@@ -0,0 +1,40 @@
from contextlib import asynccontextmanager
from .main import QBotApp
@asynccontextmanager
async def default_lifespan(app: QBotApp):
app.logger.debug("starting qbot app")
if app.config.USE_NGROK:
try:
from pyngrok import ngrok
from pyngrok.conf import PyngrokConfig
except ImportError:
app.logger.error("pyngrok is not installed")
raise
tunnel = ngrok.connect(app.config.API_PORT, pyngrok_config = PyngrokConfig(auth_token = app.config.NGROK_AUTH_TOKEN))
app.config.NGROK_URL = tunnel.public_url
await app.bot.set_webhook(url = f"{app.config.API_URL}/api/telegram/webhook",
drop_pending_updates = True,
allowed_updates = ['message', 'callback_query', 'pre_checkout_query'],
secret_token = app.bot_auth_token)
app.logger.info("qbot app started")
if app.lifespan:
async with app.lifespan(app):
yield
else:
yield
app.logger.info("stopping qbot app")
await app.bot.delete_webhook()
if app.config.USE_NGROK:
ngrok.disconnect(app.config.NGROK_URL)
ngrok.kill()
app.logger.info("qbot app stopped")