slackmock

package
v0.1.1 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: 22 Imported by: ? 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 slackmock is a stand-in for Slack, for tests and the local dev stacks. It does three things:

  • It serves the part of the Slack Web API the kōjō Slack adapter calls (auth.test, chat.postMessage, chat.update, users.info, conversations.open, apps.connections.open) under /api/, and records every message kōjō posts.
  • It plays the users: it sends mentions, direct messages and button clicks to kōjō, as signed HTTP requests to /slack/events and /slack/interactive, or over Socket Mode when the adapter has opened a connection.
  • It exposes a small control API under /mock/ so that shell scripts and end-to-end tests can drive it over HTTP.

Point the adapter at it with chat.slack.api_url: <mock URL>/api/.

Index

Constants

View Source
const (
	// BotUserID is the user ID of the kōjō bot, as auth.test reports it.
	BotUserID = "U0KOJOBOT"
	// BotID is the bot ID of the kōjō bot.
	BotID = "B0KOJOBOT"
	// TeamID is the workspace.
	TeamID = "T0SLACKMOCK"
	// AppID is the Slack app.
	AppID = "A0SLACKMOCK"
)

The mock's fixed identities.

Variables

This section is empty.

Functions

func DMChannel

func DMChannel(userID string) string

DMChannel is the ID of the direct message conversation between the bot and a user: SendDM posts there and conversations.open returns it.

func Sign

func Sign(secret, timestamp string, body []byte) string

Sign computes the X-Slack-Signature of a request body: "v0=" and the HMAC-SHA256 of "v0:<timestamp>:<body>" keyed with the signing secret.

Types

type Button

type Button struct {
	BlockID  string `json:"block_id"`
	ActionID string `json:"action_id"`
	Value    string `json:"value"`
	Label    string `json:"label"`
	Style    string `json:"style,omitempty"`
}

Button is a button on a message.

type Message

type Message struct {
	Channel string `json:"channel"`
	TS      string `json:"ts"`
	// ThreadTS is the root of the thread the message is in; empty for a
	// top-level message.
	ThreadTS string `json:"thread_ts,omitempty"`
	// Text is the top-level text, mrkdwn.
	Text string `json:"text"`
	// Blocks is the Block Kit JSON as kōjō sent it.
	Blocks json.RawMessage `json:"blocks,omitempty"`
	// BlockText is the text of the header, section and context blocks, one
	// per line: what a person reads.
	BlockText string `json:"block_text,omitempty"`
	// Buttons are the buttons of the message's actions blocks.
	Buttons []Button `json:"buttons,omitempty"`
	// Updates counts the chat.update calls on the message.
	Updates int `json:"updates"`
}

Message is a message kōjō posted, in its current state: chat.update replaces Text and Blocks and counts in Updates.

type Mock

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

Mock is the Slack mock. It is an http.Handler; Start serves it on a local port for tests.

func New

func New(opts Options) *Mock

New returns a mock with no messages.

func (*Mock) APICalls

func (m *Mock) APICalls(method string) int

APICalls is how many times kōjō called a Web API method, such as "users.info".

func (*Mock) APIURL

func (m *Mock) APIURL() string

APIURL is the value for the adapter's api_url: URL() + "/api/".

func (*Mock) AddUser

func (m *Mock) AddUser(u User)

AddUser adds a user to the workspace, or replaces the one with the same ID.

func (*Mock) ClickButton

func (m *Mock) ClickButton(ref chat.MessageRef, value, user string) error

ClickButton clicks the button with the given value on a message kōjō posted, as the given user, and delivers the block_actions payload to kōjō. It fails when the message has no such button.

func (*Mock) Close

func (m *Mock) Close()

Close drops the Socket Mode connections and stops the server Start began.

func (*Mock) DropSockets

func (m *Mock) DropSockets()

DropSockets closes every Socket Mode connection without notice, the way a network failure does. The adapter is expected to connect again.

func (*Mock) Message

func (m *Mock) Message(channel, ts string) (Message, bool)

Message returns one message kōjō posted.

func (*Mock) Messages

func (m *Mock) Messages() []Message

Messages returns the messages kōjō posted, oldest first, each in its current state.

func (*Mock) Redeliver

func (m *Mock) Redeliver(eventID string) error

Redeliver sends an event again, the way Slack retries one it believes was not received: same event ID, with X-Slack-Retry-Num set.

func (*Mock) Reset

func (m *Mock) Reset()

Reset forgets every message, event and call count. Users and connections stay.

func (*Mock) SendDM

func (m *Mock) SendDM(user, text string) (Sent, error)

SendDM posts a direct message to the bot as the given user and delivers the message event to kōjō.

func (*Mock) SendMention

func (m *Mock) SendMention(channel, user, text string) (Sent, error)

SendMention posts "@kojo <text>" in a channel as the given user and delivers the app_mention event to kōjō. It fails when kōjō does not acknowledge the event.

func (*Mock) SendReply

func (m *Mock) SendReply(channel, threadTS, user, text string) (Sent, error)

SendReply posts a reply in an existing thread: a mention in a channel, a plain message in a DM channel (D…).

func (*Mock) ServeHTTP

func (m *Mock) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the Web API (/api/), the Socket Mode endpoint (/socket), the control API (/mock/) and /healthz.

func (*Mock) SetKojoURL

func (m *Mock) SetKojoURL(u string)

SetKojoURL sets kōjō's base URL, for when it is not known at New.

func (*Mock) SocketConnections

func (m *Mock) SocketConnections() int

SocketConnections is the number of open Socket Mode connections.

func (*Mock) Start

func (m *Mock) Start() string

Start serves the mock on a local port and returns its base URL. Call Close when done.

func (*Mock) URL

func (m *Mock) URL() string

URL is the base URL of a started mock, empty before Start.

func (*Mock) VerifyURL

func (m *Mock) VerifyURL() error

VerifyURL sends kōjō the url_verification challenge Slack sends when a request URL is saved, and checks that the challenge comes back.

func (*Mock) WaitForSocket

func (m *Mock) WaitForSocket(ctx context.Context) error

WaitForSocket blocks until a Socket Mode connection is open or ctx ends.

type Options

type Options struct {
	// SigningSecret signs the requests sent to kōjō. It must be the adapter's
	// signing_secret.
	SigningSecret string
	// KojoURL is kōjō's base URL (http://localhost:8080). Events go to
	// KojoURL + /slack/events, clicks to KojoURL + /slack/interactive.
	KojoURL string
	// BotToken, when set, is the only bot token the Web API accepts.
	BotToken string
	// AppToken, when set, is the only app-level token apps.connections.open
	// accepts. Any xapp- token is accepted otherwise.
	AppToken string
	// Users are the people of the workspace. A user who sends a message
	// without being listed is added with a made-up name and email.
	Users []User
	// Logger receives one line per API call and delivery. Default: discard.
	Logger *slog.Logger
	// HTTPClient delivers events to kōjō. Default: a client with a 10 second
	// timeout.
	HTTPClient *http.Client
	// PingInterval is how often a Socket Mode connection is pinged.
	// Default: 5 seconds.
	PingInterval time.Duration
	// AckTimeout is how long a Socket Mode envelope may go unacknowledged
	// before the delivery fails. Default: 3 seconds, as Slack.
	AckTimeout time.Duration
}

Options configure a Mock. The zero value works: it accepts any token and can deliver events once SetKojoURL has been called or a Socket Mode connection is open.

type Sent

type Sent struct {
	// EventID is the Events API event ID; Redeliver takes it.
	EventID string `json:"event_id"`
	Channel string `json:"channel"`
	// TS is the message's timestamp, which is also its ID.
	TS string `json:"ts"`
	// ThreadTS is set when the message was a reply in a thread.
	ThreadTS string `json:"thread_ts,omitempty"`
}

Sent describes a message the mock sent to kōjō on behalf of a user.

type User

type User struct {
	ID string `json:"id"`
	// Name is the handle; RealName the full name. users.info reports
	// RealName as the display name too.
	Name     string `json:"name"`
	RealName string `json:"real_name,omitempty"`
	Email    string `json:"email,omitempty"`
}

User is a member of the mock workspace.

Directories

Path Synopsis
cmd
slackmock command
Command slackmock runs the Slack mock as a process, for the local dev stacks: kōjō's chat.slack.api_url points at http://<listen>/api/, and the mock delivers mentions, DMs and button clicks to kōjō when its control API (/mock/...) is called.
Command slackmock runs the Slack mock as a process, for the local dev stacks: kōjō's chat.slack.api_url points at http://<listen>/api/, and the mock delivers mentions, DMs and button clicks to kōjō when its control API (/mock/...) is called.

Jump to

Keyboard shortcuts

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