Skip to content

Commit

Permalink
feat(slackbot): support nested commands
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin committed Jan 27, 2024
1 parent 68c7ca5 commit bb514ce
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
5 changes: 2 additions & 3 deletions slackbot/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func Example_simple() {
}

func Example_nested() {
b := slackbot.New("my-slack-token")
b.Add(slackbot.Commands{
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!"}}
}),
Expand All @@ -33,7 +32,7 @@ func Example_nested() {
return []slack.Attachment{{Text: "bar"}}
}),
},
})
}))

fmt.Println("Commands: " + strings.Join(b.GetCommands(), ", "))
// Output: Commands: hello, help, say, version
Expand Down
2 changes: 1 addition & 1 deletion slackbot/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func WithName(name string) Option {
// and Command.AddCommandGroup() after creating the SlackBot with New().
func WithCommands(commands Commands) Option {
return func(b *SlackBot) {
b.Commands.Add(commands)
b.commands.Add(commands)
}
}

Expand Down
4 changes: 2 additions & 2 deletions slackbot/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func TestWithLogger(t *testing.T) {

func TestWithCommands(t *testing.T) {
b := New("123")
assert.Equal(t, []string{"help", "version"}, b.GetCommands())
assert.Equal(t, []string{"help", "version"}, b.commands.GetCommands())

b = New("123", WithCommands(Commands{
"test": HandlerFunc(func(_ context.Context, args ...string) []slack.Attachment {
return nil
}),
}))
assert.Equal(t, []string{"help", "test", "version"}, b.GetCommands())
assert.Equal(t, []string{"help", "test", "version"}, b.commands.GetCommands())
}
17 changes: 11 additions & 6 deletions slackbot/slackbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

// SlackBot connects to Slack through Slack's Bot integration.
type SlackBot struct {
Commands
client SlackClient
name string
logger *slog.Logger
client SlackClient
commands Commands
name string
logger *slog.Logger
}

type SlackClient interface {
Expand All @@ -33,7 +33,7 @@ func New(slackToken string, options ...Option) *SlackBot {
name: "slackbot",
logger: slog.Default(),
}
b.Commands = Commands{
b.commands = Commands{
"help": HandlerFunc(b.doHelp),
"version": HandlerFunc(b.doVersion),
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func (b *SlackBot) processMessage(ctx context.Context, message *slack.MessageEve
}

b.logger.Debug("running command", "args", args)
output := b.Handle(ctx, args...)
output := b.commands.Handle(ctx, args...)

return b.Send(message.Channel, output)
}
Expand Down Expand Up @@ -150,3 +150,8 @@ func (b *SlackBot) doHelp(_ context.Context, _ ...string) []slack.Attachment {
func (b *SlackBot) doVersion(_ context.Context, _ ...string) []slack.Attachment {
return []slack.Attachment{{Color: "good", Text: b.name}}
}

// GetCommands returns a sorted list of all supported commands.
func (b *SlackBot) GetCommands() []string {
return b.commands.GetCommands()
}

0 comments on commit bb514ce

Please sign in to comment.