mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2026-02-04 16:50:38 +03:00
add queue tests
This commit is contained in:
@@ -19,10 +19,11 @@ async def healthcheck() -> ORJSONResponse:
|
||||
|
||||
|
||||
@router.post(
|
||||
f"/{settings.bot_webhook_url}",
|
||||
f"/{settings.TELEGRAM_API_TOKEN}",
|
||||
name="system:process_bot_updates",
|
||||
status_code=status.HTTP_202_ACCEPTED,
|
||||
summary="process bot updates",
|
||||
include_in_schema=False,
|
||||
)
|
||||
async def process_bot_updates(request: Request) -> ORJSONResponse:
|
||||
await request.app.state.queue.put_updates_on_queue(request)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
from asyncio import Queue, sleep
|
||||
from dataclasses import dataclass
|
||||
from functools import cached_property
|
||||
@@ -10,18 +9,17 @@ from fastapi import Request, Response
|
||||
from telegram import Update
|
||||
from telegram.ext import Application, CommandHandler, ContextTypes
|
||||
|
||||
from app.constants import API_PREFIX
|
||||
from settings.config import Settings
|
||||
from settings.config import AppSettings
|
||||
|
||||
|
||||
class BotApplication:
|
||||
def __init__(self, settings: Settings, start_with_webhook: bool = False) -> None:
|
||||
def __init__(self, settings: AppSettings) -> None:
|
||||
self.application: Application = ( # type: ignore
|
||||
Application.builder().token(token=settings.TELEGRAM_API_TOKEN).build()
|
||||
)
|
||||
self.add_handlers()
|
||||
self.settings = settings
|
||||
self.start_with_webhook = start_with_webhook
|
||||
self.start_with_webhook = settings.START_WITH_WEBHOOK
|
||||
|
||||
async def set_webhook(self) -> None:
|
||||
await self.application.initialize()
|
||||
@@ -39,11 +37,7 @@ class BotApplication:
|
||||
await update.message.reply_text(
|
||||
"Help!",
|
||||
disable_notification=True,
|
||||
api_kwargs={
|
||||
"text": "Hello World",
|
||||
"date": int(time.time()) + 30,
|
||||
"schedule_date": int(time.time()) + 30,
|
||||
},
|
||||
api_kwargs={"text": "Hello World"},
|
||||
)
|
||||
return None
|
||||
|
||||
@@ -60,25 +54,20 @@ class BotApplication:
|
||||
|
||||
@cached_property
|
||||
def webhook_url(self) -> str:
|
||||
return os.path.join(
|
||||
self.settings.WEBHOOK_HOST.strip("/"),
|
||||
API_PREFIX.strip("/"),
|
||||
self.settings.URL_PREFIX.strip("/"),
|
||||
self.settings.TELEGRAM_API_TOKEN.strip("/"),
|
||||
)
|
||||
return os.path.join(self.settings.DOMAIN.strip("/"), self.settings.bot_webhook_url.strip("/"))
|
||||
|
||||
|
||||
@dataclass
|
||||
class BotQueue:
|
||||
bot_app: Application # type: ignore[type-arg]
|
||||
bot_app: BotApplication
|
||||
queue: Queue = asyncio.Queue() # type: ignore[type-arg]
|
||||
|
||||
async def put_updates_on_queue(self, request: Request) -> Response:
|
||||
"""
|
||||
Listen {URL_PREFIX}/{TELEGRAM_WEB_TOKEN} path and proxy post request to bot
|
||||
Listen /{URL_PREFIX}/{API_PREFIX}/{TELEGRAM_WEB_TOKEN} path and proxy post request to bot
|
||||
"""
|
||||
data = await request.json()
|
||||
tg_update = Update.de_json(data=data, bot=self.bot_app.bot)
|
||||
tg_update = Update.de_json(data=data, bot=self.bot_app.application.bot)
|
||||
self.queue.put_nowait(tg_update)
|
||||
|
||||
return Response(status_code=HTTPStatus.ACCEPTED)
|
||||
@@ -86,6 +75,5 @@ class BotQueue:
|
||||
async def get_updates_from_queue(self) -> None:
|
||||
while True:
|
||||
update = await self.queue.get()
|
||||
print(update)
|
||||
await self.bot_app.process_update(update)
|
||||
await self.bot_app.application.process_update(update)
|
||||
await sleep(0)
|
||||
|
||||
20
app/main.py
20
app/main.py
@@ -8,7 +8,7 @@ from loguru import logger
|
||||
|
||||
from app.core.bot import BotApplication, BotQueue
|
||||
from app.routers import api_router
|
||||
from settings.config import Settings, get_settings
|
||||
from settings.config import AppSettings, get_settings
|
||||
|
||||
logger.remove()
|
||||
logger.add(
|
||||
@@ -20,18 +20,18 @@ logger.add(
|
||||
|
||||
|
||||
class Application:
|
||||
def __init__(self, settings: Settings, bot_app: BotApplication) -> None:
|
||||
def __init__(self, settings: AppSettings, bot_app: BotApplication) -> None:
|
||||
self.app = FastAPI(
|
||||
title="Health check bot",
|
||||
description="Bot which check all services are working",
|
||||
title="Chat gpt bot",
|
||||
description="Bot for proxy to chat gpt in telegram",
|
||||
version="0.0.3",
|
||||
docs_url=f"{settings.URL_PREFIX}/docs",
|
||||
redoc_url=f"{settings.URL_PREFIX}/redocs",
|
||||
openapi_url=f"{settings.URL_PREFIX}/api/openapi.json",
|
||||
docs_url="/" + "/".join([settings.api_prefix.strip("/"), "docs"]),
|
||||
redoc_url="/" + "/".join([settings.api_prefix.strip("/"), "redocs"]),
|
||||
openapi_url="/" + "/".join([settings.api_prefix.strip("/"), "openapi.json"]),
|
||||
default_response_class=UJSONResponse,
|
||||
)
|
||||
self.app.state.settings = settings
|
||||
self.app.state.queue = BotQueue(bot_app=bot_app.application)
|
||||
self.app.state.queue = BotQueue(bot_app=bot_app)
|
||||
self.bot_app = bot_app
|
||||
|
||||
self.app.include_router(api_router)
|
||||
@@ -58,9 +58,9 @@ class Application:
|
||||
await asyncio.gather(self.bot_app.delete_webhook(), self.bot_app.shutdown())
|
||||
|
||||
|
||||
def create_app(settings: Settings | None = None) -> FastAPI:
|
||||
def create_app(settings: AppSettings | None = None) -> FastAPI:
|
||||
settings = settings or get_settings()
|
||||
bot_app = BotApplication(settings=settings, start_with_webhook=settings.START_WITH_WEBHOOK)
|
||||
bot_app = BotApplication(settings=settings)
|
||||
|
||||
return Application(settings=settings, bot_app=bot_app).fastapi_app
|
||||
|
||||
|
||||
@@ -2,9 +2,14 @@ from fastapi import APIRouter
|
||||
from fastapi.responses import ORJSONResponse
|
||||
|
||||
from app.api.system.controllers import router as system_router
|
||||
from app.constants import API_PREFIX
|
||||
from settings.config import get_settings
|
||||
|
||||
api_router = APIRouter(prefix=API_PREFIX, default_response_class=ORJSONResponse)
|
||||
settings = get_settings()
|
||||
|
||||
api_router = APIRouter(
|
||||
prefix=settings.api_prefix,
|
||||
default_response_class=ORJSONResponse,
|
||||
)
|
||||
|
||||
|
||||
api_router.include_router(system_router, tags=["system"])
|
||||
|
||||
Reference in New Issue
Block a user