ForcePilot/backend/package/yuxi/channels/adapters/whatsapp/session.py
Kris 5c3611ff19 refactor(whatsapp): 整理WhatsApp适配器代码结构并修复多线程安全问题
主要变更:
1. 重构导入顺序,统一模块导入规范
2. 提取通用方法到session模块,减少代码重复
3. 为缓存类添加线程/异步锁,修复并发安全问题
4. 新增入站处理器和发送管理器模块,拆分业务逻辑
5. 优化凭证队列,改为异步实现
6. 移除废弃的SSE_POLLING能力标识
7. 修复轮询投票解析逻辑
8. 优化Markdown转换规则,避免格式冲突
9. 完善连接控制器的异常处理
10. 新增发送静默消息的API支持
2026-05-13 16:17:30 +08:00

36 lines
926 B
Python

from __future__ import annotations
def jid_to_thread_key(channel_id: str, jid: str, scope: str = "per_sender") -> str:
if scope == "global":
return f"{channel_id}:global"
if "@g.us" in jid:
return f"{channel_id}:group:{jid}"
return f"{channel_id}:dm:{_extract_sender_number(jid)}"
def _extract_sender_number(jid: str) -> str:
return jid.split("@")[0]
extract_sender_number = _extract_sender_number
def jid_to_chat_type(jid: str) -> str:
if "@g.us" in jid:
return "group"
if "@broadcast" in jid:
return "broadcast"
return "direct"
def normalize_phone(phone: str) -> str:
return phone.lstrip("+").replace(" ", "").replace("-", "")
def resolve_session_scope(config: dict) -> str:
scope = config.get("sessionScope", config.get("session_scope", "per_sender"))
if scope not in ("per_sender", "global"):
return "per_sender"
return scope