ForcePilot/backend/package/yuxi/channel/extensions/qqbot/outbound.py
Kris 2ab65f153f feat(channel): 添加 QQ Bot 渠道扩展
新增 QQ Bot 渠道扩展,支持在 Yuxi 平台中集成 QQ 机器人渠道。

包含以下功能模块:
- api_client: QQ API 客户端封装
- api_routes: API 路由管理
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- websocket: WebSocket 实时连接
- credentials: 凭证管理
- token: Token 管理
- outbound: 外发消息管理
- outbound_media: 媒体外发
- streaming: 流式消息处理
- streaming_media: 媒体流处理
- pairing: 用户配对与绑定
- security: 安全校验
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- pipeline: 消息管道
- pipeline_stages: 管道阶段
- commands: 指令处理
- commands_builtin: 内置指令
- interaction: 交互处理
- approval: 审批流程
- ark: ARK 消息
- audio: 音频处理
- media: 媒体资源
- media_chunked: 分块媒体
- media_tags: 媒体标签
- message_queue: 消息队列
- delivery: 消息送达确认
- reconnect: 重连机制
- typing_keepalive: 输入状态保活
- group_activation: 群激活
- group_gating: 群门控
- group_history: 群历史
- known_users: 已知用户
- ref_index: 引用索引
- tools: Agent 工具集成
- types: 类型定义
2026-05-21 11:35:12 +08:00

181 lines
6.9 KiB
Python

from __future__ import annotations
import asyncio
import logging
from typing import Any
from yuxi.channel.extensions.qqbot.api_client import QQBotApiClient
from yuxi.channel.extensions.qqbot.format import chunk_markdown_text, md_to_qq_markdown
from yuxi.channel.extensions.qqbot.types import QQBotAccountConfig, QQBotChatType
logger = logging.getLogger(__name__)
class QQBotOutbound:
def __init__(
self,
api_client: QQBotApiClient,
account: QQBotAccountConfig,
streaming_controller: Any | None = None,
):
self._api_client = api_client
self._account = account
self._streaming_controller = streaming_controller
async def send_text(
self,
target_id: str,
content: str,
reply_to_id: str | None = None,
account_id: str | None = None,
) -> None:
chat_type, target = self._parse_target(target_id)
if not content:
return
message_reference = None
if reply_to_id:
message_reference = {"message_id": reply_to_id, "ignore_get_message_error": True}
if self._account.markdown_support:
formatted = md_to_qq_markdown(content)
chunks = chunk_markdown_text(formatted)
total = len(chunks)
for i, chunk in enumerate(chunks):
if total > 1:
chunk = f"{chunk}\n\n({i + 1}/{total})"
if chat_type == QQBotChatType.C2C:
await self._api_client.send_c2c_markdown(
target, chunk,
msg_id=reply_to_id if i == 0 else None,
message_reference=message_reference if i == total - 1 else None,
)
elif chat_type == QQBotChatType.GUILD:
await self._api_client.send_channel_message(
target, chunk, msg_type=0,
markdown={"content": chunk},
)
else:
await self._api_client.send_group_markdown(
target, chunk,
msg_id=reply_to_id if i == 0 else None,
message_reference=message_reference if i == total - 1 else None,
)
if i < total - 1:
await asyncio.sleep(0.2)
else:
chunks = chunk_markdown_text(content, limit=2000)
total = len(chunks)
for i, chunk in enumerate(chunks):
if total > 1:
chunk = f"{chunk}\n({i + 1}/{total})"
if chat_type == QQBotChatType.C2C:
await self._api_client.send_c2c_message(
target, chunk, msg_type=0,
msg_id=reply_to_id if i == 0 else None,
message_reference=message_reference if i == total - 1 else None,
)
elif chat_type == QQBotChatType.GUILD:
await self._api_client.send_channel_message(
target, chunk, msg_type=0,
)
else:
await self._api_client.send_group_message(
target, chunk, msg_type=0,
msg_id=reply_to_id if i == 0 else None,
message_reference=message_reference if i == total - 1 else None,
)
if i < total - 1:
await asyncio.sleep(0.2)
async def send_c2c_stream(
self,
openid: str,
stream_fn: Any | None = None,
) -> str | None:
if not self._streaming_controller:
return None
return await self._streaming_controller.start_and_stream(openid, stream_fn)
async def send_media(
self,
target_id: str,
media_url: str,
media_type: str,
reply_to_id: str | None = None,
account_id: str | None = None,
) -> None:
from yuxi.channel.extensions.qqbot.outbound_media import QQBotOutboundMedia
from yuxi.channel.extensions.qqbot.media import QQBotMedia
media_manager = QQBotMedia(
api_client=self._api_client,
url_direct_upload=self._account.url_direct_upload,
)
outbound_media = QQBotOutboundMedia(
api_client=self._api_client,
media_manager=media_manager,
)
send_map = {
"image": outbound_media.send_photo,
"voice": outbound_media.send_voice,
"video": outbound_media.send_video,
"file": outbound_media.send_document,
}
send_fn = send_map.get(media_type) or send_map.get("image")
if send_fn is None:
return
await send_fn(target_id, media_url, reply_to_id=reply_to_id)
async def send_input_notify(self, openid: str) -> None:
await self._api_client.send_input_notify(openid)
async def send_keyboard_message(
self,
target_id: str,
content: str,
keyboard: dict,
reply_to_id: str | None = None,
) -> None:
chat_type, target = self._parse_target(target_id)
formatted = md_to_qq_markdown(content)
if chat_type == QQBotChatType.C2C:
await self._api_client.send_c2c_markdown(target, formatted, keyboard=keyboard, msg_id=reply_to_id)
else:
await self._api_client.send_group_markdown(target, formatted, keyboard=keyboard, msg_id=reply_to_id)
async def unsend(self, target_id: str, message_id: str) -> bool:
chat_type, target = self._parse_target(target_id)
try:
if chat_type == QQBotChatType.C2C:
await self._api_client.delete_c2c_message(target, message_id)
elif chat_type == QQBotChatType.GUILD:
await self._api_client.delete_channel_message(target, message_id)
else:
await self._api_client.delete_group_message(target, message_id)
return True
except Exception:
logger.exception("unsend failed: target=%s, msg_id=%s", target_id, message_id)
return False
async def send_ark(self, target_id: str, ark: dict) -> None:
chat_type, target = self._parse_target(target_id)
if chat_type == QQBotChatType.C2C:
await self._api_client.send_c2c_ark(target, ark)
else:
await self._api_client.send_group_ark(target, ark)
def _parse_target(self, target_id: str) -> tuple[QQBotChatType, str]:
parts = target_id.split(":", 2)
if len(parts) >= 3:
prefix = parts[1]
actual_id = parts[2]
if prefix in ("c2c", "dm"):
return (QQBotChatType.C2C, actual_id)
elif prefix == "group":
return (QQBotChatType.GROUP, actual_id)
elif prefix == "channel":
return (QQBotChatType.GUILD, actual_id)
return (QQBotChatType.C2C, target_id)