新增了完整的Signal渠道适配器实现,包含RPC客户端、守护进程管理、安全策略、消息处理、安装配置工具等全套功能,支持通过signal-cli与Signal网络进行通信,包含账户管理、消息收发、反应处理、媒体分析、健康检查等能力。
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
def resolve_signal_account(config: dict[str, Any], account_id: str | None = None) -> dict[str, Any]:
|
|
accounts = config.get("accounts", {})
|
|
if not accounts:
|
|
return _migrate_legacy_config(config)
|
|
|
|
target_id = account_id or resolve_default_signal_account_id(config)
|
|
if not target_id:
|
|
raise ValueError("No Signal account configured")
|
|
|
|
account_cfg = accounts.get(target_id)
|
|
if not account_cfg:
|
|
raise ValueError(f"Signal account '{target_id}' not found in config")
|
|
|
|
return {**account_cfg, "_account_id": target_id}
|
|
|
|
|
|
def resolve_default_signal_account_id(config: dict[str, Any]) -> str | None:
|
|
accounts = config.get("accounts", {})
|
|
if "default" in accounts:
|
|
return "default"
|
|
if accounts:
|
|
return next(iter(accounts))
|
|
return None
|
|
|
|
|
|
def list_signal_account_ids(config: dict[str, Any]) -> list[str]:
|
|
accounts = config.get("accounts", {})
|
|
if accounts:
|
|
return list(accounts)
|
|
if config.get("signal_number"):
|
|
return ["default"]
|
|
return []
|
|
|
|
|
|
def list_enabled_signal_accounts(config: dict[str, Any]) -> list[dict[str, Any]]:
|
|
accounts = config.get("accounts", {})
|
|
result = []
|
|
for account_id, account_cfg in accounts.items():
|
|
if account_cfg.get("enabled", True):
|
|
result.append({**account_cfg, "_account_id": account_id})
|
|
if not result and config.get("signal_number"):
|
|
result.append(_migrate_legacy_config(config))
|
|
return result
|
|
|
|
|
|
def _migrate_legacy_config(config: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
"signal_number": config.get("signal_number", ""),
|
|
"account_uuid": config.get("account_uuid"),
|
|
"cli_path": config.get("cli_path", "signal-cli"),
|
|
"http_host": config.get("http_host"),
|
|
"http_port": config.get("http_port"),
|
|
"http_listen": config.get("http_listen", "127.0.0.1:8080"),
|
|
"home_dir": config.get("home_dir"),
|
|
"java_opts": config.get("java_opts", "-Xmx256m"),
|
|
"receive_mode": config.get("receive_mode"),
|
|
"send_read_receipts": config.get("send_read_receipts"),
|
|
"auto_start": config.get("auto_start", True),
|
|
"enabled": config.get("enabled", True),
|
|
"startup_timeout_ms": config.get("startup_timeout_ms"),
|
|
"_account_id": "default",
|
|
}
|