fix(tests): stabilize messaging shutdown (#5979)

This commit is contained in:
InfinityPacer
2026-06-21 07:37:47 +08:00
committed by GitHub
parent b395d820d8
commit e02cebe16c
15 changed files with 275 additions and 40 deletions

View File

@@ -264,6 +264,11 @@ class Feishu:
@staticmethod
async def _disconnect_ws_client_quietly(ws_client: lark.ws.Client) -> None:
"""静默关闭飞书 WebSocket避免 SDK 在关机时打印带敏感参数的连接地址。"""
if ws_client._conn is None:
ws_client._conn_url = ""
ws_client._conn_id = ""
ws_client._service_id = ""
return
await ws_client._lock.acquire()
try:
if ws_client._conn is not None:

View File

@@ -1,6 +1,6 @@
import threading
from pyparsing import Forward, Literal, Word, alphas, infixNotation, opAssoc, alphanums, Combine, nums, ParseResults
from pyparsing import Forward, Literal, Word, alphas, infix_notation, opAssoc, alphanums, Combine, nums, ParseResults
from app.utils import rust_accel
@@ -21,16 +21,16 @@ class RuleParser:
# 原子
atom: Combine = Combine(Word(alphas, alphanums) | (Word(nums) + Word(alphas, alphanums)))
# 逻辑非操作符
operator_not: Literal = Literal('!').setParseAction(lambda t: 'not')
operator_not: Literal = Literal('!').set_parse_action(lambda t: 'not')
# 逻辑或操作符
operator_or: Literal = Literal('|').setParseAction(lambda t: 'or')
operator_or: Literal = Literal('|').set_parse_action(lambda t: 'or')
# 逻辑与操作符
operator_and: Literal = Literal('&').setParseAction(lambda t: 'and')
operator_and: Literal = Literal('&').set_parse_action(lambda t: 'and')
# 定义表达式的语法规则
expr <<= (operator_not + expr) | atom | ('(' + expr + ')')
# 运算符优先级
self.expr = infixNotation(expr,
self.expr = infix_notation(expr,
[(operator_not, 1, opAssoc.RIGHT),
(operator_and, 2, opAssoc.LEFT),
(operator_or, 2, opAssoc.LEFT)])
@@ -53,7 +53,7 @@ class RuleParser:
rust_result = rust_accel.parse_filter_rule(expression)
if rust_result is not None:
return _RustParseResults(rust_result)
return self.expr.parseString(expression)
return self.expr.parse_string(expression)
class _RustParseResults(list):

View File

@@ -14,8 +14,8 @@ from telebot.types import (
InlineKeyboardButton,
InputMediaPhoto,
)
from telegramify_markdown import standardize, telegramify # noqa
from telegramify_markdown.type import ContentTypes, SentType
from telegramify_markdown import entities_to_markdownv2, standardize, telegramify # noqa
from telegramify_markdown.content import ContentTypes, File, Photo, Text
from app.core.config import settings
from app.core.context import MediaInfo, Context
@@ -242,6 +242,18 @@ class Telegram:
logger.error(f"下载Telegram文件失败: {e}")
return None
@staticmethod
def _telegramify_item_text(item: Text) -> str:
"""将 telegramify 文本片段转换为 Telegram MarkdownV2 字符串。"""
return entities_to_markdownv2(item.text, item.entities)
@staticmethod
def _telegramify_item_caption(item: Text | File | Photo) -> str:
"""将 telegramify 文本或媒体片段转换为 Telegram MarkdownV2 caption。"""
if isinstance(item, Text):
return Telegram._telegramify_item_text(item)
return entities_to_markdownv2(item.caption_text, item.caption_entities)
@staticmethod
def _serialize_update_payload(message: Any) -> Optional[dict]:
"""
@@ -1138,7 +1150,7 @@ class Telegram:
reply_markup = kwargs.pop("reply_markup", None)
boxs: SentType = (
boxs: list[Text | File | Photo] = (
ThreadHelper()
.submit(lambda x: asyncio.run(telegramify(x)), caption)
.result()
@@ -1158,7 +1170,9 @@ class Telegram:
if disable_web_page_preview is not None:
msg_kwargs["disable_web_page_preview"] = disable_web_page_preview
ret = self._bot.send_message(
**msg_kwargs, text=item.content, reply_markup=current_reply_markup
**msg_kwargs,
text=self._telegramify_item_text(item),
reply_markup=current_reply_markup,
)
elif item.content_type == ContentTypes.PHOTO or (image and i == 0):
@@ -1168,7 +1182,7 @@ class Telegram:
getattr(item, "file_name", ""),
getattr(item, "file_data", image),
),
caption=getattr(item, "caption", item.content),
caption=self._telegramify_item_caption(item),
reply_markup=current_reply_markup,
)
@@ -1176,7 +1190,7 @@ class Telegram:
ret = self._bot.send_document(
**kwargs,
document=(item.file_name, item.file_data),
caption=item.caption,
caption=self._telegramify_item_caption(item),
reply_markup=current_reply_markup,
)