48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from yuxi.channel.protocols import SessionResolution
|
||
|
|
|
||
|
|
|
||
|
|
class RingCentralSessionAdapter:
|
||
|
|
def resolve_session(self, msg) -> SessionResolution:
|
||
|
|
from yuxi.channel.routing.models import PeerKind
|
||
|
|
|
||
|
|
if hasattr(msg, "sender") and hasattr(msg.sender, "kind"):
|
||
|
|
if msg.sender.kind == PeerKind.DIRECT:
|
||
|
|
chat_id = _extract_chat_id(msg)
|
||
|
|
return SessionResolution(
|
||
|
|
kind="direct",
|
||
|
|
conversation_id=chat_id or msg.sender.id,
|
||
|
|
)
|
||
|
|
|
||
|
|
chat_id = _extract_chat_id(msg)
|
||
|
|
if chat_id:
|
||
|
|
return SessionResolution(kind="group", conversation_id=chat_id)
|
||
|
|
|
||
|
|
if msg.group and msg.group.id:
|
||
|
|
return SessionResolution(kind="group", conversation_id=msg.group.id)
|
||
|
|
|
||
|
|
return SessionResolution(kind="group", conversation_id="unknown")
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def build_dm_session_key(agent_id: str, account_id: str, chat_id: str) -> str:
|
||
|
|
return f"agent:{agent_id}:ringcentral:direct:{chat_id}"
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def build_group_session_key(agent_id: str, account_id: str, chat_id: str) -> str:
|
||
|
|
return f"agent:{agent_id}:ringcentral:group:{chat_id}"
|
||
|
|
|
||
|
|
|
||
|
|
def _extract_chat_id(msg) -> str | None:
|
||
|
|
metadata = getattr(msg, "metadata", {}) or {}
|
||
|
|
if metadata.get("chat_id"):
|
||
|
|
return metadata["chat_id"]
|
||
|
|
|
||
|
|
raw = getattr(msg, "raw_payload", {}) or {}
|
||
|
|
body = raw.get("body", raw)
|
||
|
|
group_id = body.get("groupId", "")
|
||
|
|
if group_id:
|
||
|
|
return group_id
|
||
|
|
|
||
|
|
return None
|