新增Slack适配器全套核心模块,包括消息处理流水线、会话管理、配置适配、权限控制等完整功能: 1. 新增语音、视觉相关的TTS和图像分析导出接口 2. 实现消息预处理、路由、线程上下文处理的完整流水线 3. 新增账号管理、缓存机制、房间上下文提取功能 4. 支持Webhook和Socket Mode两种事件接收方式 5. 实现权限白名单、审批配对、自动状态管理功能 6. 新增配置迁移、作用域校验、重连策略等辅助模块
80 lines
2.8 KiB
Python
80 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from typing import Any
|
|
|
|
|
|
@dataclass
|
|
class SetupAdapterRequest:
|
|
channel_id: str
|
|
user_id: str = ""
|
|
config: dict[str, Any] = field(default_factory=dict)
|
|
commands: list[str] = field(default_factory=list)
|
|
|
|
@property
|
|
def is_dm(self) -> bool:
|
|
return self.channel_id.startswith("D")
|
|
|
|
|
|
@dataclass
|
|
class SetupAdapterResponse:
|
|
text: str = ""
|
|
blocks: list[dict[str, Any]] = field(default_factory=list)
|
|
replace_original: bool = False
|
|
delete_original: bool = False
|
|
ephemeral: bool = True
|
|
|
|
|
|
def handle_setup_command(
|
|
request: SetupAdapterRequest,
|
|
) -> SetupAdapterResponse:
|
|
if "wizard" in request.commands or "setup" in request.commands:
|
|
return _render_wizard_response(request)
|
|
if "status" in request.commands:
|
|
return _render_status_response(request)
|
|
if "help" in request.commands:
|
|
return _render_help_response(request)
|
|
return _render_help_response(request)
|
|
|
|
|
|
def _render_wizard_response(request: SetupAdapterRequest) -> SetupAdapterResponse:
|
|
return SetupAdapterResponse(
|
|
text="*Slack Setup Wizard*\n\n"
|
|
"Let's set up your Slack bot integration.\n\n"
|
|
"1. *Bot Token* - Ensure `SLACK_BOT_TOKEN` is set in your environment\n"
|
|
"2. *App Token* - For Socket Mode, set `SLACK_APP_TOKEN`\n"
|
|
"3. *Events* - Subscribe to `message.channels`, `message.im` events\n"
|
|
"4. *Scopes* - Ensure `chat:write`, `channels:history` scopes are added\n\n"
|
|
"Use `/yuxi status` to check current configuration.",
|
|
ephemeral=True,
|
|
)
|
|
|
|
|
|
def _render_status_response(request: SetupAdapterRequest) -> SetupAdapterResponse:
|
|
import os
|
|
|
|
token_ok = bool(os.environ.get("SLACK_BOT_TOKEN", "") or request.config.get("token", ""))
|
|
app_token_ok = bool(os.environ.get("SLACK_APP_TOKEN", "") or request.config.get("app_token", ""))
|
|
security = request.config.get("security", {}) or {}
|
|
pairing_enabled = bool(security.get("pairing", {}).get("enabled"))
|
|
|
|
text = "*Slack Configuration Status*\n\n"
|
|
text += f"Bot Token: {'✅' if token_ok else '❌'}\n"
|
|
text += f"App Token: {'✅' if app_token_ok else '❌'}\n"
|
|
text += f"Pairing: {'✅ Enabled' if pairing_enabled else '⚠️ Disabled'}\n"
|
|
text += f"Config Keys: {len(request.config)} items\n"
|
|
|
|
return SetupAdapterResponse(text=text, ephemeral=True)
|
|
|
|
|
|
def _render_help_response(request: SetupAdapterRequest) -> SetupAdapterResponse:
|
|
return SetupAdapterResponse(
|
|
text="*Slack Setup Help*\n\n"
|
|
"Available commands:\n"
|
|
"• `/yuxi setup` - Start setup wizard\n"
|
|
"• `/yuxi status` - View current configuration\n"
|
|
"• `/yuxi help` - Show this help\n\n"
|
|
"Configuration is managed via `config.yaml` or environment variables.",
|
|
ephemeral=True,
|
|
)
|