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
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) GetCommands ¶ added in v0.7.0
GetCommands returns a sorted list of all supported commands.
func (Commands) Handle ¶ added in v0.7.0
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
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
WithLogger sets the slog Logger. By default, SlackBot uses slog.Default().
type SlackBot ¶
type SlackBot struct { Commands // contains filtered or unexported fields }
SlackBot connects to Slack through Slack's Bot integration.