新增 KakaoTalk 渠道扩展,支持在 Yuxi 平台中集成 KakaoTalk 即时通讯渠道。 包含以下功能模块: - bot: Bot 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - card_builder: KakaoTalk 卡片消息构建 - quick_reply: 快捷回复处理 - types: 类型定义
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class KakaoTalkSecurityAdapter:
|
|
DM_POLICY_OPTIONS = ["pairing", "allowlist", "open", "disabled"]
|
|
|
|
def resolve_dm_policy(self) -> dict:
|
|
return {"mode": "pairing", "allow_from": []}
|
|
|
|
def resolve_dm_policy_for_account(self, account: dict) -> dict:
|
|
mode = account.get("dm_policy", "pairing")
|
|
return {"mode": mode, "allow_from": account.get("allow_from", [])}
|
|
|
|
def is_allowed_dm(self, peer_id: str, allow_from: list[str], policy: str) -> bool:
|
|
if "*" in allow_from:
|
|
return True
|
|
if policy == "open":
|
|
return True
|
|
if policy == "disabled":
|
|
return False
|
|
if policy == "allowlist":
|
|
return self._match_peer(peer_id, allow_from)
|
|
if policy == "pairing":
|
|
return self._match_peer(peer_id, allow_from)
|
|
return False
|
|
|
|
@staticmethod
|
|
def _match_peer(peer_id: str, allow_from: list[str]) -> bool:
|
|
for entry in allow_from:
|
|
entry = entry.strip()
|
|
if entry == "*":
|
|
return True
|
|
if entry.startswith("kakaotalk:"):
|
|
target = entry[len("kakaotalk:"):]
|
|
else:
|
|
target = entry
|
|
if target == peer_id:
|
|
return True
|
|
return False |