slack

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
var BotEvents = []string{"app_mention", "message.im"}

BotEvents are the Events API subscriptions of the bot.

View Source
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

func Manifest(publicURL, mode string) ([]byte, error)

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

func New(cfg config.Slack, opts ...Option) (*Adapter, error)

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

func (a *Adapter) Check(ctx context.Context) error

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) Mode

func (a *Adapter) Mode() string

Mode is the mode the adapter runs in: socket or http.

func (*Adapter) Name

func (a *Adapter) Name() string

Name is the surface name: slack.

func (*Adapter) Post

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

func (a *Adapter) Routes(h chat.Handler) map[string]http.Handler

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

func (a *Adapter) Run(ctx context.Context, h chat.Handler) error

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.

func (*Adapter) Update

func (a *Adapter) Update(ctx context.Context, ref chat.MessageRef, m chat.Outgoing) error

Update replaces a posted message. A text too long for one message keeps its first part in the message and posts the rest as replies in the thread.

type Option

type Option func(*options)

Option configures New.

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient sets the HTTP client used for Slack Web API calls. The default is a client with a 30 second timeout.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger. The default is slog.Default().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL