Files
nanobot/nanobot/channels/slack/validation.py
T
chengyongru e8a29a0087 feat(webui): improve setup flows across chat channels (NAN-112) (#5356)
* feat(webui): improve channel setup flows

* fix(webui): refine channel setup interactions (NAN-112)

* fix(webui): keep email setup sections channel-owned

* fix(webui): remove concrete channel details from shared components

* refactor(webui): remove unused channel setup resources

* fix(webui): finish channel-owned setup boundaries

* fix(webui): update channel switches optimistically

* test(channels): remove obsolete absence assertions

* fix(webui): flatten channel instance settings disclosure

* fix(webui): complete channel setup and connection flows

* fix(whatsapp): wait for session database release

* fix(webui): preserve scrolling channel controls

* fix(webui): stage channel connection checks

* fix(channels): align verification actions

* fix(email): expose connection checks

* fix(webui): align channel field actions

* fix(email): verify account credentials

* fix(wecom): bypass system proxy for websocket

* fix(channels): stabilize async connection cleanup

* fix(webui): autosave channel credentials

* fix(email): make provider presets non-destructive

* test(weixin): isolate persisted QR credentials
2026-09-11 15:22:14 +08:00

92 lines
3.1 KiB
Python

"""Slack setup validation owned by the channel package."""
from typing import Any
from nanobot.channels.contracts import ChannelValidationContext
from nanobot.channels.validation import (
check,
http_post,
message_from_response,
official_action,
payload,
required_checks,
status_from_checks,
string_value,
)
def validate(values: dict[str, Any], _context: ChannelValidationContext) -> dict[str, Any]:
checks, missing = required_checks("slack", values)
app_token = string_value(values.get("appToken"))
bot_token = string_value(values.get("botToken"))
proxy = string_value(values.get("proxy"))
if app_token:
checks.append(
check(
"app_token_prefix",
"Socket Mode app token",
"pass" if app_token.startswith("xapp-") else "fail",
"App-level Socket Mode tokens start with xapp-.",
action_url=official_action("slack"),
)
)
if bot_token:
checks.append(
check(
"bot_token_prefix",
"Bot token",
"pass" if bot_token.startswith("xoxb-") else "fail",
"Bot tokens start with xoxb- after installing the Slack app.",
)
)
if bot_token.startswith("xoxb-"):
try:
data = http_post(
"https://slack.com/api/auth.test",
headers={"Authorization": f"Bearer {bot_token}"},
proxy=proxy or None,
)
if data.get("ok"):
identity = {
"name": data.get("user"),
"workspace": data.get("team"),
"account": data.get("user_id"),
}
checks.append(
check(
"auth_test",
"Workspace identity",
"pass",
"Slack accepted the bot token.",
)
)
status = "connected" if app_token.startswith("xapp-") else "configured"
return payload(
"slack",
status,
checks,
identity=identity,
missing_fields=missing,
)
checks.append(
check(
"auth_test",
"Workspace identity",
"fail",
message_from_response(data, "Slack rejected the bot token."),
)
)
except Exception as exc:
checks.append(
check(
"auth_test",
"Workspace identity",
"warn",
f"Could not reach Slack now: {exc}",
)
)
return status_from_checks("slack", checks, missing)
__all__ = ["validate"]