/slot

Slot is a golang slack bot library for simplifying common slack bot use cases

Primary LanguageGoApache License 2.0Apache-2.0

GoDoc Build Status Coverage Status Go Report Card

slot

-- import "astuart.co/slot"

package slot gives some helpful abstractions over the nlopes/slack RTM integrations. Most use cases are intended to be made easier. The common bot response abstraction is the Responder. Many implementations will be created to assist in most of the common bot use cases.

Usage

func GetAction

func GetAction(ev *slack.MessageEvent) string

GetAction takes an event and returns either the empty string, or the first !action string in the message text.

func MaybeRespond

func MaybeRespond(r *slack.RTM, ev *slack.MessageEvent, res Responder) error

MaybeRespond checks if a Responder is a MatchResponder and conditionally exits if there is no match.

type ActionMap

type ActionMap map[string]Responder

ActionMap holds action words and responders, calling the appropriate responder when an !action message is received.

func (ActionMap) Match

func (m ActionMap) Match(r *slack.RTM, ev *slack.MessageEvent) bool

Match implements MatchResponder for efficiency

func (ActionMap) Respond

func (m ActionMap) Respond(r *slack.RTM, ev *slack.MessageEvent) error

Respond implements Responder

type Bot

type Bot struct {
	Responders []Responder
}

A Bot handles a client

func (*Bot) Handle

func (b *Bot) Handle(cli *slack.Client) error

Handle manages an RTM based on the configured Handlers

type BotMentionAction

type BotMentionAction struct {
	FollowingText string
	Responder     Responder
}

BotMentionAction executes a responder if the bot's name is @mentioned

func (*BotMentionAction) Match

func (b *BotMentionAction) Match(rtm *slack.RTM, ev *slack.MessageEvent) bool

Match implements MatchResponder

func (*BotMentionAction) Respond

func (b *BotMentionAction) Respond(rtm *slack.RTM, ev *slack.MessageEvent) error

Respond implements Responder

type MatchResponder

type MatchResponder interface {
	Responder
	Match(*slack.RTM, *slack.MessageEvent) bool
}

A MatchResponder conditionally acts on a message

type RegexResponder

type RegexResponder struct {
	Regexp    *regexp.Regexp
	Responder Responder
}

RegexResponder matches a regex against an incoming string and executes a response if a match occurred

func (*RegexResponder) Match

func (r *RegexResponder) Match(rtm *slack.RTM, ev *slack.MessageEvent) bool

Match implements MatchResponder

func (*RegexResponder) Respond

func (r *RegexResponder) Respond(rtm *slack.RTM, ev *slack.MessageEvent) error

Respond implements Responder

type Responder

type Responder interface {
	Respond(*slack.RTM, *slack.MessageEvent) error
}

A Responder handles an event

type ResponderFunc

type ResponderFunc func(*slack.RTM, *slack.MessageEvent) error

An ResponderFunc is a function that can respond to a slack event

func (ResponderFunc) Respond

func (f ResponderFunc) Respond(r *slack.RTM, ev *slack.MessageEvent) error

Respond implements Responder.

type StringFuncResponder

type StringFuncResponder func() string

StringFuncResponder always responds with the result of calling the function

func (StringFuncResponder) Respond

func (s StringFuncResponder) Respond(r *slack.RTM, ev *slack.MessageEvent) error

Respond implements Responder

type TextResponder

type TextResponder string

TextResponder always responds with a string

func (TextResponder) Respond

func (p TextResponder) Respond(r *slack.RTM, ev *slack.MessageEvent) error

Respond implements Responder