Commit 3ea98d0e by sunqi02

feat: 更新同步推送逻辑,添加消息内容并重构消息发送功能

1 parent 3f7742c4
...@@ -90,9 +90,10 @@ def _run_sync_customer_group_phones(service: CustomerInfoService) -> int: ...@@ -90,9 +90,10 @@ def _run_sync_customer_group_phones(service: CustomerInfoService) -> int:
"""执行同步 + 同步触发推送。""" """执行同步 + 同步触发推送。"""
affected = service.sync_from_customer_groups() affected = service.sync_from_customer_groups()
candidates = service.get_push_candidates(mode="sync") candidates = service.get_push_candidates(mode="sync")
content = "【系统推送】推送信息填写链接"
for record in candidates: for record in candidates:
_do_push(record) if _do_push(record, content):
service.mark_pushed(record) service.mark_pushed(record)
logger.info("同步触发推送: candidates=%s", len(candidates)) logger.info("同步触发推送: candidates=%s", len(candidates))
return affected return affected
...@@ -100,18 +101,56 @@ def _run_sync_customer_group_phones(service: CustomerInfoService) -> int: ...@@ -100,18 +101,56 @@ def _run_sync_customer_group_phones(service: CustomerInfoService) -> int:
def _run_daily_phone_push(service: CustomerInfoService) -> int: def _run_daily_phone_push(service: CustomerInfoService) -> int:
"""执行每日 9 点推送。""" """执行每日 9 点推送。"""
candidates = service.get_push_candidates(mode="daily") candidates = service.get_push_candidates(mode="daily")
content = "【系统推送】推送信息填写链接"
for record in candidates: for record in candidates:
_do_push(record) if _do_push(record, content):
service.mark_pushed(record) service.mark_pushed(record)
logger.info("每日推送: pushed=%s", len(candidates)) logger.info("每日推送: pushed=%s", len(candidates))
return len(candidates) return len(candidates)
def _do_push(record) -> None: def send_im_message(
"""向群发送【系统推送】消息,引导用户填写手机号。 bot_id: str,
group_id: str,
content: str,
msg_type: str = "text",
) -> bool:
"""发送 IM 消息到指定群(可服用)。
通过 IM 业务 API 推送文本消息到 group_id 对应的群。 Args:
bot_id: 机器人账号
group_id: 目标群 contact_id
content: 消息正文
msg_type: 消息类型,默认 text
Returns:
True 发送成功 / False 发送失败
""" """
settings = ConfigLoader().load_settings()
payload = {
"contact_id": group_id,
"content": content,
"bot_id": bot_id,
"msg_type": msg_type,
"biz_rela_no": f"biz-msg-{int(time.time() * 1000)}",
}
headers = {"Content-Type": "application/json"}
if settings.im.api_key:
headers["X-Api-Key"] = settings.im.api_key
url = f"{settings.im.base_url}/api/v1/business/messages/send"
try:
resp = httpx.post(url, json=payload, headers=headers, timeout=10)
resp.raise_for_status()
logger.info("IM 消息发送成功: group_id=%s", group_id)
return True
except Exception:
logger.exception("IM 消息发送失败: group_id=%s", group_id)
return False
def _do_push(record, content: str) -> bool:
"""向群发送消息(兼容旧逻辑,查 DB 获取 bot_id 后委托 send_im_message)。"""
db = SessionLocal() db = SessionLocal()
try: try:
cg = db.query(WeComCustomerGroup).filter( cg = db.query(WeComCustomerGroup).filter(
...@@ -120,25 +159,7 @@ def _do_push(record) -> None: ...@@ -120,25 +159,7 @@ def _do_push(record) -> None:
).first() ).first()
if not cg or not cg.bot_id: if not cg or not cg.bot_id:
logger.warning("未找到群对应的 bot_id: group_id=%s", record.group_id) logger.warning("未找到群对应的 bot_id: group_id=%s", record.group_id)
return return False
return send_im_message(cg.bot_id, record.group_id, content)
settings = ConfigLoader().load_settings()
payload = {
"contact_id": record.group_id,
"content": "【系统推送】推送信息填写链接",
"bot_id": cg.bot_id,
"msg_type": "text",
"biz_rela_no": f"biz-msg-push-{int(time.time() * 1000)}",
}
headers = {"Content-Type": "application/json"}
if settings.im.api_key:
headers["X-Api-Key"] = settings.im.api_key
url = f"{settings.im.base_url}/api/v1/business/messages/send"
resp = httpx.post(url, json=payload, headers=headers, timeout=10)
resp.raise_for_status()
logger.info("推送成功: group_id=%s bot_id=%s", record.group_id, cg.bot_id)
except Exception:
logger.exception("推送失败: group_id=%s", record.group_id)
finally: finally:
db.close() db.close()
...@@ -4,19 +4,19 @@ ...@@ -4,19 +4,19 @@
"type": "openai_compatible", "type": "openai_compatible",
"api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1", "api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1",
"model_name": "qwen3.7-plus", "model_name": "qwen3.7-plus",
"api_key": "sk-107dd26bca184e39bd1744af7d634488" "api_key": "sk-321cbda8e3b24103928c984802f47301"
}, },
"message_gate": { "message_gate": {
"type": "openai_compatible", "type": "openai_compatible",
"api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1", "api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1",
"model_name": "deepseek-v4-flash", "model_name": "deepseek-v4-flash",
"api_key": "sk-107dd26bca184e39bd1744af7d634488" "api_key": "sk-321cbda8e3b24103928c984802f47301"
}, },
"thinking_summary": { "thinking_summary": {
"type": "openai_compatible", "type": "openai_compatible",
"api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1", "api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1",
"model_name": "deepseek-v4-flash", "model_name": "deepseek-v4-flash",
"api_key": "sk-107dd26bca184e39bd1744af7d634488" "api_key": "sk-321cbda8e3b24103928c984802f47301"
} }
}, },
"meeting_minutes": { "meeting_minutes": {
...@@ -24,13 +24,13 @@ ...@@ -24,13 +24,13 @@
"type": "dashscope_asr", "type": "dashscope_asr",
"api_base": "https://dashscope.aliyuncs.com/api/v1", "api_base": "https://dashscope.aliyuncs.com/api/v1",
"model_name": "fun-asr", "model_name": "fun-asr",
"api_key": "sk-107dd26bca184e39bd1744af7d634488" "api_key": "sk-321cbda8e3b24103928c984802f47301"
}, },
"qwen3.6-plus": { "qwen3.6-plus": {
"type": "openai_compatible", "type": "openai_compatible",
"api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1", "api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1",
"model_name": "qwen3.6-plus", "model_name": "qwen3.6-plus",
"api_key": "sk-107dd26bca184e39bd1744af7d634488" "api_key": "sk-321cbda8e3b24103928c984802f47301"
}, },
"oss": { "oss": {
"access_key_id": "LTAI5tB2Q3EJFmaosMYN3uPc", "access_key_id": "LTAI5tB2Q3EJFmaosMYN3uPc",
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!