quadlek

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: Apache-2.0 Imports: 21 Imported by: 5

Documentation

Overview

quadlek is a slack Bot that is built on top of the nlopes Slack client.

For a good source of examples, look at the included plugins at https://github.com/jirwin/quadlek/tree/master/plugins.

Read more about the client and Slack APIs at: https://github.com/nlopes/slack and https://api.slack.com

Index

Constants

This section is empty.

Variables

View Source
var InvalidRequestSignature = errors.New("invalid request signature")

Functions

This section is empty.

Types

type Bot

type Bot struct {
	Log *zap.Logger
	// contains filtered or unexported fields
}

This is the core struct for the Bot, and provides all methods required for interacting with various Slack APIs.

An instance of the bot is provided to plugins to enable plugins to interact with the Slack API.

func NewBot

func NewBot(parentCtx context.Context, apiKey, verificationToken, dbPath string, debug bool) (*Bot, error)

NewBot creates a new instance of Bot for use.

apiKey is the Slack API key that the Bot should use to authenticate

verificationToken is the webhook token that is used to validate webhooks are coming from slack

dbPath is the path to the database on the filesystem.

func (*Bot) GetApi

func (b *Bot) GetApi() *slack.Client

GetApi returns the Slack API client. You can use this client to perform actions that use the Slack Web API. See https://api.slack.com/web for more details.

func (*Bot) GetBotId

func (b *Bot) GetBotId() string

GetBotId returns the Slack bot ID

func (*Bot) GetChannel

func (b *Bot) GetChannel(chanId string) (*slack.Channel, error)

GetChannel returns the Slack channel object given a channel ID

func (*Bot) GetChannelId

func (b *Bot) GetChannelId(chanName string) (string, error)

GetChannelId returns the Slack channel ID for a given human-readable channel name.

func (*Bot) GetCommand

func (b *Bot) GetCommand(cmdText string) *registeredCommand

GetCommand returns the registeredCommand for the provided command name

func (*Bot) GetInteraction

func (b *Bot) GetInteraction(callbackID string) *registeredInteraction

GetWebhook returns the registeredWebhook for the given webhook name

func (*Bot) GetMessage

func (b *Bot) GetMessage(channel, ts string) (slack.Message, error)

func (*Bot) GetMessageLog

func (b *Bot) GetMessageLog(channel string, opts MessageLotOpts) ([]slack.Message, error)

GetMessageLog uses channel and a set of options to get historical messages from the Slack API.

func (*Bot) GetUser

func (b *Bot) GetUser(userId string) (*slack.User, error)

GetUser returns the Slack user object given a user ID

func (*Bot) GetUserID added in v0.0.7

func (b *Bot) GetUserID(userName string) (string, error)

GetUserID returns the slack user name for a human readable username

func (*Bot) GetUserId

func (b *Bot) GetUserId() string

GetUserId returns the Slack user ID for the Bot.

func (*Bot) GetUserName

func (b *Bot) GetUserName(userId string) (string, error)

GetUserName returns the human-readable user name for a given user ID

func (*Bot) GetWebhook

func (b *Bot) GetWebhook(name string) *registeredWebhook

GetWebhook returns the registeredWebhook for the given webhook name

func (*Bot) InitPluginBucket

func (b *Bot) InitPluginBucket(pluginId string) error

InitPluginBucket initializes the database bucket for the given pluginId

func (*Bot) MsgToBot

func (b *Bot) MsgToBot(msg string) bool

MsgToBot returns true if the message was intended for the Bot

func (*Bot) OpenView

func (b *Bot) OpenView(triggerID string, response slack.ModalViewRequest) (*slack.ViewResponse, error)

func (*Bot) PostMessage

func (b *Bot) PostMessage(channel string, options ...slack.MsgOption) (string, string, error)

PostMessage sends a new message to Slack using the provided channel and message string. It returns the channel ID the message was posted to, and the timestamp that the message was posted at. In combination these can be used to identify the exact message that was sent.

func (*Bot) React

func (b *Bot) React(msg *slack.Msg, reaction string)

React attaches an emojii reaction to a message. Reactions are formatted like: :+1:

func (*Bot) RegisterPlugin

func (b *Bot) RegisterPlugin(p interface{}) error

RegisterPlugin registers the given Plugin with the Bot.

func (*Bot) Respond

func (b *Bot) Respond(msg *slack.Msg, resp string)

Respond responds to a Slack message The sent message will go to the same channel as the message that is being responded to and will highlight the author of the original message.

func (*Bot) RespondToSlashCommand

func (b *Bot) RespondToSlashCommand(url string, cmdResp *CommandResp) error

RespondToSlashCommand sends a command response to the slack API in order to respond to a slash command.

func (*Bot) Say

func (b *Bot) Say(channel string, resp string)

Say sends a message to the provided channel

func (*Bot) Start

func (b *Bot) Start()

Start activates the Bot, creating a new API client. It also calls out to the Slack API to obtain all of the channels and users.

func (*Bot) Stop

func (b *Bot) Stop()

Stop cancel's the bots main context, closes the DB handle, and disconnects from slack

func (*Bot) ValidateSlackRequest

func (b *Bot) ValidateSlackRequest(r *http.Request) error

Validates the signature header for slack webhooks

func (*Bot) WebhookServer

func (b *Bot) WebhookServer()

WebhookServer starts a new http server that listens and responds to incoming webhooks. The Slack API uses webhooks for processing slash commands, and this server is used to respond to them. Plugins can also register custom webhooks that can be used however they choose. An example of this would be to process oauth2 callbacks to facilitate oauth2 flows for associating a user's slack account with an external service.

type Command

type Command interface {
	GetName() string
	Channel() chan<- *CommandMsg
	Run(ctx context.Context)
}

Command is the interface that plugins implement for slash commands. Slash commands are actively triggered by users in slack, and only receive messages when they are invoked.

func MakeCommand

func MakeCommand(name string, runFn func(ctx context.Context, cmdChan <-chan *CommandMsg)) Command

MakeCommand is a helper function that accepts a name and a runFunc, and returns a Command.

type CommandMsg

type CommandMsg struct {
	Bot     *Bot
	Command *slashCommand
	Store   *Store
}

CommandMsg is the struct that is passed to a commands channel as it is activated.

type CommandPlugin

type CommandPlugin interface {
	Plugin
	GetCommands() []Command
}

type CommandResp

type CommandResp struct {
	Text         string             `json:"text"`
	Attachments  []slack.Attachment `json:"attachments"`
	ResponseType string             `json:"response_type"`
	InChannel    bool               `json:"-"`
}

CommandResp is the struct that is used to respond to a command if interaction is required.

type Hook

type Hook interface {
	Channel() chan<- *HookMsg
	Run(ctx context.Context)
}

Hook is the interface that a plugin can implement to create a hook.

Hooks receive every message that the Bot sees so plugins can react accordingly.

func MakeHook

func MakeHook(runFunc func(ctx context.Context, hookChan <-chan *HookMsg)) Hook

MakeHook is a helper function that accepts a runFunc and returns a Hook

type HookMsg

type HookMsg struct {
	Bot   *Bot
	Msg   *slack.Msg
	Store *Store
}

HookMsg is the struct that is passed to a hook's channel for each message seen.

type HookPlugin

type HookPlugin interface {
	Plugin
	GetHooks() []Hook
}

type Interaction

type Interaction interface {
	GetName() string
	Channel() chan<- *InteractionMsg
	Run(ctx context.Context)
}

Interaction is the interface that plugins implement for slash Shortcuts. Slash Shortcuts are actively triggered by users in slack, and only receive messages when they are invoked.

func MakeInteraction

func MakeInteraction(name string, runFn func(ctx context.Context, cmdChan <-chan *InteractionMsg)) Interaction

MakeInteraction is a helper function that accepts a name and a runFunc, and returns a Interaction.

type InteractionMsg

type InteractionMsg struct {
	Bot         *Bot
	Interaction *slack.InteractionCallback
	Store       *Store
}

InteractionMsg is the struct that is passed to a Shortcuts channel as it is activated.

type InteractionPlugin

type InteractionPlugin interface {
	GetId() string
	GetInteractions() []Interaction
}

func MakeInteractionPlugin

func MakeInteractionPlugin(id string, plugins []Interaction) InteractionPlugin

MakePlugin is a helper function that returns a Plugin.

type LoadPlugin

type LoadPlugin interface {
	Plugin
	Load(bot *Bot, store *Store) error
}

type MessageLotOpts

type MessageLotOpts struct {
	IncludeBots     bool
	Count           int
	Period          time.Duration
	SkipAttachments bool
}

MessageLotOpts is the stuct that you use to configure what messages you want to retrieve from the API.

IncludeBots: If true, include messages from bots(not just quadlek bots)

Count: The max number of messages to return

Period: The amount of time to look backwards when looking for messages

SkipAttachments: If true, don't return message attachments.

type Plugin

type Plugin interface {
	GetId() string
}

Plugin is the interface to implement a plugin

func MakePlugin

func MakePlugin(id string, commands []Command, hooks []Hook, reactionHooks []ReactionHook, webhooks []Webhook, loadFunction loadPluginFn) Plugin

MakePlugin is a helper function that returns a Plugin.

type PluginWebhook

type PluginWebhook struct {
	Name           string
	Request        *http.Request
	ResponseWriter http.ResponseWriter
}

PluginWebhook stores an incoming web request to be passed to a plugin

type ReactionHook

type ReactionHook interface {
	Channel() chan<- *ReactionHookMsg
	Run(ctx context.Context)
}

ReactionHook is the interface that plugins implement to create reaction hooks. Reaction hooks receive an event every time a message is reacted to.

func MakeReactionHook

func MakeReactionHook(runFunc func(ctx context.Context, reactionHookChan <-chan *ReactionHookMsg)) ReactionHook

MakeReactionHook is a helper function that returns a ReactionHook

type ReactionHookMsg

type ReactionHookMsg struct {
	Bot      *Bot
	Reaction *slackevents.ReactionAddedEvent
	Store    *Store
}

ReactionHookMsg is the struct that is sent to a reaction hook when a message is reacted to.

type ReactionHookPlugin

type ReactionHookPlugin interface {
	Plugin
	GetReactionHooks() []ReactionHook
}

type Store

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

The Store struct provides a plugin a namespaced key value store for the plugin to use however it needs. By default, keys are strings, and values are []byte. You can use UpdateRaw() if this doesn't fit your needs.

func (*Store) ForEach

func (s *Store) ForEach(forEachFunc func(bucket *bolt.Bucket, key string, value []byte) error) error

func (*Store) Get

func (s *Store) Get(key string, getFunc func([]byte) error) error

Get retrieves a key from the database and passes it to the provided getFunc

func (*Store) GetAndUpdate

func (s *Store) GetAndUpdate(key string, updateFunc func([]byte) ([]byte, error)) error

GetAndUpdate retrieves a key from the database and passes its value to the provided updateFunc. This allows you to transform data atomically.

func (*Store) Update

func (s *Store) Update(key string, value []byte) error

Update stores the value at the provided key

func (*Store) UpdateRaw

func (s *Store) UpdateRaw(updateFunc func(*bolt.Bucket) error) error

UpdateRaw allows you direct access to the database when the simple key value interface doesn't work.

type Webhook

type Webhook interface {
	GetName() string
	Channel() chan<- *WebhookMsg
	Run(ctx context.Context)
}

Webhook is the interface that a plugin implements to register a custom webhook.

func MakeWebhook

func MakeWebhook(name string, runFunc func(ctx context.Context, whChan <-chan *WebhookMsg)) Webhook

MakeWebhook is a helper function that returns a Webhook

type WebhookMsg

type WebhookMsg struct {
	Bot            *Bot
	Request        *http.Request
	ResponseWriter http.ResponseWriter
	Store          *Store
	Done           chan bool
}

WebhookMsg is the struct that is sent to the plugin's channel

type WebhookPlugin

type WebhookPlugin interface {
	Plugin
	GetWebhooks() []Webhook
}

Jump to

Keyboard shortcuts

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