Skip to content

Commit bc3ade5

Browse files
authored
V2 Foundation: Groups, Middlewares, Simplified Interfaces, ... (#134)
feat: Slacker V2 - Simplified Interfaces - Added support for Groups, Middleware - Enhanced support for Interaction Callbacks - Enhanced support for Cron Jobs - Added support for more message actions - Improved examples and documentation
1 parent c38812c commit bc3ade5

59 files changed

Lines changed: 2070 additions & 2432 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 13 additions & 1022 deletions
Large diffs are not rendered by default.

analytics.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

bots.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package slacker
22

3-
// BotInteractionMode instruct the bot on how to handle incoming events that
3+
// BotMode instruct the bot on how to handle incoming events that
44
// originated from a bot.
5-
type BotInteractionMode int
5+
type BotMode int
66

77
const (
8-
// BotInteractionModeIgnoreAll instructs our bot to ignore any activity coming
8+
// BotModeIgnoreAll instructs our bot to ignore any activity coming
99
// from other bots, including our self.
10-
BotInteractionModeIgnoreAll BotInteractionMode = iota
10+
BotModeIgnoreAll BotMode = iota
1111

12-
// BotInteractionModeIgnoreApp will ignore any events that originate from a
12+
// BotModeIgnoreApp will ignore any events that originate from a
1313
// bot that is associated with the same App (ie. share the same App ID) as
1414
// this bot. OAuth scope `user:read` is required for this mode.
15-
BotInteractionModeIgnoreApp
15+
BotModeIgnoreApp
1616

17-
// BotInteractionModeIgnoreNone will not ignore any bots, including our self.
17+
// BotModeIgnoreNone will not ignore any bots, including our self.
1818
// This can lead to bots "talking" to each other so care must be taken when
1919
// selecting this option.
20-
BotInteractionModeIgnoreNone
20+
BotModeIgnoreNone
2121
)

command.go

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,42 @@ package slacker
33
import (
44
"github.com/shomali11/commander"
55
"github.com/shomali11/proper"
6-
"github.com/slack-go/slack"
7-
"github.com/slack-go/slack/socketmode"
86
)
97

108
// CommandDefinition structure contains definition of the bot command
119
type CommandDefinition struct {
12-
Description string
13-
Examples []string
14-
BlockID string
15-
AuthorizationFunc func(BotContext, Request) bool
16-
Handler func(BotContext, Request, ResponseWriter)
17-
Interactive func(InteractiveBotContext, *socketmode.Request, *slack.InteractionCallback)
10+
Command string
11+
Description string
12+
Examples []string
13+
Middlewares []CommandMiddlewareHandler
14+
Handler CommandHandler
1815

1916
// HideHelp will hide this command definition from appearing in the `help` results.
2017
HideHelp bool
2118
}
2219

23-
// NewCommand creates a new bot command object
24-
func NewCommand(usage string, definition *CommandDefinition) Command {
20+
// newCommand creates a new bot command object
21+
func newCommand(definition *CommandDefinition) Command {
2522
return &command{
26-
usage: usage,
2723
definition: definition,
28-
cmd: commander.NewCommand(usage),
24+
cmd: commander.NewCommand(definition.Command),
2925
}
3026
}
3127

3228
// Command interface
3329
type Command interface {
34-
Usage() string
3530
Definition() *CommandDefinition
3631

3732
Match(string) (*proper.Properties, bool)
3833
Tokenize() []*commander.Token
39-
Execute(BotContext, Request, ResponseWriter)
40-
Interactive(InteractiveBotContext, *socketmode.Request, *slack.InteractionCallback)
4134
}
4235

4336
// command structure contains the bot's command, description and handler
4437
type command struct {
45-
usage string
4638
definition *CommandDefinition
4739
cmd *commander.Command
4840
}
4941

50-
// Usage returns the command usage
51-
func (c *command) Usage() string {
52-
return c.usage
53-
}
54-
5542
// Definition returns the command definition
5643
func (c *command) Definition() *CommandDefinition {
5744
return c.definition
@@ -66,19 +53,3 @@ func (c *command) Match(text string) (*proper.Properties, bool) {
6653
func (c *command) Tokenize() []*commander.Token {
6754
return c.cmd.Tokenize()
6855
}
69-
70-
// Execute executes the handler logic
71-
func (c *command) Execute(botCtx BotContext, request Request, response ResponseWriter) {
72-
if c.definition == nil || c.definition.Handler == nil {
73-
return
74-
}
75-
c.definition.Handler(botCtx, request, response)
76-
}
77-
78-
// Interactive executes the interactive logic
79-
func (c *command) Interactive(botContext InteractiveBotContext, request *socketmode.Request, callback *slack.InteractionCallback) {
80-
if c.definition == nil || c.definition.Interactive == nil {
81-
return
82-
}
83-
c.definition.Interactive(botContext, request, callback)
84-
}

command_group.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package slacker
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// newGroup creates a new CommandGroup with a prefix
9+
func newGroup(prefix string) *CommandGroup {
10+
return &CommandGroup{prefix: prefix}
11+
}
12+
13+
// CommandGroup groups commands with a common prefix and middlewares
14+
type CommandGroup struct {
15+
prefix string
16+
middlewares []CommandMiddlewareHandler
17+
commands []Command
18+
}
19+
20+
// AddMiddleware define a new middleware and append it to the list of group middlewares
21+
func (g *CommandGroup) AddMiddleware(middleware CommandMiddlewareHandler) {
22+
g.middlewares = append(g.middlewares, middleware)
23+
}
24+
25+
// AddCommand define a new command and append it to the list of group bot commands
26+
func (g *CommandGroup) AddCommand(definition *CommandDefinition) {
27+
definition.Command = strings.TrimSpace(fmt.Sprintf("%s %s", g.prefix, definition.Command))
28+
g.commands = append(g.commands, newCommand(definition))
29+
}
30+
31+
// PrependCommand define a new command and prepend it to the list of group bot commands
32+
func (g *CommandGroup) PrependCommand(definition *CommandDefinition) {
33+
definition.Command = strings.TrimSpace(fmt.Sprintf("%s %s", g.prefix, definition.Command))
34+
g.commands = append([]Command{newCommand(definition)}, g.commands...)
35+
}
36+
37+
// GetPrefix returns the group's prefix
38+
func (g *CommandGroup) GetPrefix() string {
39+
return g.prefix
40+
}
41+
42+
// GetCommands returns Commands
43+
func (g *CommandGroup) GetCommands() []Command {
44+
return g.commands
45+
}
46+
47+
// GetMiddlewares returns Middlewares
48+
func (g *CommandGroup) GetMiddlewares() []CommandMiddlewareHandler {
49+
return g.middlewares
50+
}

0 commit comments

Comments
 (0)