slackbot

package module
v0.0.0-...-3c34a03 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2021 License: MIT Imports: 6 Imported by: 2

README

go-slackbot - Build Slackbots in Go

The go-slackbot project 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)

In addition to several useful functions in the utils.go file, the slackbot.Bot struct provides handy 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)
}

But wait, there's more! Well, until there's more, 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")
}

If you want to kick the tires, we would love feedback. Check out these two examples:

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://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/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/lusis/go-slackbot

Index

Constants

View Source
const (
	// WithTyping sends a message with typing indicator
	WithTyping bool = true
	// WithoutTyping sends a message without typing indicator
	WithoutTyping bool = false
)
View Source
const (
	// BotContext is the context key for the bot context entry
	BotContext = "__BOT_CONTEXT__"
	// MessageContext is the context key for the message context entry
	MessageContext = "__MESSAGE_CONTEXT__"
	// NamedCaptureContextKey is the key for named captures
	NamedCaptureContextKey = "__NAMED_CAPTURES__"
)

Variables

This section is empty.

Functions

func AddBotToContext

func AddBotToContext(ctx context.Context, bot *Bot) context.Context

AddBotToContext sets the bot reference in context and returns the newly derived context

func AddMessageToContext

func AddMessageToContext(ctx context.Context, msg *slack.MessageEvent) context.Context

AddMessageToContext sets the Slack message event reference in context and returns the newly derived context

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

MessageFromContext gets the message from the provided context

func StripDirectMention

func StripDirectMention(text string) string

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
	// contains filtered or unexported fields
}

Bot is a bot

func BotFromContext

func BotFromContext(ctx context.Context) *Bot

BotFromContext creates a Bot from provided Context

func New

func New(slackToken string) *Bot

New constructs a new Bot using the slackToken to authorize against the Slack service.

func NewWithLogger

func NewWithLogger(slackToken string, l *log.Logger) *Bot

NewWithLogger constructs a new Bot using the slackToken and custom logger instance provided

func (*Bot) BotUserID

func (b *Bot) BotUserID() string

BotUserID fetches the botUserID.

func (*Bot) OnChannelJoin

func (b *Bot) OnChannelJoin(h ChannelJoinHandler)

OnChannelJoin handles ChannelJoin events

func (*Bot) OnUnhandledEvent

func (b *Bot) OnUnhandledEvent(h EventHandler)

OnUnhandledEvent handles any events not already handled

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 listens for incoming slack RTM events, matching them to an appropriate handler.

func (*Bot) SetLogger

func (b *Bot) SetLogger(l *log.Logger)

SetLogger sets the bots logger to a custom one

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 ChannelJoinHandler

type ChannelJoinHandler func(context.Context, *Bot, *slack.Channel)

ChannelJoinHandler handles channel join events

type ChannelJoinMatch

type ChannelJoinMatch struct {
	Handler ChannelJoinHandler
}

ChannelJoinMatch stores information about a channel joined event

func (*ChannelJoinMatch) Handle

func (cjm *ChannelJoinMatch) Handle(ctx context.Context, b *Bot, channel *slack.Channel)

Handle calls the handler with provided parameters

type EventHandler

type EventHandler func(context.Context, *Bot, *slack.RTMEvent)

EventHandler handles events in a generic fashion

type EventMatch

type EventMatch struct {
	Handler EventHandler
}

EventMatch stores information about a matched event

func (*EventMatch) Handle

func (em *EventMatch) Handle(ctx context.Context, b *Bot, ev *slack.RTMEvent)

Handle handles any unspecified RTM events

type Handler

type Handler func(context.Context)

Handler is a handler

type Matcher

type Matcher interface {
	Match(context.Context) (bool, context.Context)
	SetBotID(botID string)
}

Matcher type for matching message routes

type MessageHandler

type MessageHandler func(ctx context.Context, bot *Bot, msg *slack.MessageEvent)

MessageHandler is a message handler

type MessageType

type MessageType string

MessageType represents a message type

const (
	// DirectMessage represents a message type
	DirectMessage MessageType = "direct_message"
	// DirectMention represents a direct message
	DirectMention MessageType = "direct_mention"
	// Mention is a mention
	Mention MessageType = "mention"
	// Ambient is ambient
	Ambient MessageType = "ambient"
)

type NamedCaptures

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

NamedCaptures is a container for any named captures in our context

func NamedCapturesFromContext

func NamedCapturesFromContext(ctx context.Context) NamedCaptures

NamedCapturesFromContext returns any NamedCaptures parsed from regexp

func (NamedCaptures) Get

func (nc NamedCaptures) Get(key string) string

Get returns a value from a key lookup

type Preprocessor

type Preprocessor func(context.Context) context.Context

Preprocessor is a preprocessor

type RegexpMatcher

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

RegexpMatcher is a regexp matcher

func (*RegexpMatcher) Match

func (rm *RegexpMatcher) Match(ctx context.Context) (bool, context.Context)

Match matches a message

func (*RegexpMatcher) SetBotID

func (rm *RegexpMatcher) SetBotID(botID string)

SetBotID sets the bot id

type Route

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

Route represents a route

func (*Route) AddMatcher

func (r *Route) AddMatcher(m Matcher) *Route

AddMatcher adds a matcher to the route.

func (*Route) Handler

func (r *Route) Handler(handler Handler) *Route

Handler sets a handler for the route.

func (*Route) Hear

func (r *Route) Hear(regex string) *Route

Hear adds a matcher for the message text

func (*Route) Match

func (r *Route) Match(ctx context.Context, match *RouteMatch) (bool, context.Context)

Match matches

func (*Route) MessageHandler

func (r *Route) MessageHandler(fn MessageHandler) *Route

MessageHandler is a message handler

func (*Route) Messages

func (r *Route) Messages(types ...MessageType) *Route

Messages sets the types of Messages we want to handle

func (*Route) Preprocess

func (r *Route) Preprocess(fn Preprocessor) *Route

Preprocess preproccesses

func (*Route) Subrouter

func (r *Route) Subrouter() Router

Subrouter creates a subrouter

type RouteMatch

type RouteMatch struct {
	Route   *Route
	Handler Handler
}

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
	Messages(types ...MessageType) *Route
	AddMatcher(m Matcher) *Route
	SetBotID(botID string)
}

Router represents a router

type SimpleRouter

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

SimpleRouter represents a simple router

func (*SimpleRouter) AddMatcher

func (r *SimpleRouter) AddMatcher(m Matcher) *Route

AddMatcher adds a matcher

func (*SimpleRouter) Handler

func (r *SimpleRouter) Handler(handler Handler) *Route

Handler handles

func (*SimpleRouter) Hear

func (r *SimpleRouter) Hear(regex string) *Route

Hear hears

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

MessageHandler is a message handler

func (*SimpleRouter) Messages

func (r *SimpleRouter) Messages(types ...MessageType) *Route

Messages is for messages

func (*SimpleRouter) NewRoute

func (r *SimpleRouter) NewRoute() *Route

NewRoute registers an empty route.

func (*SimpleRouter) SetBotID

func (r *SimpleRouter) SetBotID(botID string)

SetBotID sets the bot id

type TypesMatcher

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

TypesMatcher is a type matcher

func (*TypesMatcher) Match

func (tm *TypesMatcher) Match(ctx context.Context) (bool, context.Context)

Match matches

func (*TypesMatcher) SetBotID

func (tm *TypesMatcher) SetBotID(botID string)

SetBotID sets the botid

Jump to

Keyboard shortcuts

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