功能:通过 HTTP API 实现 Danding_QqPush 插件,用于 QQ 群通知
- 增加了通过外部 HTTP API 向 QQ 群组发送消息的核心功能。 - 实现了对长文本消息的图片渲染,以避免被认定为垃圾信息。 - 支持在消息中提及特定的 QQ 用户。 - 创建了用于 API 令牌和图片渲染设置的配置选项。 - 开发了一个测试脚本以验证 API 功能。 - 对现有代码进行了重构,以提高组织性和可维护性。
This commit is contained in:
69
danding_bot/plugins/danding_qqpush/text_parser.py
Normal file
69
danding_bot/plugins/danding_qqpush/text_parser.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""文本处理模块 - 处理文本换行和格式化"""
|
||||
from typing import List
|
||||
|
||||
|
||||
class TextParser:
|
||||
"""文本解析器"""
|
||||
|
||||
def __init__(self, max_length: int = 2000):
|
||||
"""
|
||||
初始化文本解析器
|
||||
|
||||
Args:
|
||||
max_length: 最大文本长度,超过将被截断
|
||||
"""
|
||||
self.max_length = max_length
|
||||
|
||||
def parse(self, text: str) -> str:
|
||||
"""
|
||||
解析文本,将 # 转换为换行符
|
||||
|
||||
Args:
|
||||
text: 原始文本,使用 # 表示换行
|
||||
|
||||
Returns:
|
||||
处理后的文本
|
||||
"""
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
# 截断超长文本
|
||||
if len(text) > self.max_length:
|
||||
text = text[:self.max_length]
|
||||
text += "...(文本过长已截断)"
|
||||
|
||||
# 将 # 替换为换行符
|
||||
parsed_text = text.replace("#", "\n")
|
||||
|
||||
return parsed_text
|
||||
|
||||
def parse_to_lines(self, text: str) -> List[str]:
|
||||
"""
|
||||
解析文本并返回行列表
|
||||
|
||||
Args:
|
||||
text: 原始文本
|
||||
|
||||
Returns:
|
||||
文本行列表
|
||||
"""
|
||||
parsed_text = self.parse(text)
|
||||
return parsed_text.split("\n")
|
||||
|
||||
def validate_text(self, text: str) -> bool:
|
||||
"""
|
||||
验证文本是否有效
|
||||
|
||||
Args:
|
||||
text: 待验证的文本
|
||||
|
||||
Returns:
|
||||
是否有效
|
||||
"""
|
||||
if not text or not isinstance(text, str):
|
||||
return False
|
||||
|
||||
if len(text.strip()) == 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
Reference in New Issue
Block a user