slackbot

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package slackbot provides Slack Socket Mode integration for Kojo agents.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteTokens

func DeleteTokens(tp TokenProvider, agentID string) error

DeleteTokens removes all Slack tokens for an agent.

func FetchChannelHistory

func FetchChannelHistory(ctx context.Context, api *slack.Client, agentDataDir, channelID string, limit int, resolve UserResolver, logger *slog.Logger) []chathistory.HistoryMessage

FetchChannelHistory retrieves recent channel messages from Slack. This is a sliding window that overwrites the previous channel history file.

func FetchThreadHistory

func FetchThreadHistory(ctx context.Context, api *slack.Client, agentDataDir, channelID, threadTS string, resolve UserResolver, logger *slog.Logger) []chathistory.HistoryMessage

FetchThreadHistory retrieves thread messages from Slack.

  • First call (no history file): fetches the entire thread including the parent message, and writes the file.
  • Subsequent calls: uses the last real Slack timestamp in the file as a cursor to fetch only new messages (delta), appending them to the file.

The history file may also contain locally-appended bot response entries (with IDs like "1234567890.bot") used by shouldAutoReply. These are ignored when determining the delta cursor.

func LoadTokens

func LoadTokens(tp TokenProvider, agentID string) (appToken, botToken string, err error)

LoadTokens retrieves the app and bot tokens from the credential store.

func PlainToSlack

func PlainToSlack(text string) string

PlainToSlack converts standard Markdown from agent output to Slack mrkdwn. It preserves code blocks and inline code from being transformed.

func SlackToPlain

func SlackToPlain(text string, resolve UserResolver) string

SlackToPlain converts Slack mrkdwn to plain text suitable for the agent. It resolves links and strips mention formatting. If resolve is non-nil, user mentions <@U12345> are resolved to display names.

func SplitMessage

func SplitMessage(text string, maxLen int) []string

SplitMessage splits a long message into chunks that fit within Slack's message length limit (approximately 3000 chars per chunk for safety).

func StoreTokens

func StoreTokens(tp TokenProvider, agentID, appToken, botToken string) error

StoreTokens saves the app and bot tokens to the credential store. If the second write fails, the first is rolled back to its previous value so the store never contains a half-updated pair.

func StripBotMention

func StripBotMention(text, botUserID string) string

StripBotMention removes all @bot mentions from the message text.

func TestConnection

func TestConnection(ctx context.Context, appToken, botToken string) (team, botUser string, err error)

TestConnection performs auth.test to validate the tokens. The provided context controls the request timeout.

Types

type AgentDataDirFunc

type AgentDataDirFunc func(agentID string) string

AgentDataDirFunc resolves an agent ID to its data directory path.

type Bot

type Bot struct {
	// contains filtered or unexported fields
}

Bot manages a single Slack Socket Mode connection for one agent.

func NewBot

func NewBot(parentCtx context.Context, agentID string, agentDataDir string, cfg agent.SlackBotConfig, appToken, botToken string, mgr ChatManager, logger *slog.Logger, extraSlackOpts ...slack.Option) *Bot

NewBot creates a new Bot instance. Call Run() to start it. agentDataDir is the agent's data directory used for storing conversation history files. parentCtx controls the Bot's lifetime: cancelling it will stop the event loop.

func (*Bot) Done

func (b *Bot) Done() <-chan struct{}

Done returns a channel that is closed when the bot exits.

func (*Bot) Run

func (b *Bot) Run()

Run starts the Socket Mode event loop. It blocks until the Bot's context is cancelled.

func (*Bot) Stop

func (b *Bot) Stop()

Stop cancels the bot's context and waits for it to finish.

type ChatManager

type ChatManager interface {
	Chat(ctx context.Context, agentID, message, role string, attachments []agent.MessageAttachment, source ...agent.BusySource) (<-chan agent.ChatEvent, error)
	ChatOneShot(ctx context.Context, agentID, message string) (<-chan agent.ChatEvent, error)
}

ChatManager is the interface the bot uses to interact with agents. agent.Manager satisfies this interface directly — no adapter needed.

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub manages all SlackBot instances across agents. All operations are serialized through a single event loop goroutine, eliminating lock juggling and TOCTOU races.

func NewHub

func NewHub(mgr ChatManager, tokens TokenProvider, agentDataDir AgentDataDirFunc, logger *slog.Logger) *Hub

NewHub creates a new Hub and starts its event loop. Call Stop() on shutdown. agentDataDir resolves an agent ID to its data directory path for history file storage.

func (*Hub) IsRunning

func (h *Hub) IsRunning(agentID string) bool

IsRunning returns true if a bot is running for the given agent.

func (*Hub) Reconfigure

func (h *Hub) Reconfigure(agentID string, cfg agent.SlackBotConfig)

Reconfigure stops and restarts the bot with new configuration. If the config is disabled, it only stops the bot.

func (*Hub) StartBot

func (h *Hub) StartBot(agentID string, cfg agent.SlackBotConfig)

StartBot starts a Slack bot for the given agent. If one is already running, it is stopped first.

func (*Hub) Stop

func (h *Hub) Stop()

Stop stops all bots and shuts down the event loop. Blocks until complete.

func (*Hub) StopBot

func (h *Hub) StopBot(agentID string)

StopBot stops the Slack bot for the given agent.

type TokenProvider

type TokenProvider interface {
	GetToken(provider, agentID, sourceID, key string) (string, error)
	SetToken(provider, agentID, sourceID, key, value string, expiresAt time.Time) error
	DeleteToken(provider, agentID, sourceID, key string) error
	DeleteTokensBySource(provider, agentID, sourceID string) error
}

TokenProvider reads/writes encrypted Slack tokens from a credential store.

type UserResolver

type UserResolver func(userID string) string

UserResolver resolves a Slack user ID to a display name. Return the original ID if resolution fails.

Jump to

Keyboard shortcuts

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