refactor(yingdao): 重构钉钉推送逻辑,新增VC运行库自包含支持
1. 重构钉钉推送模块:改为接收元组列表参数,移除群消息推送逻辑,拆分单聊/群推送职责 2. 新增VC运行库自动加载逻辑,解决onnxruntime依赖运行库缺失问题 3. 升级依赖包版本并做兼容性降级适配,统一numpy/opencv/onnxruntime版本 4. 更新考勤异常、离职审批脚本及文档,适配新的推送参数格式
This commit is contained in:
@@ -11,13 +11,10 @@
|
||||
如何在影刀中创建指令:
|
||||
1. 影刀客户端 → 新建「PC自动化指令」→ 指令编码如 dingtalk_sender,添加 Python 模块;
|
||||
2. 把本文件内容粘贴进指令的 Python 模块(或直接以本文件作为模块源码);
|
||||
3. 增加流程参数 params(字典类型,dict),或分别增加 search_keyword/verify_keyword/text 三个参数;
|
||||
3. 增加流程参数 messages(列表类型),每项为 (search_keyword, text, verify_keyword) 元组;
|
||||
4. 保存并「发版」;在需要钉钉推送的应用中「获取指令」并安装;
|
||||
5. 在流程中拖入该指令,把业务脚本输出的参数传入(两种方式任选):
|
||||
方式A(推荐):流程中先拼一个字典变量(如 {"search_keyword": "10086", "verify_keyword": "张三", "text": "..."}),
|
||||
作为 params 整体传入;
|
||||
方式B:分别传 search_keyword、verify_keyword、text 变量,指令内部自动拼成字典;
|
||||
指令返回 JSON:{"success": bool, "message": str},可在后续流程中判断是否重试。
|
||||
5. 在流程中拖入该指令,把业务脚本输出的消息列表直接传给 messages 参数;
|
||||
指令返回 JSON:{"success": bool, "message": str, "results": [...]},可在后续流程中判断是否重试。
|
||||
|
||||
推送原理(OCR 视觉方案,与 app/utils/dingtalk_sender.py 一致):
|
||||
查找钉钉窗口 → 多策略激活到前台 → Ctrl+Shift+F 搜索联系人 → OCR 定位输入框
|
||||
@@ -28,20 +25,15 @@
|
||||
pip install Pillow numpy rapidocr-onnxruntime
|
||||
|
||||
入口:
|
||||
main(args) 影刀自定义指令调用入口
|
||||
args = {
|
||||
"search_keyword": "联系人搜索关键字(支持工号,避免同名误选)",
|
||||
"verify_keyword": "会话校验关键字(钉钉标题显示姓名,用工号搜索时传姓名校验)",
|
||||
"text": "要发送的消息内容",
|
||||
}
|
||||
通过流程参数传入(支持整字典 params 或分别传 search_keyword/verify_keyword/text)
|
||||
messages 批量模式下,消息含 search_keyword 走 OCR 单聊;不含则视为群消息,
|
||||
自动改由钉钉群机器人(dingtalk_robot)webhook 推送。
|
||||
返回 JSON 安全 dict:{"success": bool, "message": str}
|
||||
main(messages) 影刀自定义指令调用入口,直接传列表,每项为 (search_keyword, text, verify_keyword) 元组:
|
||||
search_keyword 联系人搜索关键字(支持工号,避免同名误选)
|
||||
text 要发送的消息内容
|
||||
verify_keyword 会话校验关键字(钉钉标题显示姓名,用工号搜索时传姓名校验,可选)
|
||||
仅推送联系人(单聊);群消息请改用同目录 dingtalk_robot.py(「37du钉钉群机器人推送」指令)。
|
||||
返回 JSON 安全 dict:{"success": bool, "message": str, "results": [...]}
|
||||
|
||||
python dingtalk_sender.py 搜索关键词 消息内容 独立运行(等价于 main({"search_keyword": ..., "text": ...}))
|
||||
python dingtalk_sender.py 独立运行(默认发送 __main__ 中的测试消息列表)
|
||||
"""
|
||||
import sys
|
||||
import time
|
||||
|
||||
# ==================== 第三方库导入(启动校验:全部必须预先安装,不懒加载) ====================
|
||||
@@ -510,79 +502,40 @@ def send_dingtalk_text(search_keyword, text, verify_keyword=None):
|
||||
|
||||
|
||||
# ==================== 影刀自定义指令入口 ====================
|
||||
def _send_one(msg):
|
||||
"""发送单条消息,返回 {success: bool, message: str}。msg 为 {search_keyword?, verify_keyword?, text} 字典。
|
||||
|
||||
- 含 search_keyword:钉钉联系人 OCR 推送(单聊);
|
||||
- 不含 search_keyword:视为群消息,改用钉钉群机器人(webhook)推送。
|
||||
"""
|
||||
search_keyword = str((msg or {}).get("search_keyword", "") or "").strip()
|
||||
text = str((msg or {}).get("text", "") or "")
|
||||
verify_keyword = str((msg or {}).get("verify_keyword", "") or "").strip() or None
|
||||
if not text.strip():
|
||||
return {"success": False, "message": "参数 text 不能为空"}
|
||||
if not search_keyword:
|
||||
# 群消息:交由钉钉群机器人(webhook)推送
|
||||
try:
|
||||
import dingtalk_robot
|
||||
except ImportError:
|
||||
return {"success": False, "message": "未加载群机器人模块(dingtalk_robot.py),无法推送群消息"}
|
||||
try:
|
||||
return dingtalk_robot.main(text)
|
||||
except Exception as e:
|
||||
return {"success": False, "message": f"群机器人推送失败:{e}"}
|
||||
try:
|
||||
send_dingtalk_text(search_keyword, text, verify_keyword)
|
||||
return {"success": True, "message": f"已向 {search_keyword} 发送成功"}
|
||||
except Exception as e:
|
||||
return {"success": False, "message": str(e)}
|
||||
|
||||
|
||||
def main(args):
|
||||
def main(messages):
|
||||
"""
|
||||
影刀自定义指令「37du钉钉推送」调用入口。
|
||||
args: dict,通过流程变量传入(影刀变量支持传字典/列表):
|
||||
方式A(批量,推荐):args = {"messages": [{search_keyword?, verify_keyword?, text}, ...]}
|
||||
messages 为「字典列表」,逐条发送并返回汇总结果;
|
||||
含 search_keyword 走 OCR 单聊;不含则视为群消息,改用钉钉群机器人推送;
|
||||
方式B(单条):args = {"params": {search_keyword, verify_keyword, text}} 或
|
||||
{"search_keyword":..., "verify_keyword":..., "text":...};
|
||||
返回: dict
|
||||
单条: {success: bool, message: str}
|
||||
批量: {success: bool, message: str, sent: int, failed: int, results: [...]}
|
||||
直接传列表,每项为 (search_keyword, text, verify_keyword) 元组,verify_keyword 可省略:
|
||||
search_keyword 联系人搜索关键字(支持工号,避免同名误选)
|
||||
text 要发送的消息内容
|
||||
verify_keyword 会话校验关键字(可选;钉钉标题显示姓名,用工号搜索时传姓名校验)
|
||||
仅推送联系人(单聊);群消息请改用 dingtalk_robot.py(「37du钉钉群机器人推送」指令)。
|
||||
返回: {"success": bool, "message": str, "results": [...]}
|
||||
"""
|
||||
if not args:
|
||||
args = {}
|
||||
# 兼容「整个字典作为 params 传入」的用法
|
||||
if isinstance(args.get("params"), dict):
|
||||
merged = dict(args)
|
||||
merged.update(args["params"])
|
||||
args = merged
|
||||
|
||||
# 批量:messages 为字典列表,逐条发送
|
||||
if isinstance(args.get("messages"), list):
|
||||
results = [_send_one(m) for m in args["messages"]]
|
||||
ok = sum(1 for r in results if r.get("success"))
|
||||
return {
|
||||
"success": ok == len(results),
|
||||
"message": f"共 {len(results)} 条,成功 {ok},失败 {len(results) - ok}",
|
||||
"sent": ok,
|
||||
"failed": len(results) - ok,
|
||||
"results": results,
|
||||
}
|
||||
|
||||
# 单条发送
|
||||
return _send_one(args)
|
||||
results = []
|
||||
for item in messages:
|
||||
search_keyword = str(item[0] or "").strip()
|
||||
text = str(item[1] or "")
|
||||
verify_keyword = str(item[2] or "").strip() or None if len(item) > 2 else None
|
||||
if not text.strip():
|
||||
results.append({"success": False, "message": "参数 text 不能为空"})
|
||||
continue
|
||||
if not search_keyword:
|
||||
results.append({"success": False, "message": "参数 search_keyword 不能为空"})
|
||||
continue
|
||||
try:
|
||||
send_dingtalk_text(search_keyword, text, verify_keyword)
|
||||
results.append({"success": True, "message": f"已向 {search_keyword} 发送成功"})
|
||||
except Exception as e:
|
||||
results.append({"success": False, "message": str(e)})
|
||||
ok = sum(1 for r in results if r.get("success"))
|
||||
return {
|
||||
"success": ok == len(results),
|
||||
"message": f"共 {len(results)} 条,成功 {ok},失败 {len(results) - ok}",
|
||||
"results": results,
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = sys.argv
|
||||
if len(args) >= 3:
|
||||
print(_send_one({"search_keyword": args[1], "text": args[2]}))
|
||||
else:
|
||||
_messages = [
|
||||
{"search_keyword": "夏一平", "verify_keyword": "夏一平", "text": "【37du钉钉推送·测试1】你好,这是一条连通性测试消息。"},
|
||||
{"search_keyword": "夏一平", "verify_keyword": "夏一平", "text": "【37du钉钉推送·测试2】你好,这是第二条测试消息。"},
|
||||
{"text": "【37du钉钉推送·群消息测试】你好,这是一条群机器人测试消息。"},
|
||||
]
|
||||
print(main({"messages": _messages}))
|
||||
messages = [("37036361", "【37du钉钉推送·测试1】你好,这是一条连通性测试消息。", "夏一平"),("37036361", "【37du钉钉推送·测试2】你好,这是第二条测试消息。", "夏一平")]
|
||||
print(main(messages))
|
||||
|
||||
Reference in New Issue
Block a user