from __future__ import annotations import logging from typing import Any from yuxi.channel.extensions.qqbot.format import chunk_markdown_text, md_to_qq_markdown logger = logging.getLogger(__name__) class QQBotDelivery: def __init__(self, outbound: Any, account: Any): self._outbound = outbound self._account = account async def deliver_text( self, target_id: str, text: str, reply_to_id: str | None = None ) -> list[str]: formatted = md_to_qq_markdown(text) if self._account.markdown_support else text chunks = chunk_markdown_text(formatted) sent_ids = [] for i, chunk in enumerate(chunks): await self._outbound.send_text( target_id, chunk, reply_to_id=reply_to_id if i == 0 else None ) return sent_ids async def deliver_media_tags( self, target_id: str, text: str, reply_to_id: str | None = None ) -> tuple[str, list[str]]: from yuxi.channel.extensions.qqbot.media_tags import parse_media_tags plain_text, media_items = parse_media_tags(text) media_ids: list[str] = [] for media_item in media_items: try: result = await self._outbound.send_media( target_id, media_item.url, media_item.tag_type if media_item.tag_type != "qqmedia" else "auto", reply_to_id=reply_to_id, ) if result and result.get("url"): media_ids.append(result["url"]) except Exception: logger.exception("Failed to send media tag: %s", media_item.url) if plain_text.strip(): await self._outbound.send_text(target_id, plain_text, reply_to_id=reply_to_id) return plain_text, media_ids