Files
37duAutomation/.trae/documents/dingtalk-message-sender.md
T
puddy 8c97d48382 refactor: 重构项目代码结构与依赖管理
1.  **依赖与导入路径修正
    - 移除所有本地sys.path插入,统一使用绝对包导入
    - 修复logger导入路径,统一从app.utils导入
    - 重构intelligent_retry.py移除冗余导入
    - 统一hrbp各模块的导入路径

2.  **项目配置与构建
    - 新增pyproject.toml配置文件,规范项目元数据与依赖声明
    - 更新requirements.txt新增pyautogui、pywin32、pyperclip依赖

3.  **业务模块重构
    - 重构请假/加班审批模块的导入路径与内部逻辑
    - 重命名审批结果描述(将"不通过"改为"驳回")
    - 统一代码中的箭头符号(将→改为->)
    - 新增请假表单解析模块、钉钉通知模块
    - 重构加班/请假校验模块的报告生成逻辑,统一报告格式与推送流程

4.  **测试与文档
    - 新增端到端测试脚本与统一测试运行器
    - 完善README.md文档,补充项目结构、功能说明与使用方法
    - 新增钉钉消息推送工具文档
    - 新增测试用例与辅助工具

5.  **其他优化
    - 修复common/parser.py中的正则表达式兼容单引号的HTML属性
    - 新增session_manager.py移除冗余导入与路径修正
    - 新增OA审批报告生成逻辑优化,支持分组统计与详细清单输出
2026-08-07 00:18:49 +08:00

120 lines
6.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 钉钉消息推送工具 实现计划
## Context(背景)
项目目前只有基于 DrissionPage 的 **浏览器** 自动化(OA 审批),通用工具统一放在 `app/utils/`(见 [logger.py](file:///d:/work/code/Process-Automation/app/utils/logger.py)、[intelligent_retry.py](file:///d:/work/code/Process-Automation/app/utils/intelligent_retry.py))。
新需求:写一个 **钉钉桌面客户端** 消息推送工具——通过模拟键盘快捷键 + 剪贴板,向指定 **群名/人名** 发送 **文本和文档**。钉钉是桌面客户端(非浏览器),走不了 DrissionPage,需要引入桌面自动化依赖。
用户描述的操作链路:`Ctrl+D 激活钉钉 → Ctrl+Shift+F 搜索 → 回车确认 → 粘贴文本/文档 → 回车发送`。
## 已确认的决策
1. **窗口激活**:两者都支持,默认发 `Ctrl+D` 全局热键;失败自动回退到 win32 按标题查窗口置前。
2. **文本+文件**:钉钉无法一条消息同时贴文本和文件 → 先发文本、再发文件(两条消息)。
## 依赖
新增到 [requirements.txt](file:///d:/work/code/Process-Automation/requirements.txt) 和 [pyproject.toml](file:///d:/work/code/Process-Automation/pyproject.toml):
- `pyautogui` —— 发送键盘快捷键(`hotkey` / `press`)
- `pywin32` —— 窗口查找激活(`win32gui`)+ 文件剪贴板(`win32clipboard` 的 `CF_HDROP`)
- `pyperclip` —— 文本剪贴板(`pyautogui` 会自动带上,显式声明便于理解)
## 文件改动
### 1. 新增 `app/utils/dingtalk_sender.py`(核心)
遵循项目风格:文件头注释块(`@Module/@FileName/@Author/@CreateTime/@Description/@Version`)、复用 [get_logger](file:///d:/work/code/Process-Automation/app/utils/logger.py) 做日志、复用 [retry](file:///d:/work/code/Process-Automation/app/utils/intelligent_retry.py) 装饰关键步骤。
**模块结构**:
```
日志 & 常量
├─ logger = get_logger(__name__)
├─ 默认等待时间常量(搜索结果、发送、激活)
窗口激活
├─ _find_dingtalk_hwnd() # win32gui.EnumWindows 按标题"钉钉"查找
├─ _activate_by_win32() # ShowWindow(SW_RESTORE) + SetForegroundWindow(含 Alt trick 绕过前台锁)
└─ _activate_by_hotkey() # pyautogui.hotkey('ctrl','d')
剪贴板
├─ _set_text_to_clipboard(text) # pyperclip.copy
└─ _set_file_to_clipboard(path) # win32clipboard + struct 打包 DROPFILES(CF_HDROP, fWide=1, utf-16-le)
搜索定位
└─ _search_and_open(target) # Ctrl+Shift+F → 粘贴 target → 回车 → 等待
发送
├─ _send_text(text) # 剪贴板文本 → Ctrl+V → 回车
└─ _send_file(path) # 剪贴板文件 → Ctrl+V → 等待预览框 → 回车
高层 API
├─ class DingTalkSender # 封装可调参数(各步骤等待时间、激活方式)
│ └─ send(target, text, file_path, is_group)
├─ send_dingtalk_message(target, text, file_path, is_group, ...) # 一次性便捷函数
├─ send_to_group(group_name, text, file_path) # 群消息快捷封装
└─ send_to_person(name, text, file_path) # 个人消息快捷封装
```
**主流程 `DingTalkSender.send`**:
1. 激活钉钉窗口(默认 Ctrl+D,失败回退 win32)
2. `Ctrl+Shift+F` 打开搜索 → 粘贴 `target` → 回车 → 等待 `wait_search` 秒
3. 若有 `text`:粘贴文本 → 回车发送 → 等待 `wait_send` 秒
4. 若有 `file_path`:文件入剪贴板 → `Ctrl+V` → 等待 `wait_file_preview` 秒(预览框) → 回车发送
5. 全程 logger.info 记录每步,失败抛 RuntimeError 含清晰提示
**关键实现细节**:
- **中文输入**:一律走剪贴板 `Ctrl+V`,不用 `pyautogui.write`(不支持中文且受输入法干扰)。
- **文件剪贴板**:`DROPFILES` 结构(20 字节:`pFiles=20, pt=(0,0), fNC=0, fWide=1`)+ 文件绝对路径 `utf-16-le` 编码 + 双 `\0` 结尾,用 `win32clipboard.SetClipboardData(CF_HDROP, data)`。路径必须 `os.path.abspath` 转绝对路径。
- **窗口置前 trick**:`SetForegroundWindow` 会被 Windows 前台锁拦截,先 `pyautogui.press('alt')` 或 `win32gui.SendMessage(shell_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0)` 再置前。
- **搜索回车**:钉钉搜索框回车默认选中第一条结果并进入会话,焦点自动转到消息输入框。
- **参数校验**:`target` 必填;`text` 和 `file_path` 至少传一个;`file_path` 不存在则提前报错。
- **`@retry` 包裹**:激活、搜索两个易受环境影响的关键步骤用 `@retry(max_attempts=2)`,发送步骤不重试(避免重复发消息)。
**对外 API 签名**:
```python
def send_dingtalk_message(
target: str,
text: str = "",
file_path: str = "",
is_group: bool = False,
activate_method: str = "auto", # "auto"(默认Ctrl+D回退win32) | "hotkey" | "win32"
wait_search: float = 1.5,
wait_send: float = 1.0,
wait_file_preview: float = 1.5,
) -> None: ...
```
### 2. 更新 `requirements.txt`
追加三行:`pyautogui`、`pywin32`、`pyperclip`。
### 3. 更新 `pyproject.toml` `[project].dependencies`
同步追加同样三项。
## 不改动的部分
- 不动 `app/utils/__init__.py`(现有内容只是注释头,保持原样,符合项目不集中导出的习惯)。
- 不动任何 OA 业务模块(钉钉工具独立,暂不接入审批流程回调)。
## 验证方式
1. **依赖安装**:`.venv\Scripts\python.exe -m pip install pyautogui pywin32 pyperclip`
2. **导入自检**:`.venv\Scripts\python.exe -c "from app.utils.dingtalk_sender import send_to_group, send_to_person"`
3. **剪贴板单测(不发消息)**:调用 `_set_file_to_clipboard(r"C:\test.txt")` 后在资源管理器 `Ctrl+V` 验证能粘贴出文件。
4. **端到端实测**(需钉钉已登录):
```python
from app.utils.dingtalk_sender import send_to_person
send_to_person("张三", text="测试消息") # 文本
send_to_person("张三", file_path=r"C:\report\月报.xlsx") # 文件
send_to_person("张三", text="请查收", file_path=r"C:\report\月报.xlsx") # 先文本后文件
```
观察钉钉窗口是否被激活、搜索是否命中目标、消息是否发出。
5. **日志检查**:`app/logs/app_YYYYMMDD.log` 应有完整步骤记录。