ForcePilot/backend/package/yuxi/channel/extensions/flock/fetch.py

46 lines
1.3 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
from .config import _apply_env_overrides, _dict_to_account
from .constants import ENDPOINT_FETCH_MESSAGES
from .utils import create_http_client, flock_api_call
logger = logging.getLogger(__name__)
async def fetch_messages(
target_id: str,
*,
count: int = 10,
before: str | None = None,
account_id: str = "default",
config: dict,
) -> list[dict]:
account_data = config.get("accounts", {}).get(account_id, {})
account = _dict_to_account(account_data)
account = _apply_env_overrides(account)
if not account.bot_token:
logger.warning("Flock fetch_messages: bot_token not configured for account %s", account_id)
return []
payload: dict = {
"to": target_id,
"count": count,
}
if before:
payload["before"] = before
client = create_http_client()
try:
result = await flock_api_call(client, ENDPOINT_FETCH_MESSAGES, account.bot_token, payload)
messages = result.get("messages", [])
logger.info("Flock fetched %d messages for %s", len(messages), target_id)
return messages
except Exception as e:
logger.error("Flock fetch_messages failed for %s: %s", target_id, e)
return []
finally:
await client.aclose()