Documentation
¶
Overview ¶
Package slack is the Slack chat adapter: mentions and DMs in, threaded replies and approval buttons out.
It runs in one of two modes. In socket mode it holds a Socket Mode WebSocket opened with the app-level token, so kōjō needs no public ingress. In http mode Slack calls /slack/events and /slack/interactive, and every request is checked against the signing secret.
Delivery is at least once. An event or click is acknowledged to Slack once the Handler has accepted it; when the Handler returns an error the request is not acknowledged (HTTP 500, or no Socket Mode ack) so that Slack sends it again. Events Slack repeats after they were accepted are dropped.
Index ¶
- Constants
- Variables
- func Manifest(publicURL, mode string) ([]byte, error)
- type Adapter
- func (a *Adapter) AskApproval(ctx context.Context, t chat.Thread, ap chat.Approval) (chat.MessageRef, error)
- func (a *Adapter) Check(ctx context.Context) error
- func (a *Adapter) Mode() string
- func (a *Adapter) Name() string
- func (a *Adapter) Post(ctx context.Context, t chat.Thread, m chat.Outgoing) (chat.MessageRef, error)
- func (a *Adapter) ResolveApproval(ctx context.Context, ref chat.MessageRef, ap chat.Approval, outcome string) error
- func (a *Adapter) Routes(h chat.Handler) map[string]http.Handler
- func (a *Adapter) Run(ctx context.Context, h chat.Handler) error
- func (a *Adapter) Update(ctx context.Context, ref chat.MessageRef, m chat.Outgoing) error
- type Option
Constants ¶
const ( // Surface is the adapter's name, as it appears in chat.Thread.Surface. Surface = "slack" // ModeSocket holds a Socket Mode connection. It is the default. ModeSocket = "socket" // ModeHTTP receives events and clicks as signed HTTP requests. ModeHTTP = "http" // EventsPath is where Slack posts Events API requests in http mode. EventsPath = "/slack/events" // InteractivePath is where Slack posts button clicks in http mode. InteractivePath = "/slack/interactive" )
Variables ¶
var BotEvents = []string{"app_mention", "message.im"}
BotEvents are the Events API subscriptions of the bot.
var BotScopes = []string{
"app_mentions:read",
"chat:write",
"im:history",
"im:read",
"im:write",
"users:read",
"users:read.email",
}
BotScopes are the OAuth scopes the bot token needs:
- app_mentions:read: receive messages that mention the bot
- chat:write: post and update messages
- im:history: receive direct messages
- im:read, im:write: see and open direct message conversations
- users:read, users:read.email: resolve a sender's name and email, which maps the sender onto a kōjō user
Functions ¶
func Manifest ¶
Manifest returns the Slack app manifest, as YAML, for kojo setup slack: the bot scopes and event subscriptions the adapter needs, interactivity for the approval buttons, and Socket Mode on or off.
mode is socket (the default when empty) or http. In http mode the request URLs are publicURL + /slack/events and /slack/interactive, and publicURL must be an https URL, which is all Slack accepts. In socket mode publicURL is not used.
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter is the Slack implementation of chat.Adapter.
func New ¶
New builds the adapter from the Slack section of kojo.yaml. It fails when the mode is unknown or a token the mode needs is missing; it makes no network call (Check does).
func (*Adapter) AskApproval ¶
func (a *Adapter) AskApproval(ctx context.Context, t chat.Thread, ap chat.Approval) (chat.MessageRef, error)
AskApproval posts a message with one button per option: the title as a header, the text below it, then the buttons. Without options it offers Approve and Deny. The approval ID travels in the block_id of the buttons, so a click comes back as a chat.Action carrying it.
func (*Adapter) Check ¶
Check verifies the configuration against Slack: the bot token with auth.test, and in socket mode that the app-level token is an xapp- token. It is what kojo doctor runs.
func (*Adapter) Post ¶
func (a *Adapter) Post(ctx context.Context, t chat.Thread, m chat.Outgoing) (chat.MessageRef, error)
Post sends a message to a thread. The Markdown text is converted to mrkdwn. A text too long for one message continues in further messages of the same thread (under the first message when t names no thread); the returned ref is the first message.
t.Channel is a channel ID, or a user ID (U…) to send a direct message.
func (*Adapter) ResolveApproval ¶
func (a *Adapter) ResolveApproval(ctx context.Context, ref chat.MessageRef, ap chat.Approval, outcome string) error
ResolveApproval replaces the buttons of an approval message with the outcome, shown as a context line: "Approved by @ana". The outcome is Markdown; an empty outcome only removes the buttons.
func (*Adapter) Routes ¶
Routes returns the handlers to mount on the public server: /slack/events and /slack/interactive in http mode, none in socket mode.
func (*Adapter) Run ¶
Run holds the adapter's connection until ctx ends, then returns nil.
In socket mode it keeps a Socket Mode connection open: it reconnects when the connection drops or Slack asks for it, and acknowledges every envelope the Handler accepted. It returns an error only when Slack refuses the app-level token, which no retry fixes. In http mode there is no connection to hold: Run blocks on ctx.
type Option ¶
type Option func(*options)
Option configures New.
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client used for Slack Web API calls. The default is a client with a 30 second timeout.
func WithLogger ¶
WithLogger sets the logger. The default is slog.Default().