slackbot

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: MIT Imports: 9 Imported by: 1

README

slackbot

Test GoDoc

Basic slackbot implementation

Authors

  • Christophe Lambin

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Overview

Package slackbot provides a basic slackbot implementation. Using this package typically involves creating a bot as follows:

bot := slackbot.New("some-token", slackbot.WithCommands(...)
go bot.Run(context.Background())

Once running, the bot connects to Slack and listens for any commands and execute them. Slackbot itself implements two commands: "version" (which responds with the bot's name; see WithName option) and "help" (which shows all supported commands).

Applications can send messages as follows:

bot.Send(channel, []slack.Attachment{{Text: "Hello world"}})
Example (Nested)
package main

import (
	"context"
	"fmt"
	"github.com/clambin/go-common/slackbot"
	"github.com/slack-go/slack"
	"strings"
)

func main() {
	b := slackbot.New("my-slack-token", slackbot.WithCommands(slackbot.Commands{
		"hello": slackbot.HandlerFunc(func(ctx context.Context, s ...string) []slack.Attachment {
			return []slack.Attachment{{Color: "good", Text: "General Kenobi!"}}
		}),
		"say": &slackbot.Commands{
			"foo": slackbot.HandlerFunc(func(ctx context.Context, s ...string) []slack.Attachment {
				return []slack.Attachment{{Text: "foo"}}
			}),
			"bar": slackbot.HandlerFunc(func(ctx context.Context, s ...string) []slack.Attachment {
				return []slack.Attachment{{Text: "bar"}}
			}),
		},
	}))

	fmt.Println("Commands: " + strings.Join(b.GetCommands(), ", "))
}
Output:

Commands: hello, help, say, version
Example (Simple)
package main

import (
	"context"
	"fmt"
	"github.com/clambin/go-common/slackbot"
	"github.com/slack-go/slack"
	"strings"
)

func main() {
	b := slackbot.New("my-slack-token", slackbot.WithCommands(slackbot.Commands{
		"hello": slackbot.HandlerFunc(func(_ context.Context, _ ...string) []slack.Attachment {
			return []slack.Attachment{{Color: "good", Text: "General Kenobi!"}}
		}),
	}))

	fmt.Println("Commands: " + strings.Join(b.GetCommands(), ", "))
}
Output:

Commands: hello, help, version

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Commands added in v0.7.0

type Commands map[string]Handler

Commands is a map of verb/Handler pairs.

Note that Commands itself implements the Handler interface. This allows nested command structures to be built:

Commands
"foo"    -> handler
"bar"    -> Commands
            "snafu"    -> handler

This creates the commands "foo" and "bar snafu"

func (Commands) Add added in v0.7.0

func (c Commands) Add(commands Commands)

Add adds one or more commands.

func (Commands) GetCommands added in v0.7.0

func (c Commands) GetCommands() []string

GetCommands returns a sorted list of all supported commands.

func (Commands) Handle added in v0.7.0

func (c Commands) Handle(ctx context.Context, args ...string) []slack.Attachment

Handle processes the incoming command. The first arg is considered the verb. If it matches a supported command, its corresponding handler is called, passing the remaining arguments.

If the verb is not supported, an attachment is returned with all supported commands.

type Handler added in v0.7.0

type Handler interface {
	Handle(context.Context, ...string) []slack.Attachment
}

A Handler executes a command and returns messages to be posted to Slack.

type HandlerFunc added in v0.7.0

type HandlerFunc func(context.Context, ...string) []slack.Attachment

HandlerFunc is an adapter that allows a function to be used as a Handler

func (HandlerFunc) Handle added in v0.7.0

func (f HandlerFunc) Handle(ctx context.Context, args ...string) []slack.Attachment

Handle calls f(ctx, args)

type Option added in v0.4.0

type Option func(*SlackBot)

Option is the function signature for any options for New().

func WithCommands added in v0.4.0

func WithCommands(commands Commands) Option

WithCommands adds a set of provided commands to the SlackBot. For more complex Command structures, use AddCommand & AddCommandGroup. and Command.AddCommandGroup() after creating the SlackBot with New().

func WithLogger added in v0.4.0

func WithLogger(logger *slog.Logger) Option

WithLogger sets the slog Logger. By default, SlackBot uses slog.Default().

func WithName added in v0.4.0

func WithName(name string) Option

WithName sets the name of the SlackBot. The name is currently only used in the version Command.

type SlackBot

type SlackBot struct {
	Commands
	// contains filtered or unexported fields
}

SlackBot connects to Slack through Slack's Bot integration.

func New

func New(slackToken string, options ...Option) *SlackBot

New creates a new slackbot

func (*SlackBot) Run

func (b *SlackBot) Run(ctx context.Context) error

Run the slackbot

func (*SlackBot) Send

func (b *SlackBot) Send(channel string, attachments []slack.Attachment) error

Send posts attachments to Slack on the provided channel. If channel is blank, Send posts to all channels that the bot has access to.

type SlackClient added in v0.7.0

type SlackClient interface {
	Run(context.Context)
	GetMessage() chan *slack.MessageEvent
	Send(channelID string, attachments []slack.Attachment) error
	GetUserID() (string, error)
	GetChannels() ([]string, error)
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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