Merge branch 'dev' into picture-feedback
This commit is contained in:
commit
34ad40778b
180 files changed
+13262
-5333
No files matched your search
@@ -4,6 +4,7 @@ backend/app/services/integrations/wecom/notify.py
|
||||
用途:企业微信消息通知适配(文本卡片、文件、事件消息)。
|
||||
依赖:requests、Flask current_app、User / WecomNotifyAudit 模型。
|
||||
逻辑:排程完成、权限申请、培训提醒等业务事件调用本模块发送企微消息并写审计。
|
||||
文本卡片标题默认加上「AI智能体」来源;密码重置和权限变更保持原标题。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -24,6 +25,17 @@ _TOKEN_CACHE = {
|
||||
'expires_at': None,
|
||||
}
|
||||
|
||||
# 企微 textcard 没有发送者栏,用来源前缀让收件人看出是 AI 智能体推送。
|
||||
_AGENT_SOURCE_PREFIX = 'AI智能体 · '
|
||||
_TEXT_CARD_TITLE_LIMIT = 120
|
||||
# 账号与权限通知不是智能体推送,标题不加来源。
|
||||
_NO_AGENT_SOURCE_EVENTS = frozenset({
|
||||
'password_reset',
|
||||
'permission_batch_updated',
|
||||
'permission_request_approved',
|
||||
'permission_request_rejected',
|
||||
})
|
||||
|
||||
|
||||
def _get_enabled_events() -> set[str]:
|
||||
raw = current_app.config.get('WECOM_NOTIFY_EVENTS', '')
|
||||
@@ -141,19 +153,54 @@ def send_wecom_file_message(wecom_userid: str, media_id: str) -> dict:
|
||||
return resp.json()
|
||||
|
||||
|
||||
def _with_agent_source_title(title: str, *, enabled: bool = True) -> str:
|
||||
"""
|
||||
给企微 textcard 标题加上「AI智能体」来源,并限制在 120 字以内。
|
||||
|
||||
核心逻辑:前缀放在最前,超长时截断业务标题,避免来源被切掉。
|
||||
已带相同前缀时不重复添加。enabled 为 False 时只做长度截断。
|
||||
Args:
|
||||
title: 业务侧传入的卡片标题。
|
||||
enabled: 为 False 时保持原标题,用于密码重置和权限变更。
|
||||
Returns:
|
||||
实际发给企业微信的标题。
|
||||
"""
|
||||
text = (title or '').strip()
|
||||
if not enabled:
|
||||
return text[:_TEXT_CARD_TITLE_LIMIT]
|
||||
if text.startswith(_AGENT_SOURCE_PREFIX):
|
||||
return text[:_TEXT_CARD_TITLE_LIMIT]
|
||||
return f'{_AGENT_SOURCE_PREFIX}{text}'[:_TEXT_CARD_TITLE_LIMIT]
|
||||
|
||||
|
||||
def send_wecom_textcard_message(
|
||||
wecom_userid: str,
|
||||
title: str,
|
||||
description: str,
|
||||
jump_url: str,
|
||||
*,
|
||||
with_agent_source: bool = True,
|
||||
) -> dict:
|
||||
"""
|
||||
发送企业微信文本卡片。
|
||||
|
||||
核心逻辑:默认在标题前加上「AI智能体」来源。审计记录仍保存调用方传入的业务标题。
|
||||
Args:
|
||||
wecom_userid: 企业微信 userid。
|
||||
title: 业务标题。
|
||||
description: 卡片正文。
|
||||
jump_url: 点击跳转地址。
|
||||
with_agent_source: 为 False 时标题不加来源。
|
||||
Returns:
|
||||
企业微信 message/send 的 JSON 响应。
|
||||
"""
|
||||
token = _get_access_token()
|
||||
payload = {
|
||||
'touser': wecom_userid,
|
||||
'msgtype': 'textcard',
|
||||
'agentid': int(current_app.config.get('WECOM_AGENT_ID')),
|
||||
'textcard': {
|
||||
'title': title[:120],
|
||||
'title': _with_agent_source_title(title, enabled=with_agent_source),
|
||||
'description': (description or '')[:1024],
|
||||
'url': jump_url or _get_notify_base_url() or 'https://work.weixin.qq.com',
|
||||
'btntxt': '查看详情',
|
||||
@@ -200,6 +247,7 @@ def send_wecom_event_message(
|
||||
|
||||
核心逻辑:默认使用 `user.wecom_userid`;合同付款第二、三轮可传入 OA HrmResource.id
|
||||
作为 wecom_userid,因为该 id 同时是企业微信 userid。
|
||||
标题在发送时加上「AI智能体」来源;密码重置和权限变更不加。审计 title 保持业务标题。
|
||||
Args:
|
||||
event_type: 通知事件开关名。
|
||||
user: 审计关联的系统用户,可为收件人在 HXOPS 中的映射账号。
|
||||
@@ -239,7 +287,13 @@ def send_wecom_event_message(
|
||||
else:
|
||||
final_jump_url = _build_jump_url(jump_path, trace_id, jump_params)
|
||||
audit.jump_url = final_jump_url
|
||||
data = send_wecom_textcard_message(target_userid, title, content, final_jump_url)
|
||||
data = send_wecom_textcard_message(
|
||||
target_userid,
|
||||
title,
|
||||
content,
|
||||
final_jump_url,
|
||||
with_agent_source=event_type not in _NO_AGENT_SOURCE_EVENTS,
|
||||
)
|
||||
if data.get('errcode') == 0:
|
||||
audit.status = 'sent'
|
||||
return {'ok': True, 'trace_id': trace_id}
|
||||
|
||||
Reference in new issue
Block a user