handler

package
v0.0.0-...-5fb48e9 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package handler providers handler/router mechanisms for specific actions or event types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChannelCache

type ChannelCache interface {
	Lookup(name string) (slack.Channel, bool)
}

ChannelCache is the interface to describe the shape of a channel cache we accept.

type ChannelJoinActionFn

type ChannelJoinActionFn func(ctx workqueue.Context, tj ChannelJoiner, r Responder) error

ChannelJoinActionFn is a function for handlers to take actions against team join events.

type ChannelJoinActions

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

ChannelJoinActions represents actions to be taken on a team join event.

func NewChannelJoinActions

func NewChannelJoinActions(shadowMode bool, l zerolog.Logger) *ChannelJoinActions

NewChannelJoinActions returns a ChannelJoinActions for use.

func (*ChannelJoinActions) Handle

func (c *ChannelJoinActions) Handle(name, channelID string, fn ChannelJoinActionFn)

Handle registers a ChannelJoinActionFn to be taken on new join events.

func (*ChannelJoinActions) HandleStatic

func (c *ChannelJoinActions) HandleStatic(name, channelID string, content ...string)

HandleStatic registers a ChannelJoinActionFn that sends an ephemeral message to the joining user. The message is the content variadic, joined by newlines.

func (*ChannelJoinActions) Handler

Handler satisfies workqueue.ChannelJoinHandler.

type ChannelJoiner

type ChannelJoiner interface {
	ChannelID() string
	UserID() string
}

ChannelJoiner is the interface to represent an incoming team join event.

type ChannelType

type ChannelType uint8

ChannelType represents where a message was sent.

const (
	// ChannelUnknown is for when the type wasn't provided.
	ChannelUnknown ChannelType = iota

	// ChannelPublic is for public channels.
	ChannelPublic ChannelType = 1<<iota - 1

	// ChannelPrivate is for private channels, called groups by Slack API
	ChannelPrivate

	// ChannelDM is for direct messages, or called IMs by Slack API
	ChannelDM

	// ChannelGroupDM is for group direct messages, or called MPIMs by Slack API
	ChannelGroupDM

	// ChannelAppHome is for messages sent to the AppHome.
	ChannelAppHome
)

func (ChannelType) String

func (c ChannelType) String() string

type Message

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

Message is a singular message to be processed. Satisfies Messenger interface.

func NewMessage

func NewMessage(channelID, channelType, userID, threadTS, messageTS, subType, text string, files []slackevents.File) Message

NewMessage generates a new message from the various inputs.

func (Message) AllMentions

func (m Message) AllMentions() []mparser.Mention

AllMentions satisfies the Messenger interface.

func (Message) BotMentioned

func (m Message) BotMentioned() bool

BotMentioned satisfies the Messenger interface.

func (Message) ChannelID

func (m Message) ChannelID() string

ChannelID satisfies the Messenger interface.

func (Message) ChannelType

func (m Message) ChannelType() ChannelType

ChannelType satisfies the Messenger interface.

func (Message) Files

func (m Message) Files() []slackevents.File

Files satisfies the Messenger interface.

func (Message) MessageTS

func (m Message) MessageTS() string

MessageTS satisfies the Messenger interface.

func (Message) RawText

func (m Message) RawText() string

RawText satisfies the Messenger interface.

func (Message) SubType

func (m Message) SubType() string

SubType satisfies the Messenger interface.

func (Message) Text

func (m Message) Text() string

Text satisfies the Messenger interface.

func (Message) ThreadTS

func (m Message) ThreadTS() string

ThreadTS satisfies the Messenger interface.

func (Message) UserID

func (m Message) UserID() string

UserID satisfies the Messenger interface.

func (Message) UserMentions

func (m Message) UserMentions() []mparser.Mention

UserMentions satisfies the Messenger interface.

type MessageAction

type MessageAction struct {
	Self        string
	Description string
	// contains filtered or unexported fields
}

MessageAction represents a single piece of interactive action to be taken.

func (MessageAction) Do

Do is the MessageAction's enacter. It uses the Slack client from the workqueue.Context to for handler functions to use.

type MessageActionFn

type MessageActionFn func(ctx workqueue.Context, m Messenger, r Responder) error

MessageActionFn represents an interactivity action where it's not a simple response, and instead needs a function execution.

type MessageActions

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

MessageActions is the central point to the interactivity package, and gives us a way to register handlers and then match those against inputs.

func NewMessageActions

func NewMessageActions(selfID string, shadowMode bool, logger zerolog.Logger) (*MessageActions, error)

NewMessageActions returns a new MessageActions struct.

func (*MessageActions) Handle

func (m *MessageActions) Handle(trigger, description string, aliases []string, fn MessageActionFn)

Handle is similar to HandleStatic(), but instead of specifying which content to reply with you provide your own MessageActionFn function. The bot must be mentioned for this to match. This also supports aliases so that you can have shorter versions.

func (*MessageActions) HandleDynamic

func (m *MessageActions) HandleDynamic(matchFn MessageMatchFn, actionFn MessageActionFn)

HandleDynamic allows you to define a handler where you control whether it matches by providing your own MessageMatchFn. This allows for the handler to be dynamic.

func (*MessageActions) HandleMentionedReaction

func (m *MessageActions) HandleMentionedReaction(trigger string, reactions ...string)

HandleMentionedReaction handles reacting to messages that contain trigger anywhere in the message, but only if the bot is mentioned.

func (*MessageActions) HandlePrefix

func (m *MessageActions) HandlePrefix(prefix, description string, fn MessageActionFn)

HandlePrefix handles a message with any prefix, regardless of the mentions in it.

func (*MessageActions) HandleReaction

func (m *MessageActions) HandleReaction(trigger string, reactions ...string)

HandleReaction handles reacting to messages that contain trigger anywhere in the message.

func (*MessageActions) HandleReactionRand

func (m *MessageActions) HandleReactionRand(trigger string, reactions ...string)

HandleReactionRand handles reacting to messages that contain trigger anywhere in the message, but only doing it periodically.

func (*MessageActions) HandleStatic

func (m *MessageActions) HandleStatic(trigger, description string, aliases []string, content ...string)

HandleStatic is a static response handler. If the trigger matches a known value, return the content. The bot must be mentioned for this to match. It also supports aliases so that you can have shorter versions.

func (*MessageActions) HandleStaticContains

func (m *MessageActions) HandleStaticContains(contains string, content ...string)

HandleStaticContains handles reacting to messages that contain trigger anywhere in the message, except it responds instead of reacting with an emoji.

func (*MessageActions) Handler

Handler is the method that should satisfy a workqueue handler.

func (*MessageActions) Match

func (m *MessageActions) Match(message Message) []MessageAction

Match looks at the trigger to see if it matches any known handlers. Some handlers are only invoked if the bot was mentioned.

func (*MessageActions) Registered

func (m *MessageActions) Registered() []RegisteredMessageHandler

Registered returns a list of registered handlers. You could use this to build help output.

type MessageMatchFn

type MessageMatchFn func(shadowMode bool, m Messenger) bool

MessageMatchFn is a function for consumers to provider their own handler match. If the MessageMatchFn returns true, the handler matches. The shadowMode argument is true if this is a pre-production bot, meaning we should probably not react / respond.

type Messenger

type Messenger interface {
	// ChannelID is the ID of the channel where the message was sent. This could
	// be a DM and not a channel, so be sure to check ChannelType()
	ChannelID() string

	// ChannelType is the kind of channel this is, also includes DMs.
	ChannelType() ChannelType

	// UserID is the ID of the user who sent the message.
	UserID() string

	// ThreadTS is the parent message ID, indicating we are in a thread
	ThreadTS() string

	// MessageTS is the message ID. If ThreadTS is empty string, we're not in a
	// thread
	MessageTS() string

	// AllMentions contains all parsed mentions in a message, including the bot
	// user, channels, etc.
	AllMentions() []mparser.Mention

	// UserMentions() contains only non-bot user mentions in the message.
	UserMentions() []mparser.Mention

	// Text is the text with any mentions removed, and leading/trailing
	// whitespace removed.
	Text() string

	// RawText is the raw Slack messages with no mentions removed.
	RawText() string

	// BotMentioned indicates if the bot was mentioned in the message.
	BotMentioned() bool

	// Files are any files attached to the message
	Files() []slackevents.File
}

Messenger is the interface to represent an incoming message.

type RegisteredMessageHandler

type RegisteredMessageHandler struct {
	Trigger     string
	Prefix      bool
	Description string
	Aliases     []string
}

RegisteredMessageHandler is what is returned from the MessageActions.Registered() method.

type Responder

type Responder interface {
	React(ctx context.Context, emoji string) error

	Respond(ctx context.Context, msg string, attachments ...slack.Attachment) error

	// RespondTo is the same as respond, except it prefixes the message with an
	// at-mention of the user who triggered the action. Helpful if responding
	// with an error message.
	RespondTo(ctx context.Context, msg string, attachments ...slack.Attachment) error

	// RespondUnfurled is the same as Respond, except it asks slack to redner
	// URL previews in the channel or DM.
	RespondUnfurled(ctx context.Context, msg string, attachments ...slack.Attachment) error

	// RespondTextAttachment responds in the channel or thread with a text
	// attachment (helpful for sharing long messages).
	RespondTextAttachment(ctx context.Context, msg, attachment string) error

	// RespondMentions responds in the channel / thread, and mentions any users
	// who were mentioned in the original message.
	RespondMentions(ctx context.Context, msg string, attachments ...slack.Attachment) error

	// RespondMentionsUnfurled is the same as RespondMentions, but with
	// Unfurling enabled like RespondUnfurled.
	RespondMentionsUnfurled(ctx context.Context, msg string, attachments ...slack.Attachment) error

	// RespondMentionsTextAttachment is similar to RespondMentions, except with
	// the additional text attachment.
	RespondMentionsTextAttachment(ctx context.Context, msg, attachment string) error

	// RespondEphemeral responds with a message only the person who sent the message will see.
	RespondEphemeral(ctx context.Context, msg string, attachments ...slack.Attachment) error

	// RespondEphemeralTextAttachment is similar to RespondEphemeral, but also
	// includes a text attachment.
	RespondEphemeralTextAttachment(ctx context.Context, msg, attachment string) error

	// RespondeDM is for sending a DM to the user instead of responding in
	// the channel, or with an ephemeral message.
	RespondDM(ctx context.Context, msg string, attachments ...slack.Attachment) error
}

Responder is the interface to describe the functionality used by handlers to respond or react.

type TeamJoinActionFn

type TeamJoinActionFn func(ctx workqueue.Context, tj TeamJoiner, r Responder) error

TeamJoinActionFn is a function for handlers to take actions against team join events.

type TeamJoinActions

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

TeamJoinActions represents actions to be taken on a team join event.

func NewTeamJoinActions

func NewTeamJoinActions(shadowMode bool, l zerolog.Logger) *TeamJoinActions

NewTeamJoinActions returns a TeamJoinActions for use.

func (*TeamJoinActions) Handle

func (t *TeamJoinActions) Handle(name string, fn TeamJoinActionFn)

Handle registers a TeamJoinActionFn to be taken on new join events.

func (*TeamJoinActions) Handler

Handler satisfies workqueue.TeamJoinHandler.

type TeamJoiner

type TeamJoiner interface {
	User() slack.User
}

TeamJoiner is the interface to represent an incoming team join event.

Jump to

Keyboard shortcuts

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