forked from shomali11/slacker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
128 lines (109 loc) · 3.77 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package slacker
import (
"reflect"
"strings"
allot "github.com/sdslabs/allot/pkg"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
)
// CommandDefinition structure contains definition of the bot command
type CommandDefinition struct {
Description string
Examples []string
BlockID string
AuthorizationFunc func(botCtx BotContext, request Request) bool
Handler func(botCtx BotContext, request Request, response ResponseWriter)
Interactive func(*Slacker, *socketmode.Event, *slack.InteractionCallback, *socketmode.Request)
// HideHelp will cause this command to not be shown when a user requests
// help.
HideHelp bool
}
// NewBotCommand creates a new bot command object
func NewBotCommand(usage string, definition *CommandDefinition, isParameterizedCommand bool, includeChannelIds []string) BotCommand {
command := allot.New(usage)
return &botCommand{
usage: usage,
definition: definition,
command: command,
isParameterizedCommand: isParameterizedCommand,
includeChannelIds: includeChannelIds,
}
}
// BotCommand interface
type BotCommand interface {
Usage() string
Definition() *CommandDefinition
IsParameterizedCommand() bool
ContainsChannel(channelId string) bool
MsgContains(text string) bool
Match(req string) (allot.MatchInterface, error)
Matches(text string) bool
Tokenize() []*allot.Token
Parameters() []allot.Parameter
Execute(botCtx BotContext, request Request, response ResponseWriter)
Interactive(*Slacker, *socketmode.Event, *slack.InteractionCallback, *socketmode.Request)
}
// botCommand structure contains the bot's command, description and handler
type botCommand struct {
usage string
definition *CommandDefinition
command *allot.Command
isParameterizedCommand bool
includeChannelIds []string
}
// 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
}
// IsParameterizedCommand returns whether command is parameterized command or we only want substring match
func (c *botCommand) IsParameterizedCommand() bool {
return c.isParameterizedCommand
}
func (c *botCommand) ContainsChannel(channelId string) bool {
if reflect.DeepEqual(c.includeChannelIds, defaultIncludeChannelIds) {
return true
}
for _, chId := range c.includeChannelIds {
if chId == channelId {
return true
}
}
return false
}
func (c *botCommand) MsgContains(text string) bool {
return strings.Contains(strings.ToLower(text), c.usage)
}
// Match determines whether the bot should respond based on the text received
func (c *botCommand) Match(text string) (allot.MatchInterface, error) {
return c.command.Match(text)
}
// Matches checks if a comand definition matches a request
func (c *botCommand) Matches(text string) bool {
return c.command.Matches(text)
}
// Tokenize returns the command format's tokens
func (c *botCommand) Tokenize() []*allot.Token {
return c.command.Tokenize()
}
// Parameters returns the command format's tokens
func (c *botCommand) Parameters() []allot.Parameter {
return c.command.Parameters()
}
// Execute executes the handler logic
func (c *botCommand) Execute(botCtx BotContext, request Request, response ResponseWriter) {
if c.definition == nil || c.definition.Handler == nil {
return
}
c.definition.Handler(botCtx, request, response)
}
// Interactive executes the interactive logic
func (c *botCommand) Interactive(slacker *Slacker, evt *socketmode.Event, callback *slack.InteractionCallback, req *socketmode.Request) {
if c.definition == nil || c.definition.Interactive == nil {
return
}
c.definition.Interactive(slacker, evt, callback, req)
}