docs: 批量更新项目评级文档,移除200元/400元/600元金额标注
refactor: 重构钉钉推送逻辑,移除内嵌dws命令改用全局变量传递 fix: 雇主险调度逻辑,修复名额复用与增删员同步问题 style: 优化文档表格排版与代码注释格式
This commit is contained in:
@@ -2,28 +2,27 @@
|
||||
"""
|
||||
@FileName: dingtalk_dws_push.py
|
||||
@Description: 钉钉推送共享模块(单聊 + 群聊,文本 / 文件,基于 dws 命令行)。
|
||||
供各业务脚本 import 复用,也可作为影刀自定义指令「37du钉钉单聊推送」
|
||||
/「37du钉钉群聊推送」的模块源码。
|
||||
供各业务脚本 import 复用,也可作为影刀自定义指令「37du钉钉推送」的模块源码
|
||||
(同一入口,chat_type 参数决定单聊还是群聊)。
|
||||
|
||||
入口:
|
||||
main(messages, record_dir='') 影刀自定义指令「37du钉钉单聊推送」调用入口。
|
||||
传入消息列表,每项为五元组 (消息类型, 姓名, 工号, 消息内容, 文件地址):
|
||||
入口(统一 main,按消息结构自动识别单聊/群聊):
|
||||
main(messages, record_dir='', chat_type='') 影刀自定义指令「37du钉钉推送」调用入口。
|
||||
通道判定:chat_type 留空(默认)时按每条消息结构自动判断——dict 含 group/群名 键为群聊,
|
||||
其余为单聊;显式传 'single'/'group' 时整批强制用该通道(兼容旧流程)。
|
||||
chat_type='single'/单聊:传入消息列表,每项为五元组 (消息类型, 姓名, 工号, 消息内容, 文件地址):
|
||||
msg_type 消息类型:'text'/'文本' 或 'file'/'文件'
|
||||
name 接收人姓名(文本/文件均按姓名解析 openDingTalkId;同名多人时按工号精确定位)
|
||||
user_id 接收人工号(同名消歧;发文件且对方无会话时用于先建会话)
|
||||
content 消息内容(msg_type=text 时必填)
|
||||
file_path 文件地址(msg_type=file 时必填,本地绝对路径)
|
||||
record_dir(可选):非空时每条结果写入 <record_dir>/YYYY-MM-DD/推送记录.csv,
|
||||
并把记录文件路径写入影刀全局变量 push_record_path,供流程处理失败名单。
|
||||
返回: {"success": bool, "message": str, "results": [...]}
|
||||
|
||||
main_group(messages, record_dir='') 影刀自定义指令「37du钉钉群聊推送」调用入口。
|
||||
传入消息列表,每项支持 dict 或元组:
|
||||
chat_type='group'/群聊:传入消息列表,每项支持 dict 或元组:
|
||||
dict: {"group": 群名, "content": 文本, "file_path": 文件,
|
||||
"title": 标题, "at_all": bool, "at_open_ids": "openId1,openId2"}
|
||||
元组: (群名, 文本内容, 文件地址, 标题, at_all, at_open_ids),后 4 项可省略
|
||||
按群名解析 openconversation_id 后发送(文本/文件可同时存在,@ 需在内容中含占位符)。
|
||||
返回: {"success": bool, "message": str, "results": [...]}
|
||||
record_dir(可选):非空时每条结果写入 <record_dir>/YYYY-MM-DD/推送记录.csv,
|
||||
并把记录文件路径写入影刀全局变量 push_record_path,供流程处理失败名单。
|
||||
逐条发送,返回: {"success": bool, "message": str, "results": [...]}
|
||||
|
||||
send_message(msg_type, name, user_id, content, file_path, record_dir='') # 单聊单条发送(内部/业务脚本复用)
|
||||
send_group_message(group_name, content, file_path, title, at_all, at_open_ids, record_dir='') # 群聊单条发送
|
||||
@@ -373,10 +372,14 @@ def send_file(open_id: str, file_path: str):
|
||||
def search_group(query: str, limit: int = 20) -> list:
|
||||
"""按群名搜索群聊(关键词模糊搜索),返回候选列表。
|
||||
注意:dws 要求 query 使用群名连续核心词(如「项目冲刺」),勿拆成单字搜索。
|
||||
dws chat search 返回结构为 result.groups(result 是字典),此处统一提取列表。
|
||||
"""
|
||||
data = _run_dws([DWS_BIN, "chat", "search", "--query", query,
|
||||
"--limit", str(limit), "--format", "json"])
|
||||
return data.get("result") or []
|
||||
result = data.get("result") or {}
|
||||
if isinstance(result, dict):
|
||||
return result.get("groups") or []
|
||||
return result or []
|
||||
|
||||
|
||||
def _group_conv_id(group: dict) -> str:
|
||||
@@ -388,32 +391,40 @@ def _group_conv_id(group: dict) -> str:
|
||||
return ""
|
||||
|
||||
|
||||
def _group_title(group: dict) -> str:
|
||||
"""群搜索条目里的群名:dws chat search 返回的群名字段是 title(个别场景可能是 name)。"""
|
||||
for key in ("title", "name"):
|
||||
val = (group or {}).get(key)
|
||||
if val:
|
||||
return str(val)
|
||||
return ""
|
||||
|
||||
|
||||
def resolve_group_id(group_name: str) -> str:
|
||||
"""按群名解析群聊 openconversation_id。
|
||||
先模糊搜索候选,再按精确同名过滤;唯一命中即返回,多候选/无候选则报错带候选清单,
|
||||
绝不自行假定第一个(与 resolve_open_id 的同名歧义处理一致)。
|
||||
"""按群名精确解析群聊 openconversation_id(群名一律传全称)。
|
||||
先按全称搜索,再对 title 做全等过滤:唯一命中即返回;无匹配或同名多个直接报错,
|
||||
不采用模糊候选(绝不推给相近群名)。
|
||||
"""
|
||||
group_name = str(group_name or "").strip()
|
||||
if not group_name:
|
||||
raise RuntimeError("群名不能为空(请传群全称)")
|
||||
groups = search_group(group_name)
|
||||
if not groups:
|
||||
raise RuntimeError(f"未找到群「{group_name}」,请确认群名后重试")
|
||||
exact = [g for g in groups if (g.get("name") or "").strip() == group_name]
|
||||
if exact:
|
||||
groups = exact
|
||||
candidates = [g for g in groups if _group_conv_id(g)]
|
||||
if len(candidates) == 1:
|
||||
g = candidates[0]
|
||||
print(f"[INFO] 命中群: {g.get('name')} | openConversationId={_group_conv_id(g)}")
|
||||
raise RuntimeError(f"未找到群「{group_name}」,请确认群全称后重试")
|
||||
exact = [g for g in groups if _group_title(g) == group_name]
|
||||
if len(exact) == 1:
|
||||
g = exact[0]
|
||||
print(f"[INFO] 命中群: {_group_title(g)} | openConversationId={_group_conv_id(g)}")
|
||||
return _group_conv_id(g)
|
||||
if not candidates:
|
||||
if not exact:
|
||||
hints = "\n".join(
|
||||
f" {_group_title(g) or '未知群名'}" for g in groups)
|
||||
raise RuntimeError(
|
||||
f"群「{group_name}」搜索结果均无 openconversation_id,原始数据:{groups}")
|
||||
f"未找到与「{group_name}」全等匹配的群,附近候选:\n{hints}")
|
||||
hints = "\n".join(
|
||||
f" {g.get('name') or '未知群名'} | {_group_conv_id(g)}" for g in candidates
|
||||
)
|
||||
f" {_group_title(g) or '未知群名'} | {_group_conv_id(g)}" for g in exact)
|
||||
raise RuntimeError(
|
||||
f"群名「{group_name}」匹配到 {len(candidates)} 个候选,请使用更精确的群名:\n{hints}"
|
||||
)
|
||||
f"群名「{group_name}」匹配到 {len(exact)} 个同名候选,请确认后重试:\n{hints}")
|
||||
|
||||
|
||||
def send_group_text(conversation_id: str, text: str, title: str = "",
|
||||
@@ -485,41 +496,6 @@ def send_group_message(group_name, content="", file_path="", title="",
|
||||
record_dir, "", name, content, file_path)
|
||||
|
||||
|
||||
def main_group(messages, record_dir=""):
|
||||
"""影刀自定义指令「37du钉钉群聊推送」调用入口。
|
||||
传入消息列表,每项支持 dict 或元组:
|
||||
dict: {"group": 群名, "content": 文本, "file_path": 文件,
|
||||
"title": 标题, "at_all": bool, "at_open_ids": "openId1,openId2"}
|
||||
元组: (群名, 文本内容, 文件地址, 标题, at_all, at_open_ids),后 4 项可省略。
|
||||
逐条发送,返回: {"success": bool, "message": str, "results": [...]}
|
||||
"""
|
||||
results = []
|
||||
for item in messages or []:
|
||||
if isinstance(item, dict):
|
||||
group = item.get("group") or item.get("群名") or ""
|
||||
content = item.get("content") or item.get("文本内容") or ""
|
||||
file_path = item.get("file_path") or item.get("文件地址") or ""
|
||||
title = item.get("title") or item.get("标题") or ""
|
||||
at_all = bool(item.get("at_all") or item.get("@所有人"))
|
||||
at_open_ids = item.get("at_open_ids") or item.get("@成员") or ""
|
||||
else:
|
||||
group = item[0] if len(item) > 0 else ""
|
||||
content = item[1] if len(item) > 1 else ""
|
||||
file_path = item[2] if len(item) > 2 else ""
|
||||
title = item[3] if len(item) > 3 else ""
|
||||
at_all = bool(item[4]) if len(item) > 4 else False
|
||||
at_open_ids = item[5] if len(item) > 5 else ""
|
||||
results.append(send_group_message(group, content, file_path, title,
|
||||
at_all, at_open_ids, record_dir))
|
||||
ok = sum(1 for r in results if r.get("success"))
|
||||
_write_record_path_to_glv(record_dir)
|
||||
return {
|
||||
"success": ok == len(results),
|
||||
"message": f"共 {len(results)} 条,成功 {ok},失败 {len(results) - ok}",
|
||||
"results": results,
|
||||
}
|
||||
|
||||
|
||||
# ==================== 统一入口 ====================
|
||||
def send_message(msg_type, name, user_id, content, file_path="", record_dir=""):
|
||||
"""单条推送:按消息类型分发文本 / 文件。
|
||||
@@ -565,34 +541,80 @@ def send_message(msg_type, name, user_id, content, file_path="", record_dir=""):
|
||||
record_dir, name, user_id, content, file_path)
|
||||
|
||||
|
||||
def main(messages, record_dir=""):
|
||||
"""影刀自定义指令「37du钉钉单聊推送」调用入口。
|
||||
传入消息列表,每项为五元组 (消息类型, 姓名, 工号, 消息内容, 文件地址),
|
||||
缺省字段按空处理:
|
||||
msg_type 'text'/'文本' 或 'file'/'文件'
|
||||
name 接收人姓名
|
||||
user_id 接收人工号
|
||||
content 消息内容(文本类型必填)
|
||||
file_path 文件地址(文件类型必填,可留空)
|
||||
def _normalize_chat_type(chat_type: str) -> str:
|
||||
"""推送通道归一化:'single'/'单聊' → 'single';'group'/'群聊' → 'group'"""
|
||||
key = str(chat_type or "").strip().lower()
|
||||
if key in ("single", "单聊", "single_chat", "person", "个人", "私聊"):
|
||||
return "single"
|
||||
if key in ("group", "群聊", "group_chat", "群"):
|
||||
return "group"
|
||||
raise ValueError(f"未知推送通道「{chat_type}」,仅支持:single/单聊、group/群聊")
|
||||
|
||||
|
||||
def _item_is_group(item) -> bool:
|
||||
"""按消息结构自动判断推送通道:dict 含 group/群名 键 → 群聊;其余(元组 / msg_type dict)→ 单聊。"""
|
||||
return isinstance(item, dict) and bool(item.get("group") or item.get("群名"))
|
||||
|
||||
|
||||
def main(messages, record_dir="", chat_type=""):
|
||||
"""影刀自定义指令「37du钉钉推送」统一调用入口。
|
||||
|
||||
通道判定(二选一):
|
||||
- chat_type 显式传 'single'/'group':整批消息强制按该通道发送(兼容旧流程);
|
||||
- chat_type 留空(默认):按每条消息结构自动判断——dict 含 group/群名 键为群聊,其余为单聊。
|
||||
因此流程只需传消息列表,群聊消息自带 group 字段即可,无需单独配置 chat_type。
|
||||
|
||||
chat_type='single'/单聊:每项为五元组 (消息类型, 姓名, 工号, 消息内容, 文件地址),
|
||||
或 dict 含 msg_type/name/user_id(缺省字段按空处理):
|
||||
msg_type 'text'/'文本' 或 'file'/'文件'
|
||||
name 接收人姓名
|
||||
user_id 接收人工号
|
||||
content 消息内容(文本类型必填)
|
||||
file_path 文件地址(文件类型必填,可留空)
|
||||
chat_type='group'/群聊:每项支持 dict 或元组:
|
||||
dict: {"group": 群名, "content": 文本, "file_path": 文件,
|
||||
"title": 标题, "at_all": bool, "at_open_ids": "openId1,openId2"}
|
||||
元组: (群名, 文本内容, 文件地址, 标题, at_all, at_open_ids),后 4 项可省略
|
||||
record_dir(可选):非空时每条结果写入 <record_dir>/YYYY-MM-DD/推送记录.csv,
|
||||
并把记录文件路径写入影刀全局变量 push_record_path,供流程自行处理失败名单。
|
||||
逐条发送,返回: {"success": bool, "message": str, "results": [...]}
|
||||
"""
|
||||
forced_channel = _normalize_chat_type(chat_type) if chat_type else ""
|
||||
results = []
|
||||
for item in messages or []:
|
||||
if isinstance(item, dict):
|
||||
msg_type = item.get("msg_type") or item.get("消息类型") or ""
|
||||
name = item.get("name") or item.get("姓名") or ""
|
||||
user_id = item.get("user_id") or item.get("工号") or ""
|
||||
content = item.get("content") or item.get("消息内容") or ""
|
||||
file_path = item.get("file_path") or item.get("文件地址") or ""
|
||||
if (forced_channel == "group") or (not forced_channel and _item_is_group(item)):
|
||||
# 群聊:按群名解析 openconversation_id 后发送(文本/文件可同时存在)
|
||||
if isinstance(item, dict):
|
||||
group = item.get("group") or item.get("群名") or ""
|
||||
content = item.get("content") or item.get("文本内容") or ""
|
||||
file_path = item.get("file_path") or item.get("文件地址") or ""
|
||||
title = item.get("title") or item.get("标题") or ""
|
||||
at_all = bool(item.get("at_all") or item.get("@所有人"))
|
||||
at_open_ids = item.get("at_open_ids") or item.get("@成员") or ""
|
||||
else:
|
||||
group = item[0] if len(item) > 0 else ""
|
||||
content = item[1] if len(item) > 1 else ""
|
||||
file_path = item[2] if len(item) > 2 else ""
|
||||
title = item[3] if len(item) > 3 else ""
|
||||
at_all = bool(item[4]) if len(item) > 4 else False
|
||||
at_open_ids = item[5] if len(item) > 5 else ""
|
||||
results.append(send_group_message(group, content, file_path, title,
|
||||
at_all, at_open_ids, record_dir))
|
||||
else:
|
||||
msg_type = item[0] if len(item) > 0 else ""
|
||||
name = item[1] if len(item) > 1 else ""
|
||||
user_id = item[2] if len(item) > 2 else ""
|
||||
content = item[3] if len(item) > 3 else ""
|
||||
file_path = item[4] if len(item) > 4 else ""
|
||||
results.append(send_message(msg_type, name, user_id, content, file_path, record_dir))
|
||||
# 单聊:按姓名解析 openDingTalkId 后发送
|
||||
if isinstance(item, dict):
|
||||
msg_type = item.get("msg_type") or item.get("消息类型") or ""
|
||||
name = item.get("name") or item.get("姓名") or ""
|
||||
user_id = item.get("user_id") or item.get("工号") or ""
|
||||
content = item.get("content") or item.get("消息内容") or ""
|
||||
file_path = item.get("file_path") or item.get("文件地址") or ""
|
||||
else:
|
||||
msg_type = item[0] if len(item) > 0 else ""
|
||||
name = item[1] if len(item) > 1 else ""
|
||||
user_id = item[2] if len(item) > 2 else ""
|
||||
content = item[3] if len(item) > 3 else ""
|
||||
file_path = item[4] if len(item) > 4 else ""
|
||||
results.append(send_message(msg_type, name, user_id, content, file_path, record_dir))
|
||||
ok = sum(1 for r in results if r.get("success"))
|
||||
_write_record_path_to_glv(record_dir)
|
||||
return {
|
||||
@@ -604,10 +626,12 @@ def main(messages, record_dir=""):
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 本地自测/接口说明:不实际发送。业务脚本请 import 后调用
|
||||
# main([("文本", "姓名", "工号", "消息内容", ""),
|
||||
# ("文件", "姓名", "工号", "", r"文件绝对路径")])
|
||||
# main_group([{"group": "群名", "content": "消息内容"},
|
||||
# {"group": "群名", "content": "", "file_path": r"文件绝对路径"}])
|
||||
# 单聊(chat_type 留空自动识别):
|
||||
# main([("文本", "姓名", "工号", "消息内容", ""),
|
||||
# ("文件", "姓名", "工号", "", r"文件绝对路径")])
|
||||
# 群聊(消息 dict 含 group 键自动识别,无需传 chat_type):
|
||||
# main([{"group": "群名", "content": "消息内容"},
|
||||
# {"group": "群名", "content": "", "file_path": r"文件绝对路径"}])
|
||||
messages = [
|
||||
{"msg_type": "text", "name": "夏一平", "user_id": "37036361", "content": "你好,这是由ai推送的文本消息"},
|
||||
# {"msg_type": "file", "name": "夏一平", "user_id": "37036361", "file_path": r"C:\Users\admin\Desktop\test.xlsx"},
|
||||
|
||||
Reference in New Issue
Block a user