Skip to content

Commit

Permalink
Auto sync bot settings on server start
Browse files Browse the repository at this point in the history
Added a new optional field "bot_name" for auto syncing bot settings on bot server start. (When make_app is called)
  • Loading branch information
krisyang1125 committed Aug 16, 2024
1 parent 2d71318 commit 0ad4218
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/fastapi_poe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"stream_request",
"get_bot_response",
"get_final_response",
"sync_bot_settings",
"BotError",
"BotErrorNoRetry",
"Attachment",
Expand Down
24 changes: 24 additions & 0 deletions src/fastapi_poe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion src/fastapi_poe/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0ad4218

Please sign in to comment.