60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from yuxi.channels.models import ChannelResponse, DeliveryResult
|
||
|
|
|
||
|
|
WECHAT_DELIVERY_MODE = "direct"
|
||
|
|
|
||
|
|
_MESSAGE_PRIORITY = {
|
||
|
|
"text": 0,
|
||
|
|
"image": 5,
|
||
|
|
"file": 10,
|
||
|
|
"voice": 15,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
class WeChatOutboundAdapter:
|
||
|
|
delivery_mode: str = "direct"
|
||
|
|
|
||
|
|
def __init__(self):
|
||
|
|
self._priority_enabled = False
|
||
|
|
self._pending_messages: list[tuple[int, Any]] = []
|
||
|
|
|
||
|
|
def configure(self, config: dict[str, Any]) -> None:
|
||
|
|
self._priority_enabled = config.get("outbound_priority_enabled", False)
|
||
|
|
|
||
|
|
async def before_deliver_payload(self, payload: dict[str, Any], response: ChannelResponse) -> dict[str, Any]:
|
||
|
|
import time
|
||
|
|
|
||
|
|
payload["_sent_at"] = time.time()
|
||
|
|
if response.metadata.get("agent_version"):
|
||
|
|
payload["_agent_version"] = response.metadata["agent_version"]
|
||
|
|
return payload
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def should_suppress_local_payload_prompt(config: dict[str, Any]) -> bool:
|
||
|
|
return True
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def build_attached_results(result: DeliveryResult, mode: str = "") -> dict[str, Any]:
|
||
|
|
attached: dict[str, Any] = {
|
||
|
|
"success": result.success,
|
||
|
|
"error": result.error,
|
||
|
|
"message_id": result.message_id,
|
||
|
|
"sent_message_id": result.message_id,
|
||
|
|
}
|
||
|
|
if mode:
|
||
|
|
attached["mode"] = mode
|
||
|
|
if result.metadata:
|
||
|
|
attached.update(result.metadata)
|
||
|
|
return attached
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def sort_outbound_messages(messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||
|
|
def _priority(msg: dict[str, Any]) -> int:
|
||
|
|
msg_type = str(msg.get("msgtype", "text"))
|
||
|
|
return _MESSAGE_PRIORITY.get(msg_type, 0)
|
||
|
|
|
||
|
|
return sorted(messages, key=_priority, reverse=True)
|