refactor(oa&dingtalk): 重构审批推送逻辑,改用块格式Markdown并支持分批推送

1.  统一请假、加班审批的推送实现,新增`_format_leave_block`/`_format_overtime_block`格式化审批详情为标准Markdown块
2.  重构`build_push_summary`生成分组推送数据,不再拆分单条消息,直接返回带标题和块列表的分组结构
3.  升级钉钉机器人推送模块,支持块格式分组自动分批推送,兼容原有纯文本推送逻辑
4.  临时关闭自动审批开关`APPROVE_ENABLED`,适配影刀运行环境
5.  重构影刀端钉钉推送指令入口,支持传入块格式分组数据并自动处理分批
This commit is contained in:
2026-08-21 23:45:16 +08:00
parent bfef048695
commit ce0422979a
5 changed files with 514 additions and 446 deletions
+175 -18
View File
@@ -282,34 +282,43 @@ class DingTalkRobot:
is_at_all: bool = False,
server_name: str = "",
append_footer: bool = True,
raw: bool = False,
) -> bool:
"""
统一通知方法(推荐业务方使用),样式与 aa.md 中 DingTalkBot.send_notification 一致:
统一通知方法(推荐业务方使用),默认固定格式(代码块包裹),支持自定义 Markdown 格式:
默认格式(raw=False):
**<font color='#ff0000'>任务执行失败</font>** ← 标题按 status 着色
内容详情:
```text
订单同步失败 ← 内容整体用 ```text 代码块包裹
错误原因:接口超时 (按纯文本渲染,不再做 Markdown 解析)
```
---
###### 小芒 任务调度平台 | 2026-08-14 14:30:00
###### 小芒 任务调度平台 | 2026-08-14 14:30:00 ← 页脚:署名 + 当前时间
说明:内容放入代码块可避免钉钉把内容里的 '====' 等分隔线当作标题下划线(setext 语法),
否则该行之前的所有内容会被渲染成超大加粗标题。
自定义格式(raw=True):
**<font color='#ff0000'>请假审批</font>** ← 标题按 status 着色
# 【请假审批】需人工复核 共2条 ← 内容直接渲染 Markdown
---
> **【单据1】** ← 引用块、加粗、超链接等均可渲染
> 姓名:张三
> [查看详情](https://...)
---
###### 小芒 任务调度平台 | 2026-08-14 14:30:00
Args:
title: 消息标题
content: 消息主体内容,支持多行(整体用 ```text 代码块包裹)
content: 消息主体内容
status: 状态类型,决定标题颜色: info(蓝) | success(绿) | warning(橙) | error(红)
at_mobiles: 需要 @ 的手机号列表
at_user_ids: 需要 @ 的用户 userid 列表
is_at_all: 是否 @所有人
server_name: 页脚署名,缺省用实例的 server_name
append_footer: 是否自动追加页脚(署名 + 时间),默认 True
raw: 是否直接渲染 Markdown(默认 False)。
- False: 内容包裹在 ```text 代码块中,钉钉按纯文本渲染(原行为,向后兼容);
- True: 内容直接作为 Markdown 发送,钉钉会渲染引用块、加粗、超链接、列表等。
Returns:
是否推送成功
@@ -324,13 +333,18 @@ class DingTalkRobot:
color = _STATUS_COLORS.get(status, _STATUS_COLORS["info"])
text = f"**<font color='{color}'>{title}</font>**\n\n"
text += "内容详情:\n"
# 内容整体放入 ```text 代码块,钉钉按纯文本渲染、不再做 Markdown 解析:
# 避免报告里的 '====' 分隔线被当作标题下划线(setext),使该行之前的内容渲染成超大加粗标题
text += "```text\n"
text += content.strip() + "\n"
text += "```\n"
if raw:
# 自定义 Markdown 格式:内容直接渲染
text = f"**<font color='{color}'>{title}</font>**\n\n"
text += content.strip() + "\n"
else:
# 固定格式:内容包裹在 ```text 代码块中
text = f"**<font color='{color}'>{title}</font>**\n\n"
text += "内容详情:\n"
text += "```text\n"
text += content.strip() + "\n"
text += "```\n"
if append_footer:
text += f"\n\n---\n\n{self._build_footer(server_name)}"
@@ -339,10 +353,72 @@ class DingTalkRobot:
if at:
payload["at"] = at
logger.info("钉钉机器人推送通知 [%s]: %s", status, title)
fmt = "Markdown" if raw else "text"
logger.info("钉钉机器人推送通知 [%s/%s]: %s", status, fmt, title)
self._post(payload)
return True
def send_in_batches(
self,
title: str,
content: str,
status: str = "info",
server_name: str = "",
raw: bool = False,
max_chars: int = 15000,
at_mobiles: Optional[List[str]] = None,
at_user_ids: Optional[List[str]] = None,
is_at_all: bool = False,
) -> bool:
"""
分批发送长内容(按行智能拆分,避免切断单行数据)。
当钉钉消息内容超过 max_chars 字符时,自动按行拆成多批发送,
每批带「第 N/M 批」标题前缀,便于接收方识别。
Args:
title: 消息标题
content: 消息正文(长文本)
status: 状态类型,决定标题颜色
server_name: 页脚署名
raw: 是否直接渲染 Markdown
max_chars: 单批最大字符数(默认 15000,钉钉 Markdown 上限约 20000)
at_mobiles: 需要 @ 的手机号列表
at_user_ids: 需要 @ 的用户 userid 列表
is_at_all: 是否 @所有人
Returns:
是否推送成功
"""
lines = content.strip().split('\n')
batches: list = []
current_batch: list = []
current_len = 0
for line in lines:
line_len = len(line) + 1 # +1 for newline
if current_len + line_len > max_chars and current_batch:
batches.append('\n'.join(current_batch))
current_batch = [line]
current_len = line_len
else:
current_batch.append(line)
current_len += line_len
if current_batch:
batches.append('\n'.join(current_batch))
total = len(batches)
for i, batch_content in enumerate(batches, 1):
batch_title = f"{title}(第{i}/{total}批)" if total > 1 else title
logger.info("分批推送 %d/%d: %s", i, total, batch_title)
self.send_notification(
batch_title, batch_content, status=status,
server_name=server_name, raw=raw,
at_mobiles=at_mobiles, at_user_ids=at_user_ids, is_at_all=is_at_all,
)
return True
def send_action_card(
self,
title: str,
@@ -400,6 +476,54 @@ class DingTalkRobot:
self._post(payload)
return True
def send_action_card_in_batches(
self,
title: str,
content_lines: list,
single_title: str,
single_url: str,
server_name: str = "",
batch_size: int = 15,
at_mobiles: Optional[List[str]] = None,
at_user_ids: Optional[List[str]] = None,
is_at_all: bool = False,
) -> bool:
"""分批发送 ActionCard(按条目分组,每批一张卡片)。
Args:
title: 卡片标题(会自动加「第N/M批」后缀)
content_lines: 每条内容的列表,每个元素是一条完整的 Markdown 条目
single_title: 底部按钮文字
single_url: 底部按钮跳转链接
server_name: 页脚署名
batch_size: 每批最多包含的条目数(默认 15)
"""
total = len(content_lines)
num_batches = (total + batch_size - 1) // batch_size
for batch_idx in range(num_batches):
start = batch_idx * batch_size
end = min(start + batch_size, total)
sub_items = content_lines[start:end]
current = batch_idx + 1
md_parts = [f"# {title}"]
md_parts.append(f"第{current}/{num_batches}批(共{total}条)")
md_parts.append("")
for item in sub_items:
md_parts.append("---")
md_parts.append(item)
md_content = "\n".join(md_parts)
batch_title = f"{title} 第{current}/{num_batches}批"
logger.info("ActionCard 分批推送 %d/%d: %s", current, num_batches, batch_title)
self.send_action_card(
batch_title, md_content, single_title, single_url,
server_name=server_name,
at_mobiles=at_mobiles, at_user_ids=at_user_ids, is_at_all=is_at_all,
)
return True
# ==================== 一次性便捷函数 ====================
def send_robot_text(
@@ -425,13 +549,46 @@ def send_robot_notification(
at_user_ids: Optional[List[str]] = None,
is_at_all: bool = False,
server_name: str = "",
raw: bool = False,
batch: bool = False,
max_chars: int = 15000,
mode: str = "markdown",
single_title: str = "查看详情",
single_url: str = "",
batch_size: int = 15,
) -> bool:
"""一键发送统一通知(样式与 send_notification 一致)。"""
"""一键发送统一通知(样式与 send_notification 一致)。
Args:
batch: 是否启用分批发送(默认 False)。True 时调用 send_in_batches。
max_chars: 单批最大字符数,仅在 batch=True 时生效。
mode: 推送模式:
'markdown' - 标准 Markdown 消息(引用块/加粗/超链接)
'action_card' - ActionCard 卡片消息(底部带按钮跳转)
single_title: ActionCard 底部按钮文字(仅 mode='action_card' 时生效)
single_url: ActionCard 底部按钮跳转链接(仅 mode='action_card' 时生效)
batch_size: ActionCard 每批最多包含的条目数(仅 mode='action_card' 时生效)
"""
robot = DingTalkRobot(webhook=webhook, secret=secret)
if mode == "action_card":
lines = [content] if isinstance(content, str) else list(content)
return robot.send_action_card_in_batches(
title, lines, single_title, single_url,
server_name=server_name, batch_size=batch_size,
at_mobiles=at_mobiles, at_user_ids=at_user_ids, is_at_all=is_at_all,
)
if batch:
return robot.send_in_batches(
title, content, status=status,
server_name=server_name, raw=raw, max_chars=max_chars,
at_mobiles=at_mobiles, at_user_ids=at_user_ids, is_at_all=is_at_all,
)
return robot.send_notification(
title, content, status=status,
at_mobiles=at_mobiles, at_user_ids=at_user_ids, is_at_all=is_at_all,
server_name=server_name,
server_name=server_name, raw=raw,
)