feat(dingtalk): 机器人页脚统一品牌并发布 0.1.4
新增 BRAND 常量,Markdown 页脚固定为「37du 自动化平台 | 时间」,不再重复插件名;同步 api.json 导出清单。
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
{
|
{
|
||||||
"schema_version": 1,
|
"schema_version": 1,
|
||||||
"sdk_id": "dingtalk_sdk",
|
"sdk_id": "dingtalk_sdk",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"modules": {
|
"modules": {
|
||||||
"dingtalk_sdk": {
|
"dingtalk_sdk": {
|
||||||
"exports": [
|
"exports": [
|
||||||
"AccessToken",
|
"AccessToken",
|
||||||
"AiTable",
|
"AiTable",
|
||||||
|
"BRAND",
|
||||||
"ApiError",
|
"ApiError",
|
||||||
"AttachmentExpiredError",
|
"AttachmentExpiredError",
|
||||||
"AuthError",
|
"AuthError",
|
||||||
@@ -59,6 +60,7 @@
|
|||||||
"all": [
|
"all": [
|
||||||
"AccessToken",
|
"AccessToken",
|
||||||
"AiTable",
|
"AiTable",
|
||||||
|
"BRAND",
|
||||||
"ApiError",
|
"ApiError",
|
||||||
"AttachmentExpiredError",
|
"AttachmentExpiredError",
|
||||||
"AuthError",
|
"AuthError",
|
||||||
@@ -505,6 +507,7 @@
|
|||||||
},
|
},
|
||||||
"dingtalk_sdk.robot": {
|
"dingtalk_sdk.robot": {
|
||||||
"exports": [
|
"exports": [
|
||||||
|
"BRAND",
|
||||||
"DEFAULT_BATCH_SIZE",
|
"DEFAULT_BATCH_SIZE",
|
||||||
"DEFAULT_MAX_CHARS",
|
"DEFAULT_MAX_CHARS",
|
||||||
"DEFAULT_TITLE",
|
"DEFAULT_TITLE",
|
||||||
@@ -520,6 +523,7 @@
|
|||||||
"split_by_chars"
|
"split_by_chars"
|
||||||
],
|
],
|
||||||
"all": [
|
"all": [
|
||||||
|
"BRAND",
|
||||||
"DEFAULT_BATCH_SIZE",
|
"DEFAULT_BATCH_SIZE",
|
||||||
"DEFAULT_MAX_CHARS",
|
"DEFAULT_MAX_CHARS",
|
||||||
"DEFAULT_TITLE",
|
"DEFAULT_TITLE",
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ AI 表格(notable)读写 :class:`aitable.AiTable` 与值归一工具
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
__version__ = "0.1.3"
|
__version__ = "0.1.4"
|
||||||
|
|
||||||
from . import aitable, auth, config, dws, errors, http, media, robot
|
from . import aitable, auth, config, dws, errors, http, media, robot
|
||||||
from .aitable import (AiTable, attachment_items, coerce_for_fields, flatten, ms_to_str,
|
from .aitable import (AiTable, attachment_items, coerce_for_fields, flatten, ms_to_str,
|
||||||
@@ -45,8 +45,8 @@ from .dws import Dws, push as push_dws
|
|||||||
from .errors import (ApiError, AttachmentExpiredError, AuthError, CancelledError,
|
from .errors import (ApiError, AttachmentExpiredError, AuthError, CancelledError,
|
||||||
ConfigError, DingTalkError, DwsError, NetworkError)
|
ConfigError, DingTalkError, DwsError, NetworkError)
|
||||||
from .media import markdown_image, upload_media
|
from .media import markdown_image, upload_media
|
||||||
from .robot import (DingTalkRobot, Robot, build_sign, build_url, compose_batch_messages,
|
from .robot import (BRAND, DingTalkRobot, Robot, build_sign, build_url,
|
||||||
push as push_robot, split_by_chars)
|
compose_batch_messages, push as push_robot, split_by_chars)
|
||||||
|
|
||||||
|
|
||||||
def __getattr__(name: str):
|
def __getattr__(name: str):
|
||||||
@@ -64,6 +64,7 @@ def __getattr__(name: str):
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
"AccessToken",
|
"AccessToken",
|
||||||
"AiTable",
|
"AiTable",
|
||||||
|
"BRAND",
|
||||||
"ApiError",
|
"ApiError",
|
||||||
"AttachmentExpiredError",
|
"AttachmentExpiredError",
|
||||||
"AuthError",
|
"AuthError",
|
||||||
|
|||||||
@@ -55,14 +55,20 @@ def test_robot() -> None:
|
|||||||
_ok("timestamp=" in signed and "sign=" in signed, "有 secret 时追加 timestamp/sign")
|
_ok("timestamp=" in signed and "sign=" in signed, "有 secret 时追加 timestamp/sign")
|
||||||
|
|
||||||
text = robot.render_markdown("标题", "正文", status="error", server_name="署名")
|
text = robot.render_markdown("标题", "正文", status="error", server_name="署名")
|
||||||
_ok("#ff0000" in text and "标题" in text and "正文" in text and "署名" in text,
|
_ok("#ff0000" in text and "标题" in text and "正文" in text,
|
||||||
"Markdown 渲染包含状态色/标题/正文/页脚")
|
"Markdown 渲染包含状态色/标题/正文")
|
||||||
|
_ok(robot.BRAND in text and "署名" not in text,
|
||||||
|
"页脚固定平台品牌,不重复调用方标题/署名")
|
||||||
|
|
||||||
blocks = [f"block{i}" for i in range(23)]
|
blocks = [f"block{i}" for i in range(23)]
|
||||||
batches = robot.compose_batch_messages(blocks, "【测试】", batch_size=10)
|
batches = robot.compose_batch_messages(blocks, "【测试】", batch_size=10)
|
||||||
_eq(len(batches), 3, "块格式 23 条按 10 分批")
|
_eq(len(batches), 3, "块格式 23 条按 10 分批")
|
||||||
_ok("第1/3批" in batches[0] and "本批共10条" in batches[0], "块格式批次头尾")
|
_ok("第1/3批" in batches[0] and "本批共10条" in batches[0], "块格式批次头尾")
|
||||||
|
_ok("【测试】" not in batches[0], "批次正文不重复标题前缀")
|
||||||
_ok("本批共3条" in batches[2], "最后一批条数正确")
|
_ok("本批共3条" in batches[2], "最后一批条数正确")
|
||||||
|
_ok("第1/1批" not in robot.compose_batch_messages(["a"], "【测试】")[0]
|
||||||
|
and "【测试】" not in robot.compose_batch_messages(["a"], "【测试】")[0],
|
||||||
|
"单批不加批次标题")
|
||||||
_eq(robot.compose_batch_messages([], "x"), [], "空块列表返回空")
|
_eq(robot.compose_batch_messages([], "x"), [], "空块列表返回空")
|
||||||
|
|
||||||
long_text = "\n".join("行" * 50 for _ in range(10))
|
long_text = "\n".join("行" * 50 for _ in range(10))
|
||||||
@@ -73,7 +79,7 @@ def test_robot() -> None:
|
|||||||
|
|
||||||
r = robot.Robot(webhook="https://example.invalid/robot/send", server_name="X",
|
r = robot.Robot(webhook="https://example.invalid/robot/send", server_name="X",
|
||||||
retries=1, log=lambda m: None)
|
retries=1, log=lambda m: None)
|
||||||
_eq(r.server_name, "X", "Robot 页脚署名为显式值")
|
_eq(r.server_name, "X", "Robot server_name 兼容保留")
|
||||||
_raises(ValueError, lambda: robot.Robot(webhook=" "), "空 webhook 报错")
|
_raises(ValueError, lambda: robot.Robot(webhook=" "), "空 webhook 报错")
|
||||||
|
|
||||||
res = robot.push("", log=lambda m: None)
|
res = robot.push("", log=lambda m: None)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
能力(与原 ``workflows/share/dingtalk_robot.py`` 及各插件内副本一致):
|
能力(与原 ``workflows/share/dingtalk_robot.py`` 及各插件内副本一致):
|
||||||
- 加签(HMAC-SHA256 + Base64 + URL 编码)与不加签两种模式;
|
- 加签(HMAC-SHA256 + Base64 + URL 编码)与不加签两种模式;
|
||||||
- Markdown 消息:标题按状态着色 + 正文 + 页脚署名/时间;
|
- Markdown 消息:标题按状态着色 + 正文 + 统一页脚(``37du 自动化平台 | 时间``);
|
||||||
- 超长内容按行分批(``max_chars``);块格式按条数分批(``batch_size``);
|
- 超长内容按行分批(``max_chars``);块格式按条数分批(``batch_size``);
|
||||||
- 多环境(prod/dev)webhook 选择,失败自动重试(默认 3 次)。
|
- 多环境(prod/dev)webhook 选择,失败自动重试(默认 3 次)。
|
||||||
|
|
||||||
@@ -39,6 +39,9 @@ DEFAULT_TITLE = "消息通知"
|
|||||||
DEFAULT_MAX_CHARS = 15000 # 钉钉 Markdown 上限约 20000
|
DEFAULT_MAX_CHARS = 15000 # 钉钉 Markdown 上限约 20000
|
||||||
DEFAULT_BATCH_SIZE = 10
|
DEFAULT_BATCH_SIZE = 10
|
||||||
|
|
||||||
|
#: 统一页脚品牌(全平台一致,不随插件/任务变化;与 plugins/XQ0009、workflows/share 副本对齐)
|
||||||
|
BRAND = "37du 自动化平台"
|
||||||
|
|
||||||
|
|
||||||
# ==================== 加签 / URL ====================
|
# ==================== 加签 / URL ====================
|
||||||
|
|
||||||
@@ -64,20 +67,26 @@ def build_url(webhook: str, secret: str = "") -> str:
|
|||||||
|
|
||||||
def render_markdown(title: str, content: str, status: str = "info",
|
def render_markdown(title: str, content: str, status: str = "info",
|
||||||
server_name: str = "") -> str:
|
server_name: str = "") -> str:
|
||||||
"""渲染统一格式的 Markdown 正文(彩色标题 + 正文 + 页脚)。"""
|
"""渲染统一格式的 Markdown 正文(彩色标题 + 正文 + 统一页脚)。
|
||||||
|
|
||||||
|
页脚固定为 ``###### 37du 自动化平台 | 时间``,**不再重复标题/插件名**(标题上方已有);
|
||||||
|
``server_name`` 仅为旧调用方签名兼容保留,不参与渲染。
|
||||||
|
"""
|
||||||
color = STATUS_COLORS.get((status or "").strip().lower(), STATUS_COLORS["info"])
|
color = STATUS_COLORS.get((status or "").strip().lower(), STATUS_COLORS["info"])
|
||||||
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
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}"
|
return f"**<font color='{color}'>{title}</font>**\n\n{str(content).strip()}\n{footer}"
|
||||||
|
|
||||||
|
|
||||||
def compose_batch_messages(blocks: Sequence[str], title_prefix: str,
|
def compose_batch_messages(blocks: Sequence[str], title_prefix: str,
|
||||||
batch_size: int = DEFAULT_BATCH_SIZE) -> List[str]:
|
batch_size: int = DEFAULT_BATCH_SIZE) -> List[str]:
|
||||||
"""把 block 列表按 ``batch_size`` 条一批,生成带批次标题的待推送文本列表。
|
"""把 block 列表按 ``batch_size`` 条一批,生成待推送文本列表。
|
||||||
|
|
||||||
|
消息顶部已有彩色标题,正文里**不再重复标题**:单批不加头,多批只标 ``###### 第i/N批``。
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
blocks: 每条记录格式化后的 block 字符串列表(业务字段由调用方负责)
|
blocks: 每条记录格式化后的 block 字符串列表(业务字段由调用方负责)
|
||||||
title_prefix: 批次标题前缀,如 ``【请假审批】通过``
|
title_prefix: 兼容保留(旧版用它当批次标题前缀),不参与渲染
|
||||||
batch_size: 每批最大条数(默认 10)
|
batch_size: 每批最大条数(默认 10)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -90,7 +99,7 @@ def compose_batch_messages(blocks: Sequence[str], title_prefix: str,
|
|||||||
out = []
|
out = []
|
||||||
for idx in range(total):
|
for idx in range(total):
|
||||||
batch = list(blocks[idx * size:(idx + 1) * size])
|
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)}条"
|
footer = f"\n\n###### 本批共{len(batch)}条"
|
||||||
out.append(header + "\n\n".join(batch) + footer)
|
out.append(header + "\n\n".join(batch) + footer)
|
||||||
@@ -125,7 +134,7 @@ class Robot:
|
|||||||
Args:
|
Args:
|
||||||
webhook: 机器人地址;缺省按 ``env`` 取配置
|
webhook: 机器人地址;缺省按 ``env`` 取配置
|
||||||
secret: 加签密钥;缺省按 ``env`` 取配置
|
secret: 加签密钥;缺省按 ``env`` 取配置
|
||||||
server_name: 页脚署名;缺省按 ``env`` 取配置
|
server_name: 兼容保留(旧版页脚署名),页脚现固定为平台品牌
|
||||||
env: ``prod`` / ``dev``
|
env: ``prod`` / ``dev``
|
||||||
timeout: HTTP 超时秒
|
timeout: HTTP 超时秒
|
||||||
retries: 重试次数(含首次,默认 3)
|
retries: 重试次数(含首次,默认 3)
|
||||||
@@ -175,7 +184,7 @@ class Robot:
|
|||||||
# ---- 对外 ----
|
# ---- 对外 ----
|
||||||
def send(self, title: str, content: str, status: str = "info",
|
def send(self, title: str, content: str, status: str = "info",
|
||||||
server_name: str = "") -> None:
|
server_name: str = "") -> None:
|
||||||
"""发送统一格式 Markdown(彩色标题 + 正文 + 页脚)。失败抛异常。"""
|
"""发送统一格式 Markdown(彩色标题 + 正文 + 统一页脚)。失败抛异常。"""
|
||||||
text = render_markdown(title, content, status, server_name or self.server_name)
|
text = render_markdown(title, content, status, server_name or self.server_name)
|
||||||
payload = {"msgtype": "markdown", "markdown": {"title": title, "text": text}}
|
payload = {"msgtype": "markdown", "markdown": {"title": title, "text": text}}
|
||||||
self.log(f"[INFO] 钉钉机器人推送通知 [{status}]: {title}")
|
self.log(f"[INFO] 钉钉机器人推送通知 [{status}]: {title}")
|
||||||
@@ -302,6 +311,7 @@ main = push
|
|||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
"BRAND",
|
||||||
"DEFAULT_BATCH_SIZE",
|
"DEFAULT_BATCH_SIZE",
|
||||||
"DEFAULT_MAX_CHARS",
|
"DEFAULT_MAX_CHARS",
|
||||||
"DEFAULT_TITLE",
|
"DEFAULT_TITLE",
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "dingtalk_sdk"
|
name = "dingtalk_sdk"
|
||||||
version = "0.1.3"
|
version = "0.1.4"
|
||||||
description = "钉钉能力 SDK:群机器人推送 / 应用鉴权 / 媒体上传 / 单聊群聊推送(dws)/ AI 表格读写与月报周报采集"
|
description = "钉钉能力 SDK:群机器人推送 / 应用鉴权 / 媒体上传 / 单聊群聊推送(dws)/ AI 表格读写与月报周报采集"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.9"
|
requires-python = ">=3.9"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"schema_version": 1,
|
"schema_version": 1,
|
||||||
"id": "dingtalk_sdk",
|
"id": "dingtalk_sdk",
|
||||||
"name": "钉钉 SDK",
|
"name": "钉钉 SDK",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"description": "钉钉能力集合:群机器人推送(加签/Markdown/状态色/分批/块格式/多环境)、企业内部应用鉴权(v1 + oapi)、媒体库上传、dws 单聊/群聊推送(文本/文件/@/限流/推送记录)、AI 表格读写与月报周报采集",
|
"description": "钉钉能力集合:群机器人推送(加签/Markdown/状态色/分批/块格式/多环境)、企业内部应用鉴权(v1 + oapi)、媒体库上传、dws 单聊/群聊推送(文本/文件/@/限流/推送记录)、AI 表格读写与月报周报采集",
|
||||||
"provides": [
|
"provides": [
|
||||||
"dingtalk_sdk"
|
"dingtalk_sdk"
|
||||||
|
|||||||
Reference in New Issue
Block a user