forked from shomali11/slacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
69 lines (58 loc) · 1.76 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package slacker
import (
"github.com/shomali11/commander"
"github.com/shomali11/proper"
)
// CommandDefinition structure contains definition of the bot command
type CommandDefinition struct {
Description string
Example string
AuthorizationFunc func(request Request) bool
Handler func(request Request, response ResponseWriter)
}
// NewBotCommand creates a new bot command object
func NewBotCommand(usage string, definition *CommandDefinition) BotCommand {
command := commander.NewCommand(usage)
return &botCommand{
usage: usage,
definition: definition,
command: command,
}
}
// botCommand structure contains the bot's command, description and handler
type botCommand struct {
usage string
definition *CommandDefinition
command *commander.Command
}
// BotCommand interface
type BotCommand interface {
Usage() string
Definition() *CommandDefinition
Match(text string) (*proper.Properties, bool)
Tokenize() []*commander.Token
Execute(request Request, response ResponseWriter)
}
// Usage returns the command usage
func (c *botCommand) Usage() string {
return c.usage
}
// Description returns the command description
func (c *botCommand) Definition() *CommandDefinition {
return c.definition
}
// Match determines whether the bot should respond based on the text received
func (c *botCommand) Match(text string) (*proper.Properties, bool) {
return c.command.Match(text)
}
// Tokenize returns the command format's tokens
func (c *botCommand) Tokenize() []*commander.Token {
return c.command.Tokenize()
}
// Execute executes the handler logic
func (c *botCommand) Execute(request Request, response ResponseWriter) {
if c.definition == nil || c.definition.Handler == nil {
return
}
c.definition.Handler(request, response)
}