bot

package
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package bot answers whodar questions from Slack. It is transport-agnostic: a socket-mode or events-api adapter normalizes Slack events into Events and feeds them to the Engine, which resolves and replies through a Replier.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(query string, ans resolve.Answer) string

Format renders an answer as Slack mrkdwn.

Types

type AskFunc

type AskFunc func(ctx context.Context, query, mode string, limit int) (resolve.Answer, error)

AskFunc resolves a query in a mode, matching the CLI and web ask path.

type Dialer

type Dialer func(ctx context.Context, url string) (wsConn, error)

Dialer opens a WebSocket to url.

type Engine

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

Engine answers events by resolving the query and formatting a Slack reply.

func New

func New(ask AskFunc, defaultMode, botUserID string, limit int) *Engine

New returns an Engine. It panics if ask is nil, and applies sensible defaults for an empty mode or non-positive limit.

func (*Engine) Handle

func (e *Engine) Handle(ctx context.Context, ev Event, r Replier) error

Handle answers ev and replies through r. An empty question is ignored. A resolve error is reported back to the user rather than failing the transport.

type Event

type Event struct {
	// Text is the message text, possibly containing the bot mention.
	Text string
	// Channel is the channel or DM id to reply in.
	Channel string
	// ThreadTS is the thread timestamp to reply within, if any.
	ThreadTS string
}

Event is a normalized inbound message for the bot to answer.

type EventsHandler

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

EventsHandler serves the Slack Events API. It verifies the request signature, answers the url_verification handshake, and dispatches event callbacks.

func NewEventsHandler

func NewEventsHandler(engine *Engine, replier Replier, botUserID, signingSecret string, opts ...EventsOption) *EventsHandler

NewEventsHandler builds an EventsHandler. It panics on a nil engine or replier, or an empty signing secret.

func (*EventsHandler) ServeHTTP

func (h *EventsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP verifies the request, answers url_verification, and dispatches event callbacks asynchronously so Slack receives a fast acknowledgment.

type EventsOption

type EventsOption func(*EventsHandler)

EventsOption configures an EventsHandler.

func WithClock

func WithClock(now func() time.Time) EventsOption

WithClock overrides the clock, for tests.

func WithEventsLog

func WithEventsLog(w io.Writer) EventsOption

WithEventsLog sets where handler notices are written.

type Replier

type Replier interface {
	// Reply posts text to channel, threading under threadTS when non-empty.
	Reply(ctx context.Context, channel, threadTS, text string) error
}

Replier sends a reply to Slack.

type SocketOption

type SocketOption func(*SocketRunner)

SocketOption configures a SocketRunner.

func WithDialer

func WithDialer(d Dialer) SocketOption

WithDialer overrides the WebSocket dialer, for tests.

func WithLog

func WithLog(w io.Writer) SocketOption

WithLog sets where connection notices are written.

type SocketRunner

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

SocketRunner runs a Slack Socket Mode session: it opens a WebSocket with the app-level token, reads event frames, acknowledges them, and dispatches questions to the Engine. It reconnects until the context is canceled.

func NewSocketRunner

func NewSocketRunner(app *slack.Client, engine *Engine, replier Replier, botUserID string, opts ...SocketOption) *SocketRunner

NewSocketRunner builds a SocketRunner. It panics on nil dependencies.

func (*SocketRunner) Run

func (s *SocketRunner) Run(ctx context.Context) error

Run opens connections and processes events until ctx is canceled. A dropped connection is reopened.

Jump to

Keyboard shortcuts

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