mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-12-15 16:10:39 +03:00
update provider chatBase (#48)
* add get_bot iterator * get_bot is separate application * Update provider chatBase
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import asyncio
|
||||
import os
|
||||
from asyncio import Queue, sleep
|
||||
from contextlib import asynccontextmanager
|
||||
from dataclasses import dataclass
|
||||
from functools import cached_property
|
||||
from http import HTTPStatus
|
||||
from typing import Any
|
||||
from typing import Any, AsyncGenerator
|
||||
|
||||
from fastapi import Response
|
||||
from loguru import logger
|
||||
@@ -18,12 +19,12 @@ class BotApplication:
|
||||
def __init__(
|
||||
self,
|
||||
settings: AppSettings,
|
||||
handlers: list[Any],
|
||||
handlers: list[Any] | None = None,
|
||||
) -> None:
|
||||
self.application: Application = ( # type: ignore[type-arg]
|
||||
Application.builder().token(token=settings.TELEGRAM_API_TOKEN).build()
|
||||
)
|
||||
self.handlers = handlers
|
||||
self.handlers = handlers or []
|
||||
self.settings = settings
|
||||
self.start_with_webhook = settings.START_WITH_WEBHOOK
|
||||
self._add_handlers()
|
||||
@@ -52,7 +53,11 @@ class BotApplication:
|
||||
logger.info("bot started in polling mode")
|
||||
|
||||
async def shutdown(self) -> None:
|
||||
await self.application.updater.shutdown() # type: ignore
|
||||
await asyncio.gather(
|
||||
self.delete_webhook(),
|
||||
self.application.updater.shutdown(), # type: ignore[union-attr]
|
||||
)
|
||||
logger.info("the bot is turned off")
|
||||
|
||||
@cached_property
|
||||
def webhook_url(self) -> str:
|
||||
@@ -80,3 +85,12 @@ class BotQueue:
|
||||
update = await self.queue.get()
|
||||
asyncio.create_task(self.bot_app.application.process_update(update))
|
||||
await sleep(0)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def get_bot(token: str) -> AsyncGenerator[Bot, None]:
|
||||
app = Application.builder().token(token=token).build()
|
||||
try:
|
||||
yield app.bot
|
||||
finally:
|
||||
await app.shutdown()
|
||||
|
||||
@@ -38,7 +38,7 @@ class Application:
|
||||
self.app.on_event("shutdown")(shutdown(self.app))
|
||||
|
||||
self.app.include_router(api_router)
|
||||
self.configure_hooks()
|
||||
self.configure_bot_hooks()
|
||||
configure_logging(
|
||||
level=LogLevelEnum.INFO,
|
||||
enable_json_logs=settings.ENABLE_JSON_LOGS,
|
||||
@@ -62,7 +62,7 @@ class Application:
|
||||
def bot_queue(self) -> BotQueue:
|
||||
return self._bot_queue
|
||||
|
||||
def configure_hooks(self) -> None:
|
||||
def configure_bot_hooks(self) -> None:
|
||||
if self.bot_app.start_with_webhook:
|
||||
self.app.add_event_handler("startup", self._bot_start_up)
|
||||
else:
|
||||
@@ -76,7 +76,7 @@ class Application:
|
||||
loop.create_task(self.app.state.queue.get_updates_from_queue())
|
||||
|
||||
async def _bot_shutdown(self) -> None:
|
||||
await asyncio.gather(self.bot_app.delete_webhook(), self.bot_app.shutdown())
|
||||
await self.bot_app.shutdown()
|
||||
|
||||
|
||||
def create_app(settings: AppSettings | None = None) -> FastAPI:
|
||||
|
||||
@@ -254,7 +254,14 @@ async def test_ask_question_action(
|
||||
await main_application.bot_app.application.process_update(
|
||||
update=Update.de_json(data=bot_update, bot=main_application.bot_app.bot)
|
||||
)
|
||||
assert_that(mocked_send_message.call_args.kwargs).is_equal_to(
|
||||
assert_that(mocked_send_message.call_args_list[0].kwargs).is_equal_to(
|
||||
{
|
||||
"text": "Пожалуйста, подождите, ответ в среднем занимает 10-15 секунд",
|
||||
"chat_id": bot_update["message"]["chat"]["id"],
|
||||
},
|
||||
include=["text", "chat_id"],
|
||||
)
|
||||
assert_that(mocked_send_message.call_args_list[1].kwargs).is_equal_to(
|
||||
{
|
||||
"text": "Привет! Как я могу помочь вам сегодня?",
|
||||
"chat_id": bot_update["message"]["chat"]["id"],
|
||||
|
||||
Reference in New Issue
Block a user