feat: 启用考勤异常直接推送,重构钉钉推送适配逻辑
1. 启用app版考勤异常推送:移除push_abnormal中的注释,直接调用钉钉推送接口,不再仅打印日志
2. 更新钉钉推送文档与适配逻辑:
- 重构dingtalk_sender参数为search_keyword/verify_keyword替代target/display_name
- 更新考勤异常推送分组与参数格式,适配新的OCR推送逻辑
3. 移除影刀审批脚本的单聊推送支持,统一使用群机器人推送
4. 优化缺勤分析缓存逻辑,移除历史月缓存限制,改用懒加载缓存
5. 为影刀考勤异常脚本新增联调测试模式,避免联调期间打扰真实员工
This commit is contained in:
@@ -204,14 +204,14 @@ def build_notify_text(items: List[dict]) -> str:
|
||||
|
||||
|
||||
def push_abnormal(items: List[dict]) -> int:
|
||||
"""按「姓名-工号」分组生成钉钉通知文本(暂不实际发送,先供人工确认)。
|
||||
"""按「姓名-工号」分组生成钉钉通知文本并直接推送(app 版主脚本直接调用,无需全局变量)。
|
||||
|
||||
同一人员多条异常合并为一条通知,大幅减少推送次数。
|
||||
|
||||
Returns:
|
||||
生成通知的人员数(即推送次数)
|
||||
成功推送的人员数(即推送次数)
|
||||
"""
|
||||
from app.utils.dingtalk_sender import send_to_person # noqa: F401
|
||||
from app.utils.dingtalk_sender import send_to_person
|
||||
|
||||
groups: Dict[tuple, List[dict]] = {}
|
||||
for it in items:
|
||||
@@ -224,10 +224,8 @@ def push_abnormal(items: List[dict]) -> int:
|
||||
|
||||
for (_name, _code), group in groups.items():
|
||||
text = build_notify_text(group)
|
||||
# 先注释掉实际推送,仅输出文本供确认
|
||||
logger.info("【待推送通知】\n%s", text)
|
||||
# send_to_person(_name, text=text)
|
||||
# logger.info("已向 %s 推送考勤异常通知(%d 条合并)", _name, len(group))
|
||||
send_to_person(_name, text=text)
|
||||
logger.info("已向 %s 推送考勤异常通知(%d 条合并)", _name, len(group))
|
||||
return len(groups)
|
||||
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ HR 会话(getCurUserInfo 校验)→ 分页拉取当月未调整考勤异常
|
||||
| `export_excel` | 字段中文化、列排序、导出 xlsx(文件名带时间戳,处理文件占用) |
|
||||
| `classify_abnormal` | 分级判定:迟到/早退 < 30 分钟 → 不推送;其余 → 推送 |
|
||||
| `build_notify_text` | 按需求格式拼接钉钉通知文本 |
|
||||
| `push_abnormal` | 逐条生成通知文本(当前仅打印,`send_to_person` 注释待启用) |
|
||||
| `push_abnormal` | 按「姓名-工号」合并分组,直接调用 `send_to_person` 推送 |
|
||||
| `_fetch_abnormal_data` | 会话校验 + 计算查询范围 + 拉取当月异常数据 |
|
||||
| `download_attendance_abnormal` | 仅导出 Excel(无数据返回 None) |
|
||||
| `handle_attendance_abnormal` | 导出 Excel + 分级 + 推送 + 统计汇总 |
|
||||
@@ -88,9 +88,8 @@ return True # 需推送核实
|
||||
|
||||
### 5.4 推送 `push_abnormal`
|
||||
|
||||
- 遍历待推送记录,按姓名(`STAFF_NAME`)生成通知文本。
|
||||
- 遍历待推送记录,按「姓名-工号」分组(同一人员多条异常合并为一条通知),调用 `send_to_person(name, text=text)` 直接推送(app 版主脚本直接调用,不依赖影刀全局变量)。
|
||||
- 姓名缺失时记录告警并跳过。
|
||||
- **当前默认关闭实际发送**:仅 `logger.info` 输出「待推送通知」文本供人工确认;确认无误后取消 `send_to_person(name, text=text)` 注释即可启用。
|
||||
|
||||
### 5.5 汇总统计
|
||||
|
||||
@@ -143,7 +142,7 @@ path = handle_attendance_abnormal(month="2026-08") # 指定整月
|
||||
|
||||
| 能力 | 现状 | 启用方式 |
|
||||
|------|------|----------|
|
||||
| 钉钉实际推送 | 仅打印通知文本 | 取消 `push_abnormal` 中 `send_to_person` 注释 |
|
||||
| 钉钉实际推送 | 已启用(`push_abnormal` 直接调用 `send_to_person`) | 无需配置 |
|
||||
| 考勤页面自动确认异常 | 接口已封装,调用点注释 | 取消 `confirm_abnormal` 注释并按需求接入调用链 |
|
||||
| 异常截图传递当事人 | 未实现 | 需新增截图能力(如页面截图接口)后接入推送 |
|
||||
|
||||
|
||||
+4
-4
@@ -93,9 +93,9 @@ yingdao/
|
||||
### 4.5 dingtalk_sender.py(「37du钉钉推送」)
|
||||
|
||||
- 入口:`main(args)`,`args` 支持两种方式:
|
||||
- **方式A(批量,推荐)**:`{"messages": [{target?, text, display_name?}, ...]}` 逐条发送,返回 `{success, message, sent, failed, results}`
|
||||
- **方式B(单条)**:`{"params": {target, text}}` 或 `{"target", "text"}`
|
||||
- 发送路由:消息**含 `target` → OCR 联系人单聊**;**不含 `target` → 视为群消息,自动改用钉钉群机器人**(调用 `dingtalk_robot`)
|
||||
- **方式A(批量,推荐)**:`{"messages": [{search_keyword?, verify_keyword?, text}, ...]}` 逐条发送,返回 `{success, message, sent, failed, results}`
|
||||
- **方式B(单条)**:`{"params": {search_keyword, verify_keyword, text}}` 或分别传 `search_keyword`/`verify_keyword`/`text` 三个参数
|
||||
- 发送路由:消息**含 `search_keyword` → OCR 联系人单聊**(`verify_keyword` 为会话标题校验关键字,钉钉标题显示姓名,用工号搜索时传姓名);**不含 `search_keyword` → 视为群消息,自动改用钉钉群机器人**(调用 `dingtalk_robot`)
|
||||
- OCR 视觉方案:查找钉钉窗口 → 多策略激活前台 → Ctrl+Shift+F 搜索联系人 → OCR 定位输入框 → 粘贴 → 回车
|
||||
- 依赖:`pyautogui pywin32 pyperclip psutil Pillow numpy rapidocr-onnxruntime`
|
||||
|
||||
@@ -175,7 +175,7 @@ def _set_glv(name, value):
|
||||
|---|---|---|---|
|
||||
| 请假审批 `oa/leave_approval` | `messages` | **文本列表**(按结果类型逐条:通过/校验不通过/人工复核/失败 各一条) | 「37du钉钉群机器人推送」指令 |
|
||||
| 加班审批 `oa/overtime_approval` | `messages` | **文本列表**(按结果类型逐条:通过/校验不通过/人工复核/超期/失败 各一条) | 「37du钉钉群机器人推送」指令 |
|
||||
| 考勤异常 `hr/attendance_abnormal` | `messages` | 字典列表 `[{target, text}, ...]` | 「37du钉钉推送」指令批量模式 |
|
||||
| 考勤异常 `hr/attendance_abnormal` | `messages` | 字典列表 `[{search_keyword, verify_keyword, text}, ...]` | 「37du钉钉推送」指令批量模式 |
|
||||
| 考勤异常月度 `hr/attendance_abnormal_monthly` | `attendance_check_save_path` | 文件路径 | 流程后续处理 |
|
||||
| 缺勤分析 `hr/absence_analysis` | `absence_save_path` | 文件路径 | 流程后续处理 |
|
||||
| 登录指令(oa/hr) | `login_state` | 布尔 | 流程判断是否继续 |
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
往前连续)均没出勤者纳入名单(统计月内已回来出勤的不放入名单);
|
||||
若连续段触及查询月首则一个月一个月往前推(按月全量缓存/单人查询两种策略),
|
||||
直到上一个上班日(跨月追溯)→ 确定请休假起止日期与天数 → 导出 20 列 Excel 名单。
|
||||
历史月数据缓存:考勤/请假「上上月及更早」已彻底月结不会变动,接口原始结果按
|
||||
「月/月+工号」落盘缓存(%LOCALAPPDATA%\37duAutomation\absence_cache),再次运行
|
||||
同月任务命中缓存不重复请求接口;上月/当月仍在校正,始终实时拉取。
|
||||
数据缓存:统计月(上月)始终实时调接口;跨月追溯往前的历史月走懒缓存(先查
|
||||
本地缓存,未命中才调接口,结果按「月/月+工号」落盘到 %LOCALAPPDATA%\37du
|
||||
Automation\absence_cache),下次直接复用;删除缓存文件即强制刷新。
|
||||
@Version: 2.2.0
|
||||
|
||||
场景定位(月度定时任务,建议每月 1 号由影刀调度触发,传上月月份参数):
|
||||
@@ -38,7 +38,6 @@
|
||||
absence_month=(可选)查询月份 "YYYY-MM",默认上月
|
||||
absence_roster_path=(可选)花名册文件路径,默认 D:\资料\人资\门店上月请休假异常人员名单梳理\员工花名册.xlsx
|
||||
(由「37du花名册导出」指令导出的固定文件)
|
||||
absence_no_cache=(可选,默认关闭)置为 1 时跳过历史月缓存读取,强制重新请求接口并刷新缓存
|
||||
|
||||
运行:
|
||||
- 影刀「调用模块」:main({"save_dir": ..., "month": "2026-07", "roster_path": ...})
|
||||
@@ -182,13 +181,11 @@ NOT_BACK_TEXT = "未归岗,时间待定"
|
||||
# 业务脚本只读取该文件,不做浏览器导出、不加载共享模块(影刀指令拖拽机制下无法按文件路径加载)。
|
||||
DEFAULT_ROSTER_PATH = r"D:\资料\人资\请休假异常\员工花名册.xlsx"
|
||||
|
||||
# ---------- 历史月数据缓存(避免重复调用接口) ----------
|
||||
# 考勤/请假一经彻底月结不再变动,把接口返回的原始结果按「月 / 月+工号」落盘缓存,
|
||||
# 再次运行同月任务时直接命中缓存,不再重复请求接口。
|
||||
# 仅缓存「上上月及更早」的月份(上月数据仍可能在校正、当月尚未结束,两者始终实时拉取);
|
||||
# 环境变量 absence_no_cache=1 时跳过缓存读取(强制重新请求接口),新数据仍写回缓存以便刷新。
|
||||
# ---------- 接口数据懒缓存(避免重复调用接口) ----------
|
||||
# 统计月(上月)始终实时调接口;跨月追溯往前的历史月走缓存旁路:
|
||||
# 每次先查本地缓存,没有才调接口,调完按「月 / 月+工号」落盘,下次直接复用;
|
||||
# 缓存以 月/工号 为键互不影响,如需强制刷新删除对应缓存文件即可。
|
||||
CACHE_DIR = os.path.join(_APP_DATA_DIR, "absence_cache")
|
||||
_FORCE_REFRESH = os.getenv("absence_no_cache") == "1"
|
||||
|
||||
# ==================== 登录态(共享 cookies 文件) ====================
|
||||
# 登录态统一由「37duHR登录」影刀自定义指令维护(见 yingdao/share/hr_login.py),
|
||||
@@ -396,23 +393,7 @@ def fetch_all_leave(session, user_id, base_code, start_time, end_time, treecode=
|
||||
return all_rows
|
||||
|
||||
|
||||
# ==================== 历史月数据缓存(按月落盘,命中不重复请求接口) ====================
|
||||
def _month_is_historical(month_key):
|
||||
"""判断月份(YYYYMM 或 YYYY-MM)是否「上上月及更早」。
|
||||
|
||||
上上月及更早的考勤/请假已彻底月结不会变动,才允许走缓存;
|
||||
上月数据仍可能在校正、当月尚未结束,两者始终实时拉取。
|
||||
"""
|
||||
key = str(month_key).replace("-", "")
|
||||
try:
|
||||
m_year, m_mon = int(key[:4]), int(key[4:6])
|
||||
except (TypeError, ValueError, IndexError):
|
||||
return False
|
||||
today = date.today()
|
||||
prev_first = (today.replace(day=1) - timedelta(days=1)).replace(day=1) # 上月第一天
|
||||
return date(m_year, m_mon, 1) < prev_first
|
||||
|
||||
|
||||
# ==================== 接口数据懒缓存(按月落盘,命中不重复请求接口) ====================
|
||||
def _cache_path(kind, month_key, code=""):
|
||||
"""构造缓存文件路径。kind ∈ {att, leave};month_key 为 "YYYYMM";code 空表示全量。"""
|
||||
fname = f"{kind}_{month_key}" + (f"_{code}" if code else "")
|
||||
@@ -420,8 +401,8 @@ def _cache_path(kind, month_key, code=""):
|
||||
|
||||
|
||||
def _read_cache(path):
|
||||
"""读取缓存列表;不存在/损坏/强制刷新返回 None。"""
|
||||
if _FORCE_REFRESH or not os.path.exists(path):
|
||||
"""读取缓存列表;不存在/损坏返回 None。"""
|
||||
if not os.path.exists(path):
|
||||
return None
|
||||
try:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
@@ -443,39 +424,35 @@ def _write_cache(path, records):
|
||||
|
||||
|
||||
def _cached_attendance(session, user_id, month_key, base_code=""):
|
||||
"""按月缓存整月考勤统计报表(历史月命中缓存,不重复请求接口)。
|
||||
"""按月缓存整月考勤统计报表:先查缓存,未命中才调接口并落盘,下次直接复用。
|
||||
|
||||
month_key:月份 "YYYYMM";base_code 非空时为该员工单人报表(跨月追溯单人模式)。
|
||||
"""
|
||||
path = _cache_path("att", month_key, base_code) if _month_is_historical(month_key) else None
|
||||
if path is not None:
|
||||
cached = _read_cache(path)
|
||||
if cached is not None:
|
||||
logger.info("命中考勤报表缓存:%s(%d 条)", os.path.basename(path), len(cached))
|
||||
return cached
|
||||
path = _cache_path("att", month_key, base_code)
|
||||
cached = _read_cache(path)
|
||||
if cached is not None:
|
||||
logger.info("命中考勤报表缓存:%s(%d 条)", os.path.basename(path), len(cached))
|
||||
return cached
|
||||
records = fetch_all_attendance_stat(session, user_id, month_key, base_code=base_code)
|
||||
if path is not None:
|
||||
_write_cache(path, records)
|
||||
_write_cache(path, records)
|
||||
return records
|
||||
|
||||
|
||||
def _cached_leave(session, user_id, base_code, month_str):
|
||||
"""按月缓存整月请假记录(历史月命中缓存,不重复请求接口)。
|
||||
"""按月缓存整月请假记录:先查缓存,未命中才调接口并落盘,下次直接复用。
|
||||
|
||||
month_str:月份 "YYYY-MM";base_code 空为全集团,非空为该员工(跨月追溯单人模式)。
|
||||
"""
|
||||
month_key = month_str.replace("-", "")
|
||||
path = _cache_path("leave", month_key, base_code) if _month_is_historical(month_key) else None
|
||||
if path is not None:
|
||||
cached = _read_cache(path)
|
||||
if cached is not None:
|
||||
logger.info("命中请假记录缓存:%s(%d 条)", os.path.basename(path), len(cached))
|
||||
return cached
|
||||
path = _cache_path("leave", month_key, base_code)
|
||||
cached = _read_cache(path)
|
||||
if cached is not None:
|
||||
logger.info("命中请假记录缓存:%s(%d 条)", os.path.basename(path), len(cached))
|
||||
return cached
|
||||
mb, me = _month_range(month_str)
|
||||
records = fetch_all_leave(session, user_id, base_code,
|
||||
mb.strftime("%Y-%m-%d"), me.strftime("%Y-%m-%d"))
|
||||
if path is not None:
|
||||
_write_cache(path, records)
|
||||
_write_cache(path, records)
|
||||
return records
|
||||
|
||||
|
||||
@@ -828,8 +805,11 @@ def _latest_leave_end(session, user_id, code, begin, end):
|
||||
|
||||
情况A(月末仍缺席)判定:仅当存在请假截止日期 > 统计期最后一天(请假仍在进行/跨出统计期)
|
||||
时,以其截止日作为请休假结束日期;否则视为未归岗(返回 None,外部填「未归岗,时间待定」)。
|
||||
统计月始终实时调接口,不走缓存。
|
||||
"""
|
||||
leaves = _cached_leave(session, user_id, code, begin.strftime("%Y-%m"))
|
||||
leaves = fetch_all_leave(
|
||||
session, user_id, code, begin.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d")
|
||||
)
|
||||
ongoing = [d for r in leaves if (d := _parse_date(r.get("end_time"))) and d > end]
|
||||
if not ongoing:
|
||||
return None
|
||||
@@ -1001,14 +981,16 @@ def export_absence_list(save_dir=None, roster_path=None, month=None):
|
||||
session, user_id = _validate_session(session)
|
||||
|
||||
# 3. 拉取上月全集团考勤统计报表,按报表判定「休息/旷工」,再并入整天请假(leave_days>=1)
|
||||
# 统计月(上月)始终实时调接口,不走缓存;跨月追溯的历史月才走懒缓存。
|
||||
seen = {}
|
||||
records = _cached_attendance(session, user_id, month_str)
|
||||
records = fetch_all_attendance_stat(session, user_id, month_str)
|
||||
logger.info("考勤统计报表共 %d 人", len(records))
|
||||
if not records:
|
||||
logger.error("考勤统计报表无数据,请检查月份或权限")
|
||||
return ""
|
||||
month_status = _report_records_to_status(records, begin, end, seen)
|
||||
leaves = _cached_leave(session, user_id, "", begin.strftime("%Y-%m"))
|
||||
leaves = fetch_all_leave(
|
||||
session, user_id, "", begin.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d"))
|
||||
_merge_leave_status(month_status, leaves, begin, end)
|
||||
logger.info("全集团请假记录 %d 条(整天请假已并入缺勤判定)", len(leaves))
|
||||
_log_statuses(seen)
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
- 本脚本不内联任何发送逻辑,也不 import 共享推送模块;
|
||||
- 运行后会同步把待推送的 messages(字典列表)写入影刀全局变量
|
||||
glv['messages'](无数据时置为空列表),由流程拖「37du钉钉推送」指令
|
||||
完成发送(含 target 走 OCR 单聊,不含 target 走群机器人);
|
||||
完成发送(含 search_keyword 走 OCR 单聊,不含则走群机器人);
|
||||
- 处理汇总报表文本写入全局变量 glv['report'],由流程推送给 人资专用号。
|
||||
|
||||
依赖安装(影刀版启动校验:所有库必须预先装齐,任一缺失启动即报错退出,不懒加载):
|
||||
@@ -131,6 +131,16 @@ DINGTALK_PUSH_ENABLED = "true"
|
||||
# 人资专用号:处理汇总报表推送对象;未配置则跳过汇总推送
|
||||
DINGTALK_NOTIFY_TARGET = "夏一平"
|
||||
|
||||
# ===== 联调测试模式(临时,联调完成后请改回 False) =====
|
||||
# 启用时:仅推送前 PUSH_TEST_LIMIT 条当事人消息,且全部重定向到测试接收人,
|
||||
# 避免联调期间真的给真实员工发钉钉消息。
|
||||
# search_keyword 为钉钉搜索关键字(用工号避免同名误选);verify_keyword 为会话标题校验关键字(姓名)。
|
||||
PUSH_TEST_MODE = True
|
||||
PUSH_TEST_LIMIT = 10
|
||||
PUSH_TEST_SEARCH_KEYWORD = "37036361" # 测试接收人工号(搜索关键字)
|
||||
PUSH_TEST_VERIFY_KEYWORD = "夏一平" # 测试接收人姓名(会话标题校验关键字)
|
||||
# ======================================================
|
||||
|
||||
|
||||
# ==================== 登录态(共享 cookies 文件) ====================
|
||||
# 登录态统一由「37duHR登录」影刀自定义指令维护(见 yingdao/share/hr_login.py),
|
||||
@@ -353,8 +363,8 @@ def export_excel(data, save_dir, month_str):
|
||||
def push_abnormal(items):
|
||||
"""按「工号-姓名」分组,合并为 messages 字典列表,写入影刀全局变量 messages,
|
||||
由流程拖「37du钉钉推送」指令发送(本脚本不内联发送,保持单文件自包含)。
|
||||
推送目标(target)使用工号作为钉钉搜索关键字,避免同名人员误发;
|
||||
会话标题校验名(display_name)传姓名(钉钉标题显示姓名而非工号)。
|
||||
search_keyword(搜索关键字)使用工号,避免同名人员误发;
|
||||
verify_keyword(会话标题校验关键字)传姓名(钉钉标题显示姓名而非工号)。
|
||||
返回 (待推送条数, 失败数=0)。
|
||||
"""
|
||||
groups = {}
|
||||
@@ -366,10 +376,20 @@ def push_abnormal(items):
|
||||
key = (it.get("base_code", "") or "", name)
|
||||
groups.setdefault(key, []).append(it)
|
||||
|
||||
messages = [
|
||||
{"target": _code or _name, "display_name": _name, "text": build_notify_text(group)}
|
||||
for (_code, _name), group in groups.items()
|
||||
]
|
||||
# 联调测试模式:仅推送前 N 条,且全部重定向到测试接收人
|
||||
pairs = list(groups.items())
|
||||
if PUSH_TEST_MODE:
|
||||
pairs = pairs[:PUSH_TEST_LIMIT]
|
||||
logger.warning("【联调测试模式】仅推送前 %d 条,全部重定向到 %s(%s)(不打扰真实员工)",
|
||||
len(pairs), PUSH_TEST_VERIFY_KEYWORD, PUSH_TEST_SEARCH_KEYWORD)
|
||||
|
||||
messages = []
|
||||
for (_code, _name), group in pairs:
|
||||
if PUSH_TEST_MODE:
|
||||
search_keyword, verify_keyword = PUSH_TEST_SEARCH_KEYWORD, PUSH_TEST_VERIFY_KEYWORD
|
||||
else:
|
||||
search_keyword, verify_keyword = _code or _name, _name
|
||||
messages.append({"search_keyword": search_keyword, "verify_keyword": verify_keyword, "text": build_notify_text(group)})
|
||||
# 写入影刀全局变量,供流程「37du钉钉推送」指令读取(无数据也置空,避免 NameError)
|
||||
_set_glv('messages', messages)
|
||||
logger.info("当事人消息已生成并写入全局变量 messages:共 %d 条(由流程推送)", len(messages))
|
||||
|
||||
@@ -19,9 +19,7 @@
|
||||
- 本脚本不内联任何消息推送逻辑;仅生成待推送的文本消息,经 main() 写入影刀全局变量
|
||||
messages(列表,每项一条消息,按结果类型分组:通过/校验不通过/人工复核/失败 各一条),
|
||||
由「37du钉钉群机器人推送」指令读取后逐条发送;本地独立运行时静默跳过写全局变量;
|
||||
- main(push_type, target):
|
||||
push_type="group"(默认)群消息:全局变量 messages 为文本列表,逐条交钉钉群机器人 webhook 推送;
|
||||
push_type="person" 单聊:main 返回值仍为 {target, text} 列表,可交给「37du钉钉推送」指令走 OCR 单聊。
|
||||
- main():全局变量 messages 为文本列表,逐条交钉钉群机器人 webhook 推送。
|
||||
"""
|
||||
|
||||
# ==================== 标准库导入 ====================
|
||||
@@ -1098,18 +1096,17 @@ def show_rpa_result_popup(page: ChromiumPage,
|
||||
|
||||
|
||||
# ==================== 推送消息生成(不发送,交由消息推送模块) ====================
|
||||
# 本脚本不发送任何消息:仅把审批结果拼成文本消息(默认群消息,只含 text;
|
||||
# 传 push_type="person" 时生成含 target 的单聊消息),经 main() 把纯文本写入影刀全局变量 messages。
|
||||
# 本脚本不发送任何消息:仅把审批结果拼成文本消息(群消息,只含 text),
|
||||
# 经 main() 把纯文本写入影刀全局变量 messages。
|
||||
|
||||
|
||||
def _build_push_messages(texts, push_type="group", target=None):
|
||||
def _build_push_messages(texts):
|
||||
"""把推送内容列表转成推送消息列表(字典列表)。
|
||||
|
||||
texts 项可为:
|
||||
- 纯文本:包成 {"text": ...}(单条消息)
|
||||
- 块格式分组 dict(含 blocks):原样保留,交由钉钉机器人模块内部分批推送
|
||||
push_type 推送类型:"group" 群消息(钉钉群机器人 webhook 推送);
|
||||
"person" 单聊(含 target,钉钉联系人 OCR 推送)。
|
||||
仅生成群消息({"text": ...},钉钉群机器人 webhook 推送)。
|
||||
"""
|
||||
messages = []
|
||||
for text in texts:
|
||||
@@ -1117,8 +1114,6 @@ def _build_push_messages(texts, push_type="group", target=None):
|
||||
msg = dict(text)
|
||||
else:
|
||||
msg = {"text": text}
|
||||
if push_type == "person":
|
||||
msg["target"] = target
|
||||
messages.append(msg)
|
||||
return messages
|
||||
|
||||
@@ -1199,6 +1194,16 @@ def build_push_summary(results, subject=""):
|
||||
_push_group(f"【{subject}审批】审批异常", abnormal)
|
||||
_push_group(f"【{subject}审批】处理失败", failed)
|
||||
|
||||
# 汇总消息:本次审批各类别数量(一条,含 0 的类别)
|
||||
summary_lines = [f"本次共审批 **{len(results)}** 条:"]
|
||||
for label, cnt in [("通过", len(passed)),
|
||||
("校验不通过未处理", len(rejected)),
|
||||
("需人工复核", len(review)),
|
||||
("审批异常", len(abnormal)),
|
||||
("处理失败", len(failed))]:
|
||||
summary_lines.append(f"- {label}:{cnt} 条")
|
||||
messages.append({"title": f"【{subject}审批】汇总", "blocks": ["\n".join(summary_lines)]})
|
||||
|
||||
return messages
|
||||
|
||||
|
||||
@@ -1284,11 +1289,8 @@ def submit_approval(page, decision):
|
||||
return {"action": label, "ok": "false", "message": f"审批提交[{label}]失败: {e}"}
|
||||
|
||||
|
||||
def run_flow(subject, validate_doc, generate_report, push_type="group", target=None):
|
||||
"""请假审批主流程;返回生成的推送消息列表(字典列表,本脚本不发送)。
|
||||
|
||||
push_type 推送类型:"group" 群消息(钉钉群机器人 webhook);"person" 单聊(钉钉联系人 OCR 推送)。
|
||||
target 单聊推送目标(联系人),仅 push_type="person" 时使用。
|
||||
def run_flow(subject, validate_doc, generate_report):
|
||||
"""请假审批主流程;返回生成的推送消息列表(字典列表,群消息,本脚本不发送)。
|
||||
"""
|
||||
logger.info("%s审批流程开始", subject)
|
||||
|
||||
@@ -1305,7 +1307,7 @@ def run_flow(subject, validate_doc, generate_report, push_type="group", target=N
|
||||
# 无待办单据:不开浏览器,直接生成提示消息返回
|
||||
if not docs:
|
||||
logger.info("%s无待审批单据,跳过浏览器操作", subject)
|
||||
push_messages = _build_push_messages([f"暂无待审批的{subject}单据"], push_type, target)
|
||||
push_messages = _build_push_messages([f"暂无待审批的{subject}单据"])
|
||||
logger.info("%s审批流程结束,共处理 0 条(无单据)", subject)
|
||||
return push_messages
|
||||
|
||||
@@ -1370,7 +1372,7 @@ def run_flow(subject, validate_doc, generate_report, push_type="group", target=N
|
||||
# 无任何待办/结果时也生成一条提示消息,避免静默不推送
|
||||
if not texts:
|
||||
texts.append(f"暂无待审批的{subject}单据")
|
||||
push_messages = _build_push_messages(texts, push_type, target)
|
||||
push_messages = _build_push_messages(texts)
|
||||
|
||||
logger.info("%s审批流程结束,共处理 %d 条", subject, len(results))
|
||||
return push_messages
|
||||
@@ -1745,25 +1747,16 @@ def generate_report(results):
|
||||
|
||||
|
||||
# ==================== 主入口 ====================
|
||||
def main(push_type="group", target=None):
|
||||
def main():
|
||||
"""请假审批:登录 -> 查待办 -> 解析SSO -> 获取表单 -> 校验 -> 报告 -> 生成推送消息。
|
||||
|
||||
push_type 推送类型:"group" 群消息(缺省,写入全局变量 messages 为纯文本,
|
||||
直接交给钉钉群机器人 webhook 推送);
|
||||
"person" 单聊(钉钉联系人 OCR 推送,需配合 target)
|
||||
target 单聊推送目标(联系人),仅 push_type="person" 时使用
|
||||
仅生成群消息:写入全局变量 messages 为纯文本,直接交给钉钉群机器人 webhook 推送。
|
||||
返回:推送消息列表(字典列表,本脚本不发送);同时把纯文本写入全局变量 messages。
|
||||
"""
|
||||
push_type = str(push_type or "group").lower()
|
||||
if push_type == "person" and not target:
|
||||
logger.warning("push_type=person 但未提供 target,已按群消息(group)处理")
|
||||
push_type = "group"
|
||||
push_messages = run_flow(
|
||||
subject="请假",
|
||||
validate_doc=validate_doc,
|
||||
generate_report=generate_report,
|
||||
push_type=push_type,
|
||||
target=target,
|
||||
)
|
||||
# 写入影刀全局变量 messages(块格式分组列表:{title, blocks}),
|
||||
# 供「37du钉钉群机器人推送」指令读取后整体传入 main(),由模块内部分批逐条发送;
|
||||
|
||||
@@ -19,9 +19,7 @@
|
||||
- 本脚本不内联任何消息推送逻辑;仅生成待推送的文本消息,经 main() 写入影刀全局变量
|
||||
messages(列表,每项一条消息,按结果类型分组:通过/校验不通过/人工复核/超期/失败 各一条),
|
||||
由「37du钉钉群机器人推送」指令读取后逐条发送;本地独立运行时静默跳过写全局变量;
|
||||
- main(push_type, target):
|
||||
push_type="group"(默认)群消息:全局变量 messages 为文本列表,逐条交钉钉群机器人 webhook 推送;
|
||||
push_type="person" 单聊:main 返回值仍为 {target, text} 列表,可交给「37du钉钉推送」指令走 OCR 单聊。
|
||||
- main():全局变量 messages 为文本列表,逐条交钉钉群机器人 webhook 推送。
|
||||
"""
|
||||
|
||||
# ==================== 标准库导入 ====================
|
||||
@@ -777,18 +775,17 @@ def show_rpa_result_popup(page: ChromiumPage,
|
||||
|
||||
|
||||
# ==================== 推送消息生成(不发送,交由推送指令) ====================
|
||||
# 本脚本不发送任何消息:仅把审批结果按类型拼成多条文本消息(默认群消息,只含 text;
|
||||
# 传 push_type="person" 时生成含 target 的单聊消息),经 main() 把列表写入影刀全局变量 messages。
|
||||
# 本脚本不发送任何消息:仅把审批结果按类型拼成多条文本消息(群消息,只含 text),
|
||||
# 经 main() 把列表写入影刀全局变量 messages。
|
||||
|
||||
|
||||
def _build_push_messages(texts, push_type="group", target=None):
|
||||
def _build_push_messages(texts):
|
||||
"""把推送内容列表转成推送消息列表(字典列表)。
|
||||
|
||||
texts 项可为:
|
||||
- 纯文本:包成 {"text": ...}(单条消息)
|
||||
- 块格式分组 dict(含 blocks):原样保留,交由钉钉机器人模块内部分批推送
|
||||
push_type 推送类型:"group" 群消息(钉钉群机器人 webhook 推送);
|
||||
"person" 单聊(含 target,钉钉联系人 OCR 推送)。
|
||||
仅生成群消息({"text": ...},钉钉群机器人 webhook 推送)。
|
||||
"""
|
||||
messages = []
|
||||
for text in texts:
|
||||
@@ -796,8 +793,6 @@ def _build_push_messages(texts, push_type="group", target=None):
|
||||
msg = dict(text)
|
||||
else:
|
||||
msg = {"text": text}
|
||||
if push_type == "person":
|
||||
msg["target"] = target
|
||||
messages.append(msg)
|
||||
return messages
|
||||
|
||||
@@ -872,6 +867,17 @@ def build_push_summary(results, subject=""):
|
||||
_push_group(f"【{subject}审批】审批异常", abnormal)
|
||||
_push_group(f"【{subject}审批】处理失败", failed)
|
||||
|
||||
# 汇总消息:本次审批各类别数量(一条,含 0 的类别)
|
||||
summary_lines = [f"本次共审批 **{len(results)}** 条:"]
|
||||
for label, cnt in [("通过", len(passed)),
|
||||
("校验不通过未处理", len(rejected)),
|
||||
("需人工复核", len(review)),
|
||||
("超期待人资处理", len(overdue)),
|
||||
("审批异常", len(abnormal)),
|
||||
("处理失败", len(failed))]:
|
||||
summary_lines.append(f"- {label}:{cnt} 条")
|
||||
messages.append({"title": f"【{subject}审批】汇总", "blocks": ["\n".join(summary_lines)]})
|
||||
|
||||
return messages
|
||||
|
||||
def _find_button(page, selector, timeout=3.0):
|
||||
@@ -956,11 +962,8 @@ def submit_approval(page, decision):
|
||||
return {"action": label, "ok": "false", "message": f"审批提交[{label}]失败: {e}"}
|
||||
|
||||
|
||||
def run_flow(subject, validate_doc, generate_report, push_type="group", target=None):
|
||||
"""加班审批主流程;返回生成的推送消息列表(字典列表,本脚本不发送)。
|
||||
|
||||
push_type 推送类型:"group" 群消息(钉钉群机器人 webhook);"person" 单聊(钉钉联系人 OCR 推送)。
|
||||
target 单聊推送目标(联系人),仅 push_type="person" 时使用。
|
||||
def run_flow(subject, validate_doc, generate_report):
|
||||
"""加班审批主流程;返回生成的推送消息列表(字典列表,群消息,本脚本不发送)。
|
||||
"""
|
||||
logger.info("%s审批流程开始", subject)
|
||||
|
||||
@@ -977,7 +980,7 @@ def run_flow(subject, validate_doc, generate_report, push_type="group", target=N
|
||||
# 无待办单据:不开浏览器,直接生成提示消息返回
|
||||
if not docs:
|
||||
logger.info("%s无待审批单据,跳过浏览器操作", subject)
|
||||
push_messages = _build_push_messages([f"暂无待审批的{subject}单据"], push_type, target)
|
||||
push_messages = _build_push_messages([f"暂无待审批的{subject}单据"])
|
||||
logger.info("%s审批流程结束,共处理 0 条(无单据)", subject)
|
||||
return push_messages
|
||||
|
||||
@@ -1038,7 +1041,7 @@ def run_flow(subject, validate_doc, generate_report, push_type="group", target=N
|
||||
# 无任何待办/结果时也生成一条提示消息,避免静默不推送
|
||||
if not texts:
|
||||
texts.append(f"暂无待审批的{subject}单据")
|
||||
push_messages = _build_push_messages(texts, push_type, target)
|
||||
push_messages = _build_push_messages(texts)
|
||||
|
||||
logger.info("%s审批流程结束,共处理 %d 条", subject, len(results))
|
||||
return push_messages
|
||||
@@ -1729,27 +1732,16 @@ def generate_report(results):
|
||||
|
||||
|
||||
# ==================== 主入口 ====================
|
||||
def main(push_type="group", target=None):
|
||||
"""加班审批:查待办 -> 解析SSO -> 获取表单 -> 校验 -> 报告 -> 生成推送消息。...
|
||||
def main():
|
||||
"""加班审批:查待办 -> 解析SSO -> 获取表单 -> 校验 -> 报告 -> 生成推送消息。
|
||||
|
||||
本脚本不发送任何消息:仅生成待推送消息(返回列表并写入影刀全局变量 messages),
|
||||
由「37du钉钉群机器人推送」/「37du钉钉推送」指令消费。
|
||||
|
||||
Args:
|
||||
push_type: 推送类型 "group"(默认,群消息,全局变量 messages 为纯文本直接发送)
|
||||
/ "person"(单聊,返回列表含 target,交由 OCR 单聊推送指令)
|
||||
target: 单聊推送目标(联系人),仅 push_type="person" 时使用
|
||||
由「37du钉钉群机器人推送」指令消费;仅生成群消息。
|
||||
"""
|
||||
push_type = str(push_type or "group").lower()
|
||||
if push_type == "person" and not target:
|
||||
logger.warning("push_type=person 但未提供 target,已按群消息(group)处理")
|
||||
push_type = "group"
|
||||
push_messages = run_flow(
|
||||
subject="加班",
|
||||
validate_doc=validate_doc,
|
||||
generate_report=generate_report,
|
||||
push_type=push_type,
|
||||
target=target,
|
||||
)
|
||||
# 写入影刀全局变量 messages(块格式分组列表:{title, blocks}),
|
||||
# 供「37du钉钉群机器人推送」指令读取后整体传入 main(),由模块内部分批逐条发送;
|
||||
|
||||
@@ -29,9 +29,7 @@
|
||||
- 本脚本不内联任何消息推送逻辑;仅生成待推送的文本消息,经 main() 写入影刀全局变量
|
||||
messages(列表,仅一条汇总消息:本次审批通过/未通过各多少个,不含单笔明细),
|
||||
由「37du钉钉群机器人推送」指令读取后发送;本地独立运行时静默跳过写全局变量;
|
||||
- main(push_type, target):
|
||||
push_type="group"(默认)群消息:全局变量 messages 为文本列表,逐条交钉钉群机器人 webhook 推送;
|
||||
push_type="person" 单聊:main 返回值仍为 {target, text} 列表,可交给「37du钉钉推送」指令走 OCR 单聊。
|
||||
- main():全局变量 messages 为文本列表,逐条交钉钉群机器人 webhook 推送。
|
||||
"""
|
||||
|
||||
# ==================== 标准库导入 ====================
|
||||
@@ -671,26 +669,18 @@ def show_rpa_result_popup(page: ChromiumPage,
|
||||
|
||||
|
||||
# ==================== 推送消息生成(不发送,交由推送指令) ====================
|
||||
# 本脚本不发送任何消息:仅把审批结果拼成一条汇总文本消息(默认群消息,只含 text;
|
||||
# 传 push_type="person" 时生成含 target 的单聊消息),经 main() 把列表写入影刀全局变量 messages。
|
||||
# 本脚本不发送任何消息:仅把审批结果拼成一条汇总文本消息(群消息,只含 text),
|
||||
# 经 main() 把列表写入影刀全局变量 messages。
|
||||
|
||||
|
||||
# ==================== 推送消息生成(不发送,交由推送指令) ====================
|
||||
# 本脚本不发送任何消息:仅把审批结果拼成一条汇总文本消息(默认群消息,只含 text;
|
||||
# 传 push_type="person" 时生成含 target 的单聊消息),经 main() 把列表写入影刀全局变量 messages。
|
||||
|
||||
|
||||
def _build_push_messages(texts, push_type="group", target=None):
|
||||
def _build_push_messages(texts):
|
||||
"""把文本列表转成推送消息列表(字典列表)。
|
||||
|
||||
push_type 推送类型:"group" 群消息(仅 text,钉钉群机器人 webhook 推送);
|
||||
"person" 单聊(含 target,钉钉联系人 OCR 推送)。
|
||||
仅生成群消息({"text": ...},钉钉群机器人 webhook 推送)。
|
||||
"""
|
||||
messages = []
|
||||
for text in texts:
|
||||
msg = {"text": text}
|
||||
if push_type == "person":
|
||||
msg["target"] = target
|
||||
messages.append(msg)
|
||||
return messages
|
||||
|
||||
@@ -799,11 +789,8 @@ def submit_approval(page, decision):
|
||||
return {"action": label, "ok": "false", "message": f"审批提交[{label}]失败: {e}"}
|
||||
|
||||
|
||||
def run_flow(subject, validate_doc, generate_report, push_type="group", target=None):
|
||||
"""离职电子签审批主流程;返回生成的推送消息列表(字典列表,本脚本不发送)。
|
||||
|
||||
push_type 推送类型:"group" 群消息(钉钉群机器人 webhook);"person" 单聊(钉钉联系人 OCR 推送)。
|
||||
target 单聊推送目标(联系人),仅 push_type="person" 时使用。
|
||||
def run_flow(subject, validate_doc, generate_report):
|
||||
"""离职电子签审批主流程;返回生成的推送消息列表(字典列表,群消息,本脚本不发送)。
|
||||
"""
|
||||
logger.info("%s审批流程开始", subject)
|
||||
|
||||
@@ -820,7 +807,7 @@ def run_flow(subject, validate_doc, generate_report, push_type="group", target=N
|
||||
# 无待办单据:不开浏览器,直接生成提示消息返回
|
||||
if not docs:
|
||||
logger.info("%s无待审批单据,跳过浏览器操作", subject)
|
||||
push_messages = _build_push_messages([f"暂无待审批的{subject}单据"], push_type, target)
|
||||
push_messages = _build_push_messages([f"暂无待审批的{subject}单据"])
|
||||
logger.info("%s审批流程结束,共处理 0 条(无单据)", subject)
|
||||
return push_messages
|
||||
|
||||
@@ -881,7 +868,7 @@ def run_flow(subject, validate_doc, generate_report, push_type="group", target=N
|
||||
# 无任何待办/结果时也生成一条提示消息,避免静默不推送
|
||||
if not texts:
|
||||
texts.append(f"暂无待审批的{subject}单据")
|
||||
push_messages = _build_push_messages(texts, push_type, target)
|
||||
push_messages = _build_push_messages(texts)
|
||||
|
||||
logger.info("%s审批流程结束,共处理 %d 条", subject, len(results))
|
||||
return push_messages
|
||||
@@ -1057,27 +1044,16 @@ def generate_report(results):
|
||||
|
||||
|
||||
# ==================== 主入口 ====================
|
||||
def main(push_type="group", target=None):
|
||||
def main():
|
||||
"""改签离职电子签流程审核:查待办 -> 解析SSO -> 获取表单 -> 校验 -> 报告 -> 生成推送消息。
|
||||
|
||||
本脚本不发送任何消息:仅生成待推送消息(返回列表并写入影刀全局变量 messages),
|
||||
由「37du钉钉群机器人推送」/「37du钉钉推送」指令消费。
|
||||
|
||||
Args:
|
||||
push_type: 推送类型 "group"(默认,群消息,全局变量 messages 为纯文本直接发送)
|
||||
/ "person"(单聊,返回列表含 target,交由 OCR 单聊推送指令)
|
||||
target: 单聊推送目标(联系人),仅 push_type="person" 时使用
|
||||
由「37du钉钉群机器人推送」指令消费;仅生成群消息。
|
||||
"""
|
||||
push_type = str(push_type or "group").lower()
|
||||
if push_type == "person" and not target:
|
||||
logger.warning("push_type=person 但未提供 target,已按群消息(group)处理")
|
||||
push_type = "group"
|
||||
push_messages = run_flow(
|
||||
subject="改签离职电子签流程",
|
||||
validate_doc=validate_doc,
|
||||
generate_report=generate_report,
|
||||
push_type=push_type,
|
||||
target=target,
|
||||
)
|
||||
# 写入影刀全局变量 messages(列表,每项一条消息,按结果类型分组),
|
||||
# 供「37du钉钉群机器人推送」指令读取后逐条发送;本地独立运行时静默跳过
|
||||
|
||||
@@ -11,12 +11,12 @@
|
||||
如何在影刀中创建指令:
|
||||
1. 影刀客户端 → 新建「PC自动化指令」→ 指令编码如 dingtalk_sender,添加 Python 模块;
|
||||
2. 把本文件内容粘贴进指令的 Python 模块(或直接以本文件作为模块源码);
|
||||
3. 增加流程参数 params(字典类型,dict),或分别增加 target/text 两个参数;
|
||||
3. 增加流程参数 params(字典类型,dict),或分别增加 search_keyword/verify_keyword/text 三个参数;
|
||||
4. 保存并「发版」;在需要钉钉推送的应用中「获取指令」并安装;
|
||||
5. 在流程中拖入该指令,把业务脚本输出的参数传入(两种方式任选):
|
||||
方式A(推荐):流程中先拼一个字典变量(如 {"target": "张三", "text": "..."}),
|
||||
方式A(推荐):流程中先拼一个字典变量(如 {"search_keyword": "10086", "verify_keyword": "张三", "text": "..."}),
|
||||
作为 params 整体传入;
|
||||
方式B:分别传 target、text 变量,指令内部自动拼成字典;
|
||||
方式B:分别传 search_keyword、verify_keyword、text 变量,指令内部自动拼成字典;
|
||||
指令返回 JSON:{"success": bool, "message": str},可在后续流程中判断是否重试。
|
||||
|
||||
推送原理(OCR 视觉方案,与 app/utils/dingtalk_sender.py 一致):
|
||||
@@ -30,16 +30,16 @@
|
||||
入口:
|
||||
main(args) 影刀自定义指令调用入口
|
||||
args = {
|
||||
"target": "联系人搜索关键字(支持工号,避免同名误选)",
|
||||
"text": "要发送的文本内容",
|
||||
"display_name": "会话标题校验名(可选;钉钉标题显示姓名,target 用工号时传姓名校验)",
|
||||
"search_keyword": "联系人搜索关键字(支持工号,避免同名误选)",
|
||||
"verify_keyword": "会话校验关键字(钉钉标题显示姓名,用工号搜索时传姓名校验)",
|
||||
"text": "要发送的消息内容",
|
||||
}
|
||||
通过流程参数传入(支持整字典 params 或分别传 target/text)
|
||||
messages 批量模式下,消息含 target 走 OCR 单聊;不含 target 视为群消息,
|
||||
通过流程参数传入(支持整字典 params 或分别传 search_keyword/verify_keyword/text)
|
||||
messages 批量模式下,消息含 search_keyword 走 OCR 单聊;不含则视为群消息,
|
||||
自动改由钉钉群机器人(dingtalk_robot)webhook 推送。
|
||||
返回 JSON 安全 dict:{"success": bool, "message": str}
|
||||
|
||||
python dingtalk_sender.py 目标 文本 独立运行(等价于 main({"target": ..., "text": ...}))
|
||||
python dingtalk_sender.py 搜索关键词 消息内容 独立运行(等价于 main({"search_keyword": ..., "text": ...}))
|
||||
"""
|
||||
import sys
|
||||
import time
|
||||
@@ -267,12 +267,12 @@ def _dt_is_chat_opened(hwnd, target_name):
|
||||
return False
|
||||
|
||||
|
||||
def _dt_search_and_open(hwnd, target_name, verify_name=None):
|
||||
def _dt_search_and_open(hwnd, search_keyword, verify_keyword=None):
|
||||
"""搜索并打开指定联系人的会话(仅单聊;群消息改用钉钉群机器人 webhook 推送)。
|
||||
|
||||
target_name 为搜索关键字(支持工号,避免同名人员误选,如搜索 10086);
|
||||
verify_name 为打开会话后校验标题用的名称(钉钉会话标题显示的是联系人姓名
|
||||
而非工号,故同名人场景需传姓名用于校验;不传则回退为 target_name)。
|
||||
search_keyword 为搜索关键字(支持工号,避免同名人员误选,如搜索 10086);
|
||||
verify_keyword 为打开会话后校验标题用的关键字(钉钉会话标题显示的是联系人姓名
|
||||
而非工号,故同名人场景需传姓名用于校验;不传则回退为 search_keyword)。
|
||||
"""
|
||||
_dt_activate_window(hwnd)
|
||||
for attempt in range(_DT_SEARCH_RETRY):
|
||||
@@ -280,11 +280,11 @@ def _dt_search_and_open(hwnd, target_name, verify_name=None):
|
||||
time.sleep(0.8)
|
||||
pyautogui.hotkey('ctrl', 'a')
|
||||
pyautogui.press('delete')
|
||||
pyperclip.copy(target_name)
|
||||
pyperclip.copy(search_keyword)
|
||||
pyautogui.hotkey('ctrl', 'v')
|
||||
time.sleep(0.5)
|
||||
pyautogui.press('enter')
|
||||
logger.info("搜索 '%s' (尝试 %d)", target_name, attempt + 1)
|
||||
logger.info("搜索 '%s' (尝试 %d)", search_keyword, attempt + 1)
|
||||
time.sleep(0.8)
|
||||
if _dt_wait_for_tab(hwnd, "联系人", timeout=3):
|
||||
break
|
||||
@@ -316,12 +316,12 @@ def _dt_search_and_open(hwnd, target_name, verify_name=None):
|
||||
|
||||
start = time.time()
|
||||
while time.time() - start < _DT_TIMEOUT:
|
||||
if _dt_is_chat_opened(hwnd, verify_name or target_name):
|
||||
logger.info("成功进入会话 '%s'", verify_name or target_name)
|
||||
if _dt_is_chat_opened(hwnd, verify_keyword or search_keyword):
|
||||
logger.info("成功进入会话 '%s'", verify_keyword or search_keyword)
|
||||
return True
|
||||
_dt_activate_window(hwnd)
|
||||
time.sleep(0.5)
|
||||
logger.warning("未检测到会话名 '%s'", verify_name or target_name)
|
||||
logger.warning("未检测到会话名 '%s'", verify_keyword or search_keyword)
|
||||
return False
|
||||
|
||||
|
||||
@@ -464,10 +464,10 @@ def _dt_find_hwnd_or_raise():
|
||||
return hwnd
|
||||
|
||||
|
||||
def _dt_send_text_to_person(name, text, display_name=None):
|
||||
def _dt_send_text_to_person(search_keyword, text, verify_keyword=None):
|
||||
"""向指定联系人发送文本(OCR 视觉方案):查找窗口→多策略激活→搜索打开会话→定位输入框→粘贴发送。
|
||||
name 为搜索关键字(支持工号);display_name 为会话标题校验名(钉钉标题显示姓名,
|
||||
同名人时传姓名用于校验;不传则用 name 校验),二者分离避免同名误选。
|
||||
search_keyword 为搜索关键字(支持工号);verify_keyword 为会话标题校验关键字(钉钉标题显示姓名,
|
||||
同名人时传姓名校验;不传则用 search_keyword 校验),二者分离避免同名误选。
|
||||
"""
|
||||
if not text or not text.strip():
|
||||
raise ValueError("text 不能为空")
|
||||
@@ -478,7 +478,7 @@ def _dt_send_text_to_person(name, text, display_name=None):
|
||||
_dt_ensure_activate_dingtalk(hwnd, target_name=None, timeout=10)
|
||||
hwnd = _dt_find_dingtalk_window(include_hidden=True) or hwnd
|
||||
|
||||
if not _dt_search_and_open(hwnd, name, display_name):
|
||||
if not _dt_search_and_open(hwnd, search_keyword, verify_keyword):
|
||||
raise RuntimeError("打开联系人会话失败")
|
||||
|
||||
_dt_focus_input_box(hwnd)
|
||||
@@ -501,27 +501,27 @@ def _dt_send_text_to_person(name, text, display_name=None):
|
||||
raise RuntimeError("重试 %d 次后失败" % _DT_RETRY_TIMES)
|
||||
|
||||
|
||||
def send_dingtalk_text(target, text, display_name=None):
|
||||
def send_dingtalk_text(search_keyword, text, verify_keyword=None):
|
||||
"""推送文本到钉钉联系人(OCR 视觉方案,仅单聊)。供业务脚本 import 调用。
|
||||
target 为搜索关键字(支持工号);display_name 为会话标题校验名(可选,同名人传姓名)。
|
||||
search_keyword 为搜索关键字(支持工号);verify_keyword 为会话标题校验关键字(可选,同名人传姓名)。
|
||||
群消息请改用 dingtalk_robot(钉钉群机器人 webhook)推送。
|
||||
"""
|
||||
return _dt_send_text_to_person(target, text, display_name)
|
||||
return _dt_send_text_to_person(search_keyword, text, verify_keyword)
|
||||
|
||||
|
||||
# ==================== 影刀自定义指令入口 ====================
|
||||
def _send_one(msg):
|
||||
"""发送单条消息,返回 {success: bool, message: str}。msg 为 {target?, text, display_name?} 字典。
|
||||
"""发送单条消息,返回 {success: bool, message: str}。msg 为 {search_keyword?, verify_keyword?, text} 字典。
|
||||
|
||||
- 含 target:钉钉联系人 OCR 推送(单聊);
|
||||
- 不含 target:视为群消息,改用钉钉群机器人(webhook)推送(替换原 OCR 群聊)。
|
||||
- 含 search_keyword:钉钉联系人 OCR 推送(单聊);
|
||||
- 不含 search_keyword:视为群消息,改用钉钉群机器人(webhook)推送。
|
||||
"""
|
||||
target = str((msg or {}).get("target", "") or "").strip()
|
||||
search_keyword = str((msg or {}).get("search_keyword", "") or "").strip()
|
||||
text = str((msg or {}).get("text", "") or "")
|
||||
display_name = str((msg or {}).get("display_name", "") or "").strip() or None
|
||||
verify_keyword = str((msg or {}).get("verify_keyword", "") or "").strip() or None
|
||||
if not text.strip():
|
||||
return {"success": False, "message": "参数 text 不能为空"}
|
||||
if not target:
|
||||
if not search_keyword:
|
||||
# 群消息:交由钉钉群机器人(webhook)推送
|
||||
try:
|
||||
import dingtalk_robot
|
||||
@@ -532,8 +532,8 @@ def _send_one(msg):
|
||||
except Exception as e:
|
||||
return {"success": False, "message": f"群机器人推送失败:{e}"}
|
||||
try:
|
||||
send_dingtalk_text(target, text, display_name)
|
||||
return {"success": True, "message": f"已向 {target} 发送成功"}
|
||||
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)}
|
||||
|
||||
@@ -542,11 +542,11 @@ def main(args):
|
||||
"""
|
||||
影刀自定义指令「37du钉钉推送」调用入口。
|
||||
args: dict,通过流程变量传入(影刀变量支持传字典/列表):
|
||||
方式A(批量,推荐):args = {"messages": [{target?, text, display_name?}, ...]}
|
||||
方式A(批量,推荐):args = {"messages": [{search_keyword?, verify_keyword?, text}, ...]}
|
||||
messages 为「字典列表」,逐条发送并返回汇总结果;
|
||||
含 target 走 OCR 单聊;不含 target 视为群消息,改用钉钉群机器人推送;
|
||||
方式B(单条):args = {"params": {target, text}} 或
|
||||
{"target":..., "text":...};
|
||||
含 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: [...]}
|
||||
@@ -576,13 +576,13 @@ def main(args):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 仅本地自测连通性。影刀中会直接调用 main(args) 并通过流程变量传参,不会走这里。
|
||||
# 批量测试:流程变量 messages = 字典列表,含 target 走 OCR 单聊;
|
||||
# 不含 target 视为群消息,交由钉钉群机器人(webhook)推送。
|
||||
_messages = [
|
||||
{"target": "夏一平", "text": "【37du钉钉推送·测试1】你好,这是一条连通性测试消息。"},
|
||||
{"target": "夏一平", "text": "【37du钉钉推送·测试2】你好,这是第二条测试消息。"},
|
||||
{"text": "【37du钉钉推送·群消息测试】你好,这是一条群机器人测试消息。"},
|
||||
]
|
||||
result = main({"messages": _messages})
|
||||
print("最终结果:", result)
|
||||
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}))
|
||||
|
||||
Reference in New Issue
Block a user