新增Slack适配器全套核心模块,包括消息处理流水线、会话管理、配置适配、权限控制等完整功能: 1. 新增语音、视觉相关的TTS和图像分析导出接口 2. 实现消息预处理、路由、线程上下文处理的完整流水线 3. 新增账号管理、缓存机制、房间上下文提取功能 4. 支持Webhook和Socket Mode两种事件接收方式 5. 实现权限白名单、审批配对、自动状态管理功能 6. 新增配置迁移、作用域校验、重连策略等辅助模块
87 lines
3.3 KiB
Python
87 lines
3.3 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass
|
|
class SlackCredentials:
|
|
account_id: str = "default"
|
|
bot_token: str = ""
|
|
app_token: str = ""
|
|
user_token: str = ""
|
|
signing_secret: str = ""
|
|
bot_token_source: str = "none"
|
|
app_token_source: str = "none"
|
|
user_token_source: str = "none"
|
|
signing_secret_source: str = "none"
|
|
|
|
@classmethod
|
|
def from_config(cls, account_id: str, config: dict[str, str]) -> SlackCredentials:
|
|
creds = cls(account_id=account_id)
|
|
|
|
creds.bot_token = config.get("bot_token", "")
|
|
creds.app_token = config.get("app_token", "")
|
|
creds.user_token = config.get("user_token", "")
|
|
creds.signing_secret = config.get("signing_secret", "")
|
|
|
|
if creds.bot_token:
|
|
creds.bot_token_source = "config"
|
|
else:
|
|
creds.bot_token = os.getenv("SLACK_BOT_TOKEN", "")
|
|
creds.bot_token_source = "env" if creds.bot_token else "none"
|
|
|
|
if creds.app_token:
|
|
creds.app_token_source = "config"
|
|
else:
|
|
creds.app_token = os.getenv("SLACK_APP_TOKEN", "")
|
|
creds.app_token_source = "env" if creds.app_token else "none"
|
|
|
|
if creds.user_token:
|
|
creds.user_token_source = "config"
|
|
else:
|
|
creds.user_token = os.getenv("SLACK_USER_TOKEN", "")
|
|
creds.user_token_source = "env" if creds.user_token else "none"
|
|
|
|
if creds.signing_secret:
|
|
creds.signing_secret_source = "config"
|
|
else:
|
|
creds.signing_secret = os.getenv("SLACK_SIGNING_SECRET", "")
|
|
creds.signing_secret_source = "env" if creds.signing_secret else "none"
|
|
|
|
return creds
|
|
|
|
@classmethod
|
|
def from_env(cls, account_id: str = "default") -> SlackCredentials:
|
|
creds = cls(account_id=account_id)
|
|
creds.bot_token = os.getenv("SLACK_BOT_TOKEN", "")
|
|
creds.app_token = os.getenv("SLACK_APP_TOKEN", "")
|
|
creds.user_token = os.getenv("SLACK_USER_TOKEN", "")
|
|
creds.signing_secret = os.getenv("SLACK_SIGNING_SECRET", "")
|
|
|
|
creds.bot_token_source = "env" if creds.bot_token else "none"
|
|
creds.app_token_source = "env" if creds.app_token else "none"
|
|
creds.user_token_source = "env" if creds.user_token else "none"
|
|
creds.signing_secret_source = "env" if creds.signing_secret else "none"
|
|
|
|
return creds
|
|
|
|
def validate(self) -> list[str]:
|
|
errors: list[str] = []
|
|
if self.bot_token and not self.bot_token.startswith("xoxb-"):
|
|
errors.append(f"bot_token must start with 'xoxb-', got: {self.bot_token[:5]}...")
|
|
if self.app_token and not self.app_token.startswith("xapp-"):
|
|
errors.append(f"app_token must start with 'xapp-', got: {self.app_token[:5]}...")
|
|
if self.user_token and not self.user_token.startswith("xoxp-"):
|
|
errors.append(f"user_token must start with 'xoxp-', got: {self.user_token[:5]}...")
|
|
return errors
|
|
|
|
def to_dict(self) -> dict[str, str]:
|
|
return {
|
|
"account_id": self.account_id,
|
|
"bot_token_source": self.bot_token_source,
|
|
"app_token_source": self.app_token_source,
|
|
"user_token_source": self.user_token_source,
|
|
"signing_secret_source": self.signing_secret_source,
|
|
}
|