mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-07-11 14:27:34 +08:00
145 lines
5.0 KiB
Python
145 lines
5.0 KiB
Python
"""发送消息工具"""
|
|
|
|
from typing import Optional, Type
|
|
|
|
from pydantic import BaseModel, Field, model_validator
|
|
|
|
from app.agent.tools.base import MoviePilotTool
|
|
from app.agent.tools.tags import ToolTag
|
|
from app.log import logger
|
|
from app.schemas import Notification
|
|
from app.schemas.types import NotificationType
|
|
|
|
SEND_MESSAGE_PARSE_MODE_MARKDOWN = "MarkdownV2"
|
|
SEND_MESSAGE_PARSE_MODE_HTML = "HTML"
|
|
SEND_MESSAGE_PARSE_MODE_ALIASES = {
|
|
"markdownv2": SEND_MESSAGE_PARSE_MODE_MARKDOWN,
|
|
"mdv2": SEND_MESSAGE_PARSE_MODE_MARKDOWN,
|
|
"html": SEND_MESSAGE_PARSE_MODE_HTML,
|
|
}
|
|
|
|
|
|
class SendMessageInput(BaseModel):
|
|
"""发送消息工具的输入参数模型"""
|
|
|
|
explanation: Optional[str] = Field(
|
|
None,
|
|
description="Clear explanation of why this tool is being used in the current context",
|
|
)
|
|
message: Optional[str] = Field(
|
|
None,
|
|
description="The message content to send to the user (should be clear and informative)",
|
|
)
|
|
title: Optional[str] = Field(
|
|
None,
|
|
description="Title of the message, a short summary of the message content",
|
|
)
|
|
image_url: Optional[str] = Field(
|
|
None,
|
|
description="Optional image URL to send together with the message on channels that support images (such as Telegram and Slack)",
|
|
)
|
|
parse_mode: Optional[str] = Field(
|
|
None,
|
|
description=(
|
|
"Optional Telegram message body format. Supported values: HTML or MarkdownV2. "
|
|
"Leave empty for default."
|
|
),
|
|
)
|
|
|
|
@model_validator(mode="after")
|
|
def validate_payload(self) -> "SendMessageInput":
|
|
"""校验消息内容和可选格式参数。"""
|
|
if not self.message and not self.title and not self.image_url:
|
|
raise ValueError("message、title、image_url 至少需要提供一个")
|
|
self.parse_mode = SendMessageTool.normalize_parse_mode(self.parse_mode)
|
|
return self
|
|
|
|
|
|
class SendMessageTool(MoviePilotTool):
|
|
"""发送普通通知消息给当前用户。"""
|
|
|
|
name: str = "send_message"
|
|
tags: list[str] = [
|
|
ToolTag.Write,
|
|
ToolTag.Message,
|
|
ToolTag.Admin,
|
|
]
|
|
sends_message: bool = True
|
|
description: str = (
|
|
"Send notification message to the user through configured notification channels "
|
|
"(Telegram, Slack, WeChat, etc.). Supports optional image_url on channels that can "
|
|
"send images. For Telegram, the optional parse_mode parameter controls message body "
|
|
"rendering. Supported values are HTML and MarkdownV2; leave it empty for default."
|
|
)
|
|
args_schema: Type[BaseModel] = SendMessageInput
|
|
require_admin: bool = True
|
|
|
|
@staticmethod
|
|
def normalize_parse_mode(parse_mode: Optional[str]) -> Optional[str]:
|
|
"""
|
|
规范化 send_message 支持的 Telegram 格式参数。
|
|
"""
|
|
if not parse_mode:
|
|
return None
|
|
normalized = SEND_MESSAGE_PARSE_MODE_ALIASES.get(str(parse_mode).strip().lower())
|
|
if not normalized:
|
|
raise ValueError("parse_mode 仅支持 MarkdownV2 或 HTML")
|
|
return normalized
|
|
|
|
def get_tool_message(self, **kwargs) -> Optional[str]:
|
|
"""根据消息参数生成友好的提示消息"""
|
|
message = kwargs.get("message", "") or ""
|
|
title = kwargs.get("title") or ""
|
|
image_url = kwargs.get("image_url")
|
|
|
|
# 截断过长的消息
|
|
if len(message) > 50:
|
|
message = message[:50] + "..."
|
|
|
|
if title and image_url:
|
|
return f"发送图文消息: [{title}] {message}"
|
|
if title:
|
|
return f"发送消息: [{title}] {message}"
|
|
if image_url:
|
|
return f"发送图片消息: {message}"
|
|
return f"发送消息: {message}"
|
|
|
|
async def run(
|
|
self,
|
|
message: Optional[str] = None,
|
|
title: Optional[str] = None,
|
|
image_url: Optional[str] = None,
|
|
parse_mode: Optional[str] = None,
|
|
**kwargs,
|
|
) -> str:
|
|
"""发送消息到当前会话渠道。"""
|
|
title = title or ("图片" if image_url and not message else "")
|
|
text = message or ""
|
|
try:
|
|
parse_mode = self.normalize_parse_mode(parse_mode)
|
|
except ValueError as e:
|
|
return str(e)
|
|
|
|
logger.info(
|
|
f"执行工具: {self.name}, 参数: title={title}, message={text}, "
|
|
f"image_url={image_url}, parse_mode={parse_mode}"
|
|
)
|
|
try:
|
|
await self.send_notification_message(
|
|
Notification(
|
|
channel=self._channel,
|
|
source=self._source,
|
|
mtype=NotificationType.Other,
|
|
userid=self._user_id,
|
|
username=self._username,
|
|
title=title,
|
|
text=text,
|
|
image=image_url,
|
|
parse_mode=parse_mode,
|
|
)
|
|
)
|
|
return "消息已发送"
|
|
except Exception as e:
|
|
logger.error(f"发送消息失败: {e}")
|
|
return f"发送消息时发生错误: {str(e)}"
|