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

65 lines
1.7 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
from .constants import (
ENDPOINT_CHANNEL_GET_INFO,
ENDPOINT_CHANNEL_GET_MEMBERS,
ENDPOINT_CHANNEL_LIST,
)
from .utils import create_http_client, flock_api_call
logger = logging.getLogger(__name__)
async def list_channels(
bot_token: str,
) -> list[dict]:
client = create_http_client()
try:
result = await flock_api_call(client, ENDPOINT_CHANNEL_LIST, bot_token, {})
channels = result.get("channels", [])
logger.info("Flock listed %d channels", len(channels))
return channels
except Exception as e:
logger.error("Flock list_channels failed: %s", e)
return []
finally:
await client.aclose()
async def get_channel_info(
channel_id: str,
bot_token: str,
) -> dict | None:
client = create_http_client()
try:
result = await flock_api_call(
client, ENDPOINT_CHANNEL_GET_INFO, bot_token, {"channelId": channel_id}
)
return result
except Exception as e:
logger.error("Flock get_channel_info failed for %s: %s", channel_id, e)
return None
finally:
await client.aclose()
async def get_channel_members(
channel_id: str,
bot_token: str,
) -> list[dict]:
client = create_http_client()
try:
result = await flock_api_call(
client, ENDPOINT_CHANNEL_GET_MEMBERS, bot_token, {"channelId": channel_id}
)
members = result.get("members", [])
logger.info("Flock got %d members for channel %s", len(members), channel_id)
return members
except Exception as e:
logger.error("Flock get_channel_members failed for %s: %s", channel_id, e)
return []
finally:
await client.aclose()