slack

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 11 Imported by: 1 module, 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 plugin's domain: messages, deltas and the facts the source stores. The core never imports it.

Index

Constants

View Source
const (
	UserIDPattern  = `[UW][A-Z0-9]{8,}`
	GroupIDPattern = `S[A-Z0-9]{8,}`
)

Slack ids: a user's starts with U or W, a user group's with S, each followed by at least eight letters or digits. The patterns are for regular expressions that embed an id.

View Source
const Provider unit.ProviderID = "slack"
View Source
const UnknownGroup = "a user group"

UnknownGroup names a user group whose handle no message gave.

Variables

View Source
var ErrUnknownUnit = errors.New("slack: unknown unit")
View Source
var HiddenSubtypes = []string{"message_replied", "channel_join", "channel_leave", "group_join",
	"group_leave", "channel_topic", "channel_purpose", "channel_name", "pinned_item", "unpinned_item"}

Functions

func CompareTS

func CompareTS(a, b string) int

CompareTS orders two Slack timestamps numerically; "" sorts first.

func DMObject

func DMObject(conv string) string

DMObject is "<conv>": a DM or group DM conversation.

func GroupHandles

func GroupHandles(into map[string]string, texts ...string)

GroupHandles adds to into the handle of each user group texts mention as <!subteam^ID|@handle>.

func GroupKey

func GroupKey(id string) string

GroupKey is the ledger key of user group id.

func GroupName

func GroupName(id string, handles map[string]string) string

GroupName is how a person reads user group id: its handle, else UnknownGroup.

func IsGroupID

func IsGroupID(s string) bool

IsGroupID says whether s is a Slack user group id.

func IsUserID

func IsUserID(s string) bool

IsUserID says whether s is a Slack user id.

func ObjectOf

func ObjectOf(conv string, kind ConversationKind, m Message) string

ObjectOf: in an im or mpim every message, thread replies included, belongs to DMObject(conv); elsewhere a message belongs to ThreadObject(conv, ThreadTS or TS).

func ParseObject

func ParseObject(s string) (conv, ts string, err error)

ParseObject splits an object id; ts is "" for a DM object.

func TSTime

func TSTime(ts string) time.Time

TSTime is the instant a Slack ts names, to the microsecond; zero for a ts that names none.

func TeamKey

func TeamKey(name string) string

TeamKey is the ledger key of a team or role named name.

func ThreadObject

func ThreadObject(conv, ts string) string

ThreadObject is "<conv>/<ts>": a thread, or a lone channel message keyed by its own ts.

func ThreadUnit

func ThreadUnit(team, conv, ts string) unit.Unit

ThreadUnit has the key "<team>/<conv>/<ts>", valid for ValidKey.

func UserKey

func UserKey(id string) string

UserKey is the ledger key of user id.

func ValidKey

func ValidKey(key string) error

ValidKey says why key is not a Slack unit key: "<team>/<conv>/<ts>" for a thread, "<team>/<dm>/seg/<id>" for a DM segment.

func ValidPersonKey

func ValidPersonKey(key string) error

ValidPersonKey says why key is not a person key the plugin writes: "slack:<user id>", "group:<user group id>" or "group:<name>", where a name is one line of text that is no id and carries no key prefix or mention of its own.

Types

type Change

type Change struct {
	Seq                        int64
	Conversation, TS, ThreadTS string
	Kind                       ChangeKind
	At                         time.Time
}

type ChangeKind

type ChangeKind string // "new", "edited", "deleted", "reaction"
const (
	ChangeNew      ChangeKind = "new"
	ChangeEdited   ChangeKind = "edited"
	ChangeDeleted  ChangeKind = "deleted"
	ChangeReaction ChangeKind = "reaction"
)

type Conversation

type Conversation struct {
	ID, Name           string // Name "" for an im
	Kind               ConversationKind
	IsMember, Archived bool
	DMUser             string // the other party of an im
}

type ConversationKind

type ConversationKind string // "channel", "private", "im", "mpim"
const (
	KindChannel ConversationKind = "channel"
	KindPrivate ConversationKind = "private"
	KindIM      ConversationKind = "im"
	KindMPIM    ConversationKind = "mpim"
)

func (ConversationKind) DM

func (k ConversationKind) DM() bool

DM says whether the conversation is an im or an mpim.

type DMWindow

type DMWindow struct {
	Conversation, Latest string
	Messages             []Message
	// Units: Segments.UnitsFor(conv, ThreadTS or TS) per message, so a reply follows its
	// parent's segment; empty until segmented.
	Units []unit.Unit
}

func (DMWindow) Version

func (w DMWindow) Version() string

Version is the DM conversation's Stub.Version: its latest message ts.

type Delta

type Delta struct {
	Conversation, ChannelType, TS, ThreadTS string
	EventID                                 string
	Kind                                    Kind
	Message                                 *Message // nil for reactions
	Reaction                                *struct{ Name, User string }
	Via                                     Via
	ReceivedAt                              time.Time
}

func FromEvent

func FromEvent(team string, payload json.RawMessage, via Via, at time.Time) (Delta, bool, error)

FromEvent maps every message, edit, delete, reply and reaction whatever its bot fields; ok is false only for non-message events and the hidden subtypes in HiddenSubtypes. team is the workspace the payload came from; a Delta does not carry it.

type Facts

type Facts struct {
	Team          string
	Conversations []Conversation
	Threads       []Thread
	DMs           []DMWindow
	Users         []User
}

func (Facts) Units

func (f Facts) Units() []unit.Unit

Units are the thread units, then DM segment units; distinct, sorted by Unit.Less.

type FactsReader

type FactsReader interface {
	// Messages of u with ts > after, ascending, at most limit (0: all). A segment unit
	// is sliced by Segments.Range, replies in the DM included by their parent's ts.
	Messages(ctx context.Context, u unit.Unit, after string, limit int) ([]Message, error)
	Conversation(ctx context.Context, id string) (Conversation, error)
	Users(ctx context.Context, ids []string) (map[string]User, error) // cached only, never calls Slack
	UserByEmail(ctx context.Context, email string) (User, bool, error)
	Changes(ctx context.Context, afterSeq int64, limit int) ([]Change, error) // ascending Seq
	Viewer(ctx context.Context) (team, user string, err error)
}

type File

type File struct{ ID, Title, Mimetype string }

type Key

type Key struct{ Team, Conversation, TS, Segment string }

Key is a Slack unit key: a thread when TS is set, a DM segment when Segment is.

func ParseUnit

func ParseUnit(u unit.Unit) (Key, error)

ParseUnit refuses a unit of another provider or an invalid key.

func (Key) Unit

func (k Key) Unit() unit.Unit

type Kind

type Kind string
const (
	KindMessage         Kind = "message"
	KindEdit            Kind = "edit"
	KindDelete          Kind = "delete"
	KindReactionAdded   Kind = "reaction_added"
	KindReactionRemoved Kind = "reaction_removed"
)

type Message

type Message struct {
	TS, ThreadTS, ParentUser     string // ThreadTS "" or == TS for a parent or a lone message
	User, BotID, AppID, Username string // User is the human when set, whatever BotID holds
	Subtype, Text, EditedTS      string
	Deleted                      bool
	Files                        []File
	Links                        []string // URLs in text and attachments
	Mentions                     []string // user ids mentioned
	Reactions                    []Reaction
	ReplyCount                   int
	LatestReply                  string
	Permalink                    string
}

func MessageFrom

func MessageFrom(raw json.RawMessage) (Message, error)

MessageFrom decodes one message of a Web API page (history, replies, search).

func (Message) Anchor

func (m Message) Anchor() string

Anchor is the ts that places the message: its thread's parent, or its own.

type ObjectRef

type ObjectRef struct{ Conversation, TS string }

ObjectRef names a watched object; TS "" names a DM conversation.

func (ObjectRef) Object

func (r ObjectRef) Object() string

Object is the ref's object id.

type Reaction

type Reaction struct {
	Name  string
	Users []string
}

type Scheduler

type Scheduler interface {
	Next(ctx context.Context, objects []string, now time.Time) (map[string]time.Time, error)
}

Scheduler gives the next poll time of watched objects. The default is now+5 min; the module replaces it with a heat scheduler.

type SegmentWriter

type SegmentWriter interface {
	// Apply replaces conv's spans from the first span's FromTS on. A kept ID moves its range;
	// a new ID is minted once and never reused. It returns the units of every span.
	Apply(ctx context.Context, team, conv string, spans []Span) ([]unit.Unit, error)
}

type Segments

type Segments interface {
	// UnitsFor returns the segments covering ts; empty until segmented. Callers pass a
	// reply's thread_ts, so a reply follows its parent's segment.
	UnitsFor(ctx context.Context, conv, ts string) ([]unit.Unit, error)
	Range(ctx context.Context, u unit.Unit) (conv, fromTS, toTS string, err error) // toTS "" while open
}

type Span

type Span struct{ ID, FromTS, ToTS string }

Span is a DM segment's range; ID "" asks for a new segment; ToTS "" is open.

type Thread

type Thread struct {
	Conversation, TS, LatestReply string
	ReplyCount                    int
	Messages                      []Message // ascending ts; Messages[0] is the parent when it was read
}

func (Thread) Version

func (t Thread) Version() string

Version is the thread's Stub.Version: latest_reply, the parent's ts when it has no replies, and a lone message's ts with its edited ts.

type User

type User struct {
	ID, Name, DisplayName, Email string
	IsBot, Deleted               bool // USLACKBOT is always IsBot
	Fetched                      time.Time
}

type Via

type Via string

Via is how a message arrived. A message search stored with no thread is not trusted to place a post: search may not say the thread.

const (
	ViaSocket  Via = "socket"
	ViaCatchUp Via = "catchup"
	ViaProbe   Via = "probe"
	ViaSearch  Via = "search"
	ViaFollow  Via = "follow"
)

type WatchReason

type WatchReason string // "admission", "extraction", "user-follow", "turn"
const (
	ReasonAdmission  WatchReason = "admission"
	ReasonExtraction WatchReason = "extraction"
	ReasonUserFollow WatchReason = "user-follow"
	ReasonTurn       WatchReason = "turn"
)

type Watcher

type Watcher interface {
	Watch(ctx context.Context, obj ObjectRef, reason WatchReason) error // idempotent; keeps the first reason, adds later ones
	Unwatch(ctx context.Context, obj ObjectRef) error                   // stops polling; stored messages stay
}

Jump to

Keyboard shortcuts

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