Documentation ¶
Overview ¶
Package slackbot hopes to ease development of Slack bots by adding helpful methods and a mux-router style interface to the github.com/slack-go/slack package.
Incoming Slack RTM events are mapped to a handler in the following form:
bot.Hear("(?i)how are you(.*)").MessageHandler(HowAreYouHandler)
The package adds Reply and ReplyWithAttachments methods:
func HowAreYouHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) { bot.Reply(evt, "A bit tired. You get it? A bit?", slackbot.WithTyping) } func HowAreYouAttachmentsHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) { txt := "Beep Beep Boop is a ridiculously simple hosting platform for your Slackbots." attachment := slack.Attachment{ Pretext: "We bring bots to life. :sunglasses: :thumbsup:", Title: "Host, deploy and share your bot in seconds.", TitleLink: "https://GrantStreetGroup.com/", Text: txt, Fallback: txt, ImageURL: "https://storage.googleapis.com/GrantStreetGroup/_assets/bot-1.22f6fb.png", Color: "#7CD197", } attachments := []slack.Attachment{attachment} bot.ReplyWithAttachments(evt, attachments, slackbot.WithTyping) }
The slackbot package exposes github.com/slack-go/slack RTM and Client objects enabling a consumer to interact with the lower level package directly:
func HowAreYouHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) { bot.RTM.NewOutgoingMessage("Hello", "#random") }
Project home and samples: https://github.com/GrantStreetGroup/go-slackbot
Index ¶
- Constants
- func AddBotToContext(ctx context.Context, bot *Bot) context.Context
- func AddMessageToContext(ctx context.Context, msg *slack.MessageEvent) context.Context
- func AddReactionAddedToContext(ctx context.Context, react *slack.ReactionAddedEvent) context.Context
- func AddReactionRemovedToContext(ctx context.Context, react *slack.ReactionRemovedEvent) context.Context
- func ConvertReactionAdded(evt *slackevents.ReactionAddedEvent) *slack.ReactionAddedEvent
- func ConvertReactionRemoved(evt *slackevents.ReactionRemovedEvent) *slack.ReactionRemovedEvent
- func IsDebug(ctx context.Context) bool
- func IsDirectMention(evt *slack.MessageEvent, userID string) bool
- func IsDirectMessage(evt *slack.MessageEvent) bool
- func IsMention(evt *slack.MessageEvent) bool
- func IsMentioned(evt *slack.MessageEvent, userID string) bool
- func MessageFromContext(ctx context.Context) *slack.MessageEvent
- func ReactionAddedFromContext(ctx context.Context) *slack.ReactionAddedEvent
- func ReactionRemovedFromContext(ctx context.Context) *slack.ReactionRemovedEvent
- func ReactionTypeFromContext(ctx context.Context) string
- func SetDebug(ctx context.Context) context.Context
- func StripDirectMention(text string) string
- func WhoMentioned(evt *slack.MessageEvent) []string
- type Bot
- func (b *Bot) BotUserID() string
- func (b *Bot) ConvertMessageEvent(evt *slackevents.MessageEvent) *slack.MessageEvent
- func (b *Bot) Reply(evt *slack.MessageEvent, msg string, typing bool)
- func (b *Bot) ReplyWithAttachments(evt *slack.MessageEvent, attachments []slack.Attachment, typing bool)
- func (b *Bot) Run()
- func (b *Bot) RunRTM()
- func (b *Bot) RunSocketMode()
- func (b *Bot) SetBotID(ID string)
- func (b *Bot) Type(evt *slack.MessageEvent, msg interface{})
- type Handler
- type Matcher
- type MessageHandler
- type MessageType
- type Preprocessor
- type ReactionHandler
- type ReactionMatcher
- type Reactions
- type RegexpMatcher
- type Route
- func (r *Route) AddMatcher(m Matcher) *Route
- func (r *Route) Handler(handler Handler) *Route
- func (r *Route) Hear(regex string) *Route
- func (r *Route) Match(ctx context.Context, match *RouteMatch) (bool, context.Context)
- func (r *Route) MessageHandler(fn MessageHandler) *Route
- func (r *Route) Messages(types ...MessageType) *Route
- func (r *Route) Preprocess(fn Preprocessor) *Route
- func (r *Route) ReactTo(react string) *Route
- func (r *Route) ReactionHandler(fn ReactionHandler) *Route
- func (r *Route) Subrouter() Router
- type RouteMatch
- type Router
- type SimpleRouter
- func (r *SimpleRouter) AddMatcher(m Matcher) *Route
- func (r *SimpleRouter) Handler(handler Handler) *Route
- func (r *SimpleRouter) Hear(regex string) *Route
- func (r *SimpleRouter) Match(ctx context.Context, match *RouteMatch) (bool, context.Context)
- func (r *SimpleRouter) MessageHandler(handler MessageHandler) *Route
- func (r *SimpleRouter) Messages(types ...MessageType) *Route
- func (r *SimpleRouter) NewRoute() *Route
- func (r *SimpleRouter) ReactTo(react string) *Route
- func (r *SimpleRouter) ReactionHandler(handler ReactionHandler) *Route
- func (r *SimpleRouter) SetBotID(botID string)
- type TypesMatcher
Constants ¶
const ( WithTyping bool = true WithoutTyping bool = false )
const ( BOT_CONTEXT = "__BOT_CONTEXT__" MESSAGE_CONTEXT = "__MESSAGE_CONTEXT__" REACTION_CONTEXT = "__REACTION_CONTEXT__" REACTION_EVENT = "__REACTION_EVENT__" BOT_DEBUG = "__BOT_DEBUG__" )
Variables ¶
This section is empty.
Functions ¶
func AddBotToContext ¶
AddBotToContext sets the bot reference in context and returns the newly derived context
func AddMessageToContext ¶
AddMessageToContext sets the Slack message event reference in context and returns the newly derived context
func AddReactionAddedToContext ¶
func AddReactionAddedToContext(ctx context.Context, react *slack.ReactionAddedEvent) context.Context
AddReactionAddedToContext
func AddReactionRemovedToContext ¶
func AddReactionRemovedToContext(ctx context.Context, react *slack.ReactionRemovedEvent) context.Context
AddReactionAddedToContext
func ConvertReactionAdded ¶
func ConvertReactionAdded(evt *slackevents.ReactionAddedEvent) *slack.ReactionAddedEvent
func ConvertReactionRemoved ¶
func ConvertReactionRemoved(evt *slackevents.ReactionRemovedEvent) *slack.ReactionRemovedEvent
func IsDirectMention ¶
func IsDirectMention(evt *slack.MessageEvent, userID string) bool
IsDirectMention returns true is message is a Direct Mention that mentions a specific user. A direct mention is a mention at the very beginning of the message
func IsDirectMessage ¶
func IsDirectMessage(evt *slack.MessageEvent) bool
IsDirectMessage returns true if this message is in a direct message conversation
func IsMention ¶
func IsMention(evt *slack.MessageEvent) bool
IsMention returns true the message contains a mention
func IsMentioned ¶
func IsMentioned(evt *slack.MessageEvent, userID string) bool
IsMentioned returns true if this message contains a mention of a specific user
func MessageFromContext ¶
func MessageFromContext(ctx context.Context) *slack.MessageEvent
func ReactionAddedFromContext ¶
func ReactionAddedFromContext(ctx context.Context) *slack.ReactionAddedEvent
func ReactionRemovedFromContext ¶
func ReactionRemovedFromContext(ctx context.Context) *slack.ReactionRemovedEvent
func ReactionTypeFromContext ¶
func StripDirectMention ¶
StripDirectMention removes a leading mention (aka direct mention) from a message string
func WhoMentioned ¶
func WhoMentioned(evt *slack.MessageEvent) []string
WhoMentioned returns a list of userIDs mentioned in the message
Types ¶
type Bot ¶
type Bot struct { SimpleRouter // Slack API Client *slack.Client RTM *slack.RTM Socket *socketmode.Client Debug bool EventMode string // contains filtered or unexported fields }
func BotFromContext ¶
func (*Bot) ConvertMessageEvent ¶
func (b *Bot) ConvertMessageEvent(evt *slackevents.MessageEvent) *slack.MessageEvent
func (*Bot) Reply ¶
func (b *Bot) Reply(evt *slack.MessageEvent, msg string, typing bool)
Reply replies to a message event with a simple message.
func (*Bot) ReplyWithAttachments ¶
func (b *Bot) ReplyWithAttachments(evt *slack.MessageEvent, attachments []slack.Attachment, typing bool)
ReplyWithAttachments replys to a message event with a Slack Attachments message.
func (*Bot) Run ¶
func (b *Bot) Run()
Run listeners for incoming slack events via RTM or Socketmode, matching them to an appropriate handler.
func (*Bot) RunRTM ¶
func (b *Bot) RunRTM()
Run listeners for incoming slack RTM events, matching them to an appropriate handler.
func (*Bot) RunSocketMode ¶
func (b *Bot) RunSocketMode()
func (*Bot) Type ¶
func (b *Bot) Type(evt *slack.MessageEvent, msg interface{})
Type sends a typing message and simulates delay (max 2000ms) based on message size.
type MessageHandler ¶
type MessageHandler func(ctx context.Context, bot *Bot, msg *slack.MessageEvent)
type MessageType ¶
type MessageType string
const ( DirectMessage MessageType = "direct_message" DirectMention MessageType = "direct_mention" Mention MessageType = "mention" Ambient MessageType = "ambient" )
type ReactionHandler ¶
type ReactionHandler func(ctx context.Context, bot *Bot, added *slack.ReactionAddedEvent, removed *slack.ReactionRemovedEvent)
type ReactionMatcher ¶
type ReactionMatcher struct {
// contains filtered or unexported fields
}
func (*ReactionMatcher) SetBotID ¶
func (rm *ReactionMatcher) SetBotID(botID string)
type RegexpMatcher ¶
type RegexpMatcher struct {
// contains filtered or unexported fields
}
func (*RegexpMatcher) SetBotID ¶
func (rm *RegexpMatcher) SetBotID(botID string)
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
func (*Route) AddMatcher ¶
addMatcher adds a matcher to the route.
func (*Route) MessageHandler ¶
func (r *Route) MessageHandler(fn MessageHandler) *Route
func (*Route) Messages ¶
func (r *Route) Messages(types ...MessageType) *Route
func (*Route) Preprocess ¶
func (r *Route) Preprocess(fn Preprocessor) *Route
func (*Route) ReactionHandler ¶
func (r *Route) ReactionHandler(fn ReactionHandler) *Route
type RouteMatch ¶
RouteMatch stores information about a matched route.
type Router ¶
type Router interface { Match(context.Context, *RouteMatch) (bool, context.Context) NewRoute() *Route Hear(regex string) *Route Handler(handler Handler) *Route MessageHandler(handler MessageHandler) *Route ReactionHandler(handler ReactionHandler) *Route Messages(types ...MessageType) *Route AddMatcher(m Matcher) *Route SetBotID(botID string) ReactTo(react string) *Route }
type SimpleRouter ¶
type SimpleRouter struct {
// contains filtered or unexported fields
}
func (*SimpleRouter) AddMatcher ¶
func (r *SimpleRouter) AddMatcher(m Matcher) *Route
func (*SimpleRouter) Handler ¶
func (r *SimpleRouter) Handler(handler Handler) *Route
func (*SimpleRouter) Hear ¶
func (r *SimpleRouter) Hear(regex string) *Route
func (*SimpleRouter) Match ¶
func (r *SimpleRouter) Match(ctx context.Context, match *RouteMatch) (bool, context.Context)
Match matches registered routes against the request.
func (*SimpleRouter) MessageHandler ¶
func (r *SimpleRouter) MessageHandler(handler MessageHandler) *Route
func (*SimpleRouter) Messages ¶
func (r *SimpleRouter) Messages(types ...MessageType) *Route
func (*SimpleRouter) NewRoute ¶
func (r *SimpleRouter) NewRoute() *Route
NewRoute registers an empty route.
func (*SimpleRouter) ReactTo ¶
func (r *SimpleRouter) ReactTo(react string) *Route
func (*SimpleRouter) ReactionHandler ¶
func (r *SimpleRouter) ReactionHandler(handler ReactionHandler) *Route
func (*SimpleRouter) SetBotID ¶
func (r *SimpleRouter) SetBotID(botID string)
type TypesMatcher ¶
type TypesMatcher struct {
// contains filtered or unexported fields
}
func (*TypesMatcher) SetBotID ¶
func (tm *TypesMatcher) SetBotID(botID string)