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/nlopes/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://beepboophq.com/", Text: txt, Fallback: txt, ImageURL: "https://storage.googleapis.com/beepboophq/_assets/bot-1.22f6fb.png", Color: "#7CD197", } attachments := []slack.Attachment{attachment} bot.ReplyWithAttachments(evt, attachments, slackbot.WithTyping) }
The slackbot package exposes github.com/nlopes/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/BeepBoopHQ/go-slackbot
Index ¶
- 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 StripDirectMention(text string) string
- func WhoMentioned(evt *slack.MessageEvent) []string
- type Bot
- func (b *Bot) BotUserID() string
- func (b *Bot) Reply(evt *slack.MessageEvent, msg string)
- func (b *Bot) ReplyWithAttachments(evt *slack.MessageEvent, msg string, attachments ...slack.Attachment)
- func (b *Bot) Run(quitCh <-chan struct{}) error
- func (b *Bot) Type(evt *slack.MessageEvent)
- func (b *Bot) TypeByMessage(evt *slack.MessageEvent, msg interface{})
- func (b *Bot) WithDebugging() *Bot
- type Handler
- type Matcher
- type MessageHandler
- type MessageType
- type Preprocessor
- type RegexpMatcher
- type Route
- func (r *Route) AddMatcher(m Matcher) *Route
- func (r *Route) Err() error
- func (r *Route) Handler(handler Handler) error
- 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) error
- func (r *Route) Messages(types ...MessageType) *Route
- func (r *Route) NoTalkToSelf() *Route
- func (r *Route) Preprocess(fn Preprocessor) *Route
- func (r *Route) Subrouter() Router
- func (r *Route) TalkToSelf() *Route
- type RouteMatch
- type Router
- type SimpleRouter
- func (r *SimpleRouter) AddMatcher(m Matcher) *Route
- func (r *SimpleRouter) AlwaysTalkToSelf() Router
- func (r *SimpleRouter) Err() error
- func (r *SimpleRouter) Handler(handler Handler) error
- 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) error
- func (r *SimpleRouter) Messages(types ...MessageType) *Route
- func (r *SimpleRouter) NeverTalkToSelf() Router
- func (r *SimpleRouter) NoTalkToSelf() *Route
- func (r *SimpleRouter) SetBotID(botID string)
- func (r *SimpleRouter) TalkToSelf() *Route
- type TypesMatcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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 Client *slack.Client // Slack API RTM *slack.RTM TypingDelayMultiplier float64 // Multiplier on typing delay. Default 0 -> no delay. 1 -> 2ms per character, 5 -> 10ms per, 0.5 -> 1ms per. Max delay is 2000ms regardless. // contains filtered or unexported fields }
func BotFromContext ¶
func (*Bot) Reply ¶
func (b *Bot) Reply(evt *slack.MessageEvent, msg string)
Reply replies to a message event with a simple message.
func (*Bot) ReplyWithAttachments ¶
func (b *Bot) ReplyWithAttachments(evt *slack.MessageEvent, msg string, attachments ...slack.Attachment)
ReplyWithAttachments replys to a message event with a Slack Attachments message.
func (*Bot) Run ¶
Run listens for incoming slack RTM events, matching them to an appropriate handler. It will terminate when the provided channel is closed, or if it encounters an error during initial authentication. Authentication is done synchronously, and a non-nil error will be returned if an authentication error is encounters. Once authentication has succeeded, Run will create a new goroutine for the actual message handling, and thus does not need to be run in a goroutine itself.
func (*Bot) Type ¶
func (b *Bot) Type(evt *slack.MessageEvent)
Type sends a typing event to indicate that the bot is "typing" or otherwise working.
func (*Bot) TypeByMessage ¶
func (b *Bot) TypeByMessage(evt *slack.MessageEvent, msg interface{})
TypeByMessage sends a typing message and simulates delay (max 2000ms) based on message size.
func (*Bot) WithDebugging ¶
Returns a copy of the bot with debugging enabled. Intended to be daisychained with the New() constructor. Note that this is only a shallow copy, if used after Run() is called, race conditions may occur.
type MessageHandler ¶
type MessageHandler func(ctx context.Context, bot *Bot, msg *slack.MessageEvent)
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) error
func (*Route) Messages ¶
func (r *Route) Messages(types ...MessageType) *Route
func (*Route) NoTalkToSelf ¶
func (*Route) Preprocess ¶
func (r *Route) Preprocess(fn Preprocessor) *Route
func (*Route) TalkToSelf ¶
type RouteMatch ¶
RouteMatch stores information about a matched route.
type Router ¶
type Router interface { Match(context.Context, *RouteMatch) (bool, context.Context) SetBotID(botID string) Hear(regex string) *Route Messages(types ...MessageType) *Route AddMatcher(m Matcher) *Route TalkToSelf() *Route NoTalkToSelf() *Route AlwaysTalkToSelf() Router NeverTalkToSelf() Router Handler(handler Handler) error MessageHandler(handler MessageHandler) error Err() error }
type SimpleRouter ¶
type SimpleRouter struct {
// contains filtered or unexported fields
}
func (*SimpleRouter) AddMatcher ¶
func (r *SimpleRouter) AddMatcher(m Matcher) *Route
func (*SimpleRouter) AlwaysTalkToSelf ¶
func (r *SimpleRouter) AlwaysTalkToSelf() Router
func (*SimpleRouter) Err ¶
func (r *SimpleRouter) Err() error
func (*SimpleRouter) Handler ¶
func (r *SimpleRouter) Handler(handler Handler) error
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) error
func (*SimpleRouter) Messages ¶
func (r *SimpleRouter) Messages(types ...MessageType) *Route
func (*SimpleRouter) NeverTalkToSelf ¶
func (r *SimpleRouter) NeverTalkToSelf() Router
func (*SimpleRouter) NoTalkToSelf ¶
func (r *SimpleRouter) NoTalkToSelf() *Route
func (*SimpleRouter) SetBotID ¶
func (r *SimpleRouter) SetBotID(botID string)
func (*SimpleRouter) TalkToSelf ¶
func (r *SimpleRouter) TalkToSelf() *Route
type TypesMatcher ¶
type TypesMatcher struct {
// contains filtered or unexported fields
}
func (*TypesMatcher) SetBotID ¶
func (tm *TypesMatcher) SetBotID(botID string)