diff --git a/src/fastapi_poe/__init__.py b/src/fastapi_poe/__init__.py index dcf7b67..cd0e5a3 100644 --- a/src/fastapi_poe/__init__.py +++ b/src/fastapi_poe/__init__.py @@ -5,7 +5,6 @@ "stream_request", "get_bot_response", "get_final_response", - "sync_bot_settings", "BotError", "BotErrorNoRetry", "Attachment", diff --git a/src/fastapi_poe/base.py b/src/fastapi_poe/base.py index 3ca4bc8..79d77d8 100644 --- a/src/fastapi_poe/base.py +++ b/src/fastapi_poe/base.py @@ -29,6 +29,7 @@ from starlette.types import Message from typing_extensions import deprecated, overload +from fastapi_poe.client import sync_bot_settings from fastapi_poe.templates import ( IMAGE_VISION_ATTACHMENT_TEMPLATE, TEXT_ATTACHMENT_TEMPLATE, @@ -88,7 +89,7 @@ async def dispatch( logger.info(f"Response status: {response.status_code}") try: if hasattr(response, "body"): - body = json.loads(response.body.decode()) + body = json.loads(bytes(response.body).decode()) logger.debug(f"Response body: {json.dumps(body)}") except json.JSONDecodeError: logger.error("Response body: Unable to parse JSON") @@ -131,6 +132,7 @@ class PoeBot: """ path: str = "/" # Path where this bot will be exposed + bot_name: Optional[str] = None # Name of the bot using this PoeBot instance in Poe access_key: Optional[str] = None # Access key for this bot should_insert_attachment_messages: bool = ( True # Whether to insert attachment messages into the conversation @@ -933,6 +935,28 @@ def make_app( if bot_obj.access_key is None and not allow_without_key: raise ValueError(f"Missing access key on {bot_obj}") _add_routes_for_bot(app, bot_obj) + if not bot_obj.bot_name or not bot_obj.access_key: + logger.warning("\n************* Warning *************") + logger.warning( + "Bot name or access key is not set for PoeBot.\n" + "Bot settings will NOT be synced automatically on server start/update." + "Please remember to sync bot settings manually.\n\n" + "For more information, see: https://creator.poe.com/docs/server-bots-functional-guides#updating-bot-settings" + ) + logger.warning("\n************* Warning *************") + else: + try: + sync_bot_settings(bot_obj.bot_name, bot_obj.access_key) + except Exception as e: + logger.error("\n*********** Error ***********") + logger.error( + f"Bot settings sync failed for {bot_obj.bot_name}: /n{e}/n/n" + ) + logger.error("Please sync bot settings manually./n/n") + logger.error( + "For more information, see: https://creator.poe.com/docs/server-bots-functional-guides#updating-bot-settings" + ) + logger.error("\n*********** Error ***********") # Uncomment this line to print out request and response # app.add_middleware(LoggingMiddleware) diff --git a/src/fastapi_poe/client.py b/src/fastapi_poe/client.py index 1e3e5b5..5aeafeb 100644 --- a/src/fastapi_poe/client.py +++ b/src/fastapi_poe/client.py @@ -620,5 +620,14 @@ def sync_bot_settings( base_url: str = "https://api.poe.com/bot/fetch_settings/", ) -> None: """Sync bot settings with the Poe server using bot name and its Access Key.""" - response = httpx.post(f"{base_url}{bot_name}/{access_key}/{PROTOCOL_VERSION}") + try: + response = httpx.post(f"{base_url}{bot_name}/{access_key}/{PROTOCOL_VERSION}") + if response.status_code != 200: + raise BotError( + f"Error fetching settings for bot {bot_name}: {response.text}" + ) + except httpx.ReadTimeout as e: + raise BotError( + f"Timeout fetching settings for bot {bot_name}. Try sync manually later." + ) from e print(response.text)