ForcePilot/backend/package/yuxi/channels/adapters/whatsapp/session.py

33 lines
877 B
Python
Raw Normal View History

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]
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