Documentation
¶
Overview ¶
Package slack is the Slack integration: an outbound notifier (a bus Subscriber posting Block Kit messages via chat.postMessage, threaded per initiative) and an inbound Socket Mode client (buttons and the /idea slash command) that routes every action through the daemon's own HTTP handlers, gated by a per-project allowlist of Slack user IDs.
Socket Mode, rather than an HTTP Request URL, because the daemon is loopback-only by design (docs/security-model.md): Socket Mode is an OUTBOUND websocket, so interactivity works with no public URL, no tunnel and no inbound port.
Index ¶
Constants ¶
const ( ActionApproveChangeSet = "idea.cs.approve" ActionRejectChangeSet = "idea.cs.reject" ActionAcceptRecommendations = "idea.judgment.accept_recommendations" ActionAnswerQuestion = "idea.question.answer" ActionOpenUI = "idea.open_ui" CallbackAnswerModal = "idea.answer" )
Action ids. The value of every button is an actionValue, JSON-encoded, so the interaction handler can route a click back to its project without any state of its own.
const DefaultAPIBase = "https://slack.com/api/"
DefaultAPIBase is Slack's Web API root.
const SubscriberName = "slack"
SubscriberName keys Slack's outbox rows and cursor.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthIdentity ¶
type AuthIdentity struct {
Team string `json:"team"`
User string `json:"user"`
UserID string `json:"user_id"`
BotID string `json:"bot_id"`
}
AuthIdentity is auth.test's answer.
type Block ¶
Block is one Block Kit block. A map rather than a typed tree: this package emits a handful of shapes, and Slack -- not a Go type -- is the schema.
type CheckResult ¶
type CheckResult struct {
Team string `json:"team"`
BotUser string `json:"bot_user"`
MessageTS string `json:"message_ts"`
SocketMode string `json:"socket_mode"`
}
CheckResult is what `idea integrations test slack` reports.
func Check ¶
func Check(ctx context.Context, bot *Client, cfg config.SlackConfig, appToken string) (CheckResult, error)
Check validates the bot token (auth.test), posts a test message to the configured channel -- which is what proves the bot is IN the channel, the most common setup miss -- and, when socket_mode is on, that the app-level token can open a Socket Mode connection.
type Client ¶
type Client struct {
// APIBase is the Web API root (tests point it at an httptest server).
APIBase string
BotToken string
AppToken string
HTTP *http.Client
}
Client is a minimal Slack Web API client: the five methods this integration uses, nothing else.
func (*Client) AuthTest ¶
func (c *Client) AuthTest(ctx context.Context) (AuthIdentity, error)
AuthTest validates the bot token.
func (*Client) OpenConnection ¶
OpenConnection asks for a Socket Mode websocket URL (app-level token).
type Envelope ¶
type Envelope struct {
Type string `json:"type"`
EnvelopeID string `json:"envelope_id,omitempty"`
Payload json.RawMessage `json:"payload,omitempty"`
AcceptsResponsePayload bool `json:"accepts_response_payload,omitempty"`
// Reason is set on a "disconnect" frame: refresh_requested, warning,
// link_disabled.
Reason string `json:"reason,omitempty"`
}
Envelope is one Socket Mode frame.
type EnvelopeHandler ¶
EnvelopeHandler handles one enveloped frame and returns the ack payload (nil for an empty ack). It must return quickly: slow work belongs on a goroutine, answered later through the interaction's response_url.
type JudgmentItems ¶
JudgmentItems are the open items attached to a needs_judgment message.
type Message ¶
type Message struct {
Channel string `json:"channel"`
Text string `json:"text"`
Blocks []Block `json:"blocks,omitempty"`
ThreadTS string `json:"thread_ts,omitempty"`
ReplyBroadcast bool `json:"reply_broadcast,omitempty"`
// UnfurlLinks is off: the only links are to the loopback UI, which
// Slack's servers cannot reach anyway.
UnfurlLinks bool `json:"unfurl_links"`
UnfurlMedia bool `json:"unfurl_media"`
}
Message is a chat.postMessage request.
type Notifier ¶
type Notifier struct {
Cfg config.SlackConfig
Client *Client
DB *store.DB
// UIBase is the deep-link root, e.g. http://127.0.0.1:8787. Empty
// disables links.
UIBase string
// Redact scrubs known secrets from outbound text. Slack is egress: a
// failure reason can quote an error that quotes a key.
Redact func(string) string
// contains filtered or unexported fields
}
Notifier is the outbound half: a bus Subscriber that posts each wanted event to the configured channel, threaded per initiative.
func (*Notifier) Deliver ¶
Deliver implements integrations.Subscriber.
The first event for an initiative becomes the root of its thread; every later one replies in that thread, and a needs_judgment reply is also broadcast to the channel (with the configured mentions) because a human decision buried in a thread is a decision nobody makes.
Delivery is at-least-once. Slack's chat.postMessage has no idempotency key, so a daemon killed between Slack accepting a post and the outbox recording it re-posts that one message on restart; the outbox guarantees that window is the only one.
type Route ¶
type Route struct {
ProjectID string
Cfg config.SlackConfig
// Bot is the project's bot-token client, used for views.open.
Bot *Client
Actions integrations.Actions
}
Route is one project's registration on a Socket Mode connection.
type SocketClient ¶
type SocketClient struct {
API *Client
Handler EnvelopeHandler
Log *slog.Logger
// MinBackoff/MaxBackoff bound reconnect delays (defaults 1s, 2m).
MinBackoff, MaxBackoff time.Duration
// contains filtered or unexported fields
}
SocketClient holds one Socket Mode connection open, reconnecting on disconnects, refresh requests and errors.
The websocket library is github.com/coder/websocket (formerly nhooyr.io/websocket): zero dependencies of its own, context-aware reads and writes -- which is what lets shutdown and the per-frame deadlines here be a context cancel rather than a goroutine juggling SetDeadline -- and safe concurrent writes, so an ack from the read loop and an ack from a slower handler cannot interleave frames. gorilla/websocket is the common alternative; it needs a write mutex everywhere and has no context support.
func (*SocketClient) Run ¶
func (c *SocketClient) Run(ctx context.Context)
Run connects and serves until ctx is cancelled.
func (*SocketClient) State ¶
func (c *SocketClient) State() SocketState
State returns a snapshot of the connection's health.
type SocketHub ¶
type SocketHub struct {
// APIBase and HTTP configure the Web API client used for
// apps.connections.open (tests).
APIBase string
HTTP *http.Client
Log *slog.Logger
// ResponseURLAllowed vets an interaction's response_url before anything
// is POSTed to it. Default: only https://hooks.slack.com/.
ResponseURLAllowed func(string) bool
// Parent is the lifetime every connection derives from.
Parent context.Context
// contains filtered or unexported fields
}
SocketHub owns one Socket Mode connection per distinct app-level token and routes each interaction to the project it belongs to.
Per token, not per project, because Slack load-balances a Socket Mode app's frames across ALL of its open connections: two projects each opening their own connection with the same app token would each receive a random half of the other's button clicks. One connection per token, with the project id carried in every button's value, is the only shape where every click reaches the project it was for.
func (*SocketHub) Close ¶
func (h *SocketHub) Close()
Close stops every connection and waits for them to end.
func (*SocketHub) Register ¶
Register attaches a project to the connection for appToken, opening that connection if it is the first project to use it. Re-registering a project replaces its route (a config reload).
func (*SocketHub) State ¶
func (h *SocketHub) State(projectID string) (SocketState, bool)
State returns the health of the connection projectID is registered on.
func (*SocketHub) Unregister ¶
Unregister detaches a project, closing its connection when it was the last.