71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
|
|
import asyncio
|
||
|
|
import logging
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.twitch.client import client_manager_registry
|
||
|
|
from yuxi.channel.extensions.twitch.config import (
|
||
|
|
TwitchConfig,
|
||
|
|
get_account_config,
|
||
|
|
is_account_configured,
|
||
|
|
normalize_twitch_channel,
|
||
|
|
)
|
||
|
|
from yuxi.channel.extensions.twitch.markdown import chunk_text_for_twitch, strip_markdown_for_twitch
|
||
|
|
from yuxi.channel.extensions.twitch.types import TwitchSendResult
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
async def send_message_twitch_internal(
|
||
|
|
channel: str,
|
||
|
|
text: str,
|
||
|
|
cfg: TwitchConfig,
|
||
|
|
account_id: str = "default",
|
||
|
|
strip_markdown: bool = True,
|
||
|
|
reply_to_id: str | None = None,
|
||
|
|
) -> TwitchSendResult:
|
||
|
|
account = get_account_config(cfg, account_id)
|
||
|
|
if not is_account_configured(account):
|
||
|
|
return TwitchSendResult(ok=False, error=f"Account not found: {account_id}")
|
||
|
|
|
||
|
|
normalized_channel = normalize_twitch_channel(channel or account.channel)
|
||
|
|
|
||
|
|
if strip_markdown:
|
||
|
|
text = strip_markdown_for_twitch(text)
|
||
|
|
if not text:
|
||
|
|
return TwitchSendResult(ok=True, message_id="skipped")
|
||
|
|
|
||
|
|
manager = client_manager_registry.get(account_id)
|
||
|
|
if manager is None:
|
||
|
|
return TwitchSendResult(ok=False, error="Please start the Twitch gateway first")
|
||
|
|
|
||
|
|
return await manager.send_message(account, normalized_channel, text, reply_to_id=reply_to_id)
|
||
|
|
|
||
|
|
|
||
|
|
async def deliver_twitch_reply(
|
||
|
|
channel: str,
|
||
|
|
text: str,
|
||
|
|
cfg: TwitchConfig,
|
||
|
|
account_id: str = "default",
|
||
|
|
reply_to_id: str | None = None,
|
||
|
|
):
|
||
|
|
chunks = chunk_text_for_twitch(text, 500)
|
||
|
|
interval = (cfg.send_chunk_interval_ms / 1000) if cfg.send_chunk_interval_ms > 0 else 0
|
||
|
|
for i, chunk in enumerate(chunks):
|
||
|
|
if i > 0 and interval > 0:
|
||
|
|
await asyncio.sleep(interval)
|
||
|
|
result = await send_message_twitch_internal(
|
||
|
|
channel=channel,
|
||
|
|
text=chunk,
|
||
|
|
cfg=cfg,
|
||
|
|
account_id=account_id,
|
||
|
|
strip_markdown=False,
|
||
|
|
reply_to_id=reply_to_id,
|
||
|
|
)
|
||
|
|
if not result.ok:
|
||
|
|
logger.warning(
|
||
|
|
"Twitch send failed for chunk %d/%d: channel=%s error=%s",
|
||
|
|
i + 1,
|
||
|
|
len(chunks),
|
||
|
|
channel,
|
||
|
|
result.error,
|
||
|
|
)
|