feat(dingtalk): 机器人页脚统一品牌并发布 0.1.4
新增 BRAND 常量,Markdown 页脚固定为「37du 自动化平台 | 时间」,不再重复插件名;同步 api.json 导出清单。
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
能力(与原 ``workflows/share/dingtalk_robot.py`` 及各插件内副本一致):
|
||||
- 加签(HMAC-SHA256 + Base64 + URL 编码)与不加签两种模式;
|
||||
- Markdown 消息:标题按状态着色 + 正文 + 页脚署名/时间;
|
||||
- Markdown 消息:标题按状态着色 + 正文 + 统一页脚(``37du 自动化平台 | 时间``);
|
||||
- 超长内容按行分批(``max_chars``);块格式按条数分批(``batch_size``);
|
||||
- 多环境(prod/dev)webhook 选择,失败自动重试(默认 3 次)。
|
||||
|
||||
@@ -39,6 +39,9 @@ DEFAULT_TITLE = "消息通知"
|
||||
DEFAULT_MAX_CHARS = 15000 # 钉钉 Markdown 上限约 20000
|
||||
DEFAULT_BATCH_SIZE = 10
|
||||
|
||||
#: 统一页脚品牌(全平台一致,不随插件/任务变化;与 plugins/XQ0009、workflows/share 副本对齐)
|
||||
BRAND = "37du 自动化平台"
|
||||
|
||||
|
||||
# ==================== 加签 / URL ====================
|
||||
|
||||
@@ -64,20 +67,26 @@ def build_url(webhook: str, secret: str = "") -> str:
|
||||
|
||||
def render_markdown(title: str, content: str, status: str = "info",
|
||||
server_name: str = "") -> str:
|
||||
"""渲染统一格式的 Markdown 正文(彩色标题 + 正文 + 页脚)。"""
|
||||
"""渲染统一格式的 Markdown 正文(彩色标题 + 正文 + 统一页脚)。
|
||||
|
||||
页脚固定为 ``###### 37du 自动化平台 | 时间``,**不再重复标题/插件名**(标题上方已有);
|
||||
``server_name`` 仅为旧调用方签名兼容保留,不参与渲染。
|
||||
"""
|
||||
color = STATUS_COLORS.get((status or "").strip().lower(), STATUS_COLORS["info"])
|
||||
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
footer = f"\n\n---\n\n###### {(server_name or '').strip()} | {now}"
|
||||
footer = f"\n\n---\n\n###### {BRAND} | {now}"
|
||||
return f"**<font color='{color}'>{title}</font>**\n\n{str(content).strip()}\n{footer}"
|
||||
|
||||
|
||||
def compose_batch_messages(blocks: Sequence[str], title_prefix: str,
|
||||
batch_size: int = DEFAULT_BATCH_SIZE) -> List[str]:
|
||||
"""把 block 列表按 ``batch_size`` 条一批,生成带批次标题的待推送文本列表。
|
||||
"""把 block 列表按 ``batch_size`` 条一批,生成待推送文本列表。
|
||||
|
||||
消息顶部已有彩色标题,正文里**不再重复标题**:单批不加头,多批只标 ``###### 第i/N批``。
|
||||
|
||||
Args:
|
||||
blocks: 每条记录格式化后的 block 字符串列表(业务字段由调用方负责)
|
||||
title_prefix: 批次标题前缀,如 ``【请假审批】通过``
|
||||
title_prefix: 兼容保留(旧版用它当批次标题前缀),不参与渲染
|
||||
batch_size: 每批最大条数(默认 10)
|
||||
|
||||
Returns:
|
||||
@@ -90,7 +99,7 @@ def compose_batch_messages(blocks: Sequence[str], title_prefix: str,
|
||||
out = []
|
||||
for idx in range(total):
|
||||
batch = list(blocks[idx * size:(idx + 1) * size])
|
||||
header = f"###### {title_prefix}(第{idx + 1}/{total}批)\n\n"
|
||||
header = f"###### 第{idx + 1}/{total}批\n\n" if total > 1 else ""
|
||||
# 钉钉中引用块尾行后需空行(双换行)再进入新标题,否则会紧贴渲染
|
||||
footer = f"\n\n###### 本批共{len(batch)}条"
|
||||
out.append(header + "\n\n".join(batch) + footer)
|
||||
@@ -125,7 +134,7 @@ class Robot:
|
||||
Args:
|
||||
webhook: 机器人地址;缺省按 ``env`` 取配置
|
||||
secret: 加签密钥;缺省按 ``env`` 取配置
|
||||
server_name: 页脚署名;缺省按 ``env`` 取配置
|
||||
server_name: 兼容保留(旧版页脚署名),页脚现固定为平台品牌
|
||||
env: ``prod`` / ``dev``
|
||||
timeout: HTTP 超时秒
|
||||
retries: 重试次数(含首次,默认 3)
|
||||
@@ -175,7 +184,7 @@ class Robot:
|
||||
# ---- 对外 ----
|
||||
def send(self, title: str, content: str, status: str = "info",
|
||||
server_name: str = "") -> None:
|
||||
"""发送统一格式 Markdown(彩色标题 + 正文 + 页脚)。失败抛异常。"""
|
||||
"""发送统一格式 Markdown(彩色标题 + 正文 + 统一页脚)。失败抛异常。"""
|
||||
text = render_markdown(title, content, status, server_name or self.server_name)
|
||||
payload = {"msgtype": "markdown", "markdown": {"title": title, "text": text}}
|
||||
self.log(f"[INFO] 钉钉机器人推送通知 [{status}]: {title}")
|
||||
@@ -302,6 +311,7 @@ main = push
|
||||
|
||||
|
||||
__all__ = [
|
||||
"BRAND",
|
||||
"DEFAULT_BATCH_SIZE",
|
||||
"DEFAULT_MAX_CHARS",
|
||||
"DEFAULT_TITLE",
|
||||
|
||||
Reference in New Issue
Block a user