-
Notifications
You must be signed in to change notification settings - Fork 10
/
help.go
170 lines (137 loc) · 5.35 KB
/
help.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package slackscot
import (
"fmt"
"github.com/alexandre-normand/slackscot/config"
"io"
"strings"
)
type helpPlugin struct {
Plugin
name string
slackscotVersion string
timeLocation string
commands map[string][]ActionDefinition
hearActions []ActionDefinition
pluginScheduledActions []pluginScheduledAction
cmdPrefix string
}
const (
helpPluginName = "help"
)
// pluginScheduledAction represents a plugin's scheduled action with the plugin name and the action's definition
type pluginScheduledAction struct {
plugin string
ScheduledActionDefinition
}
func (s *Slackscot) newHelpPlugin(version string) *helpPlugin {
commands, hearActions, scheduledActions := findAllActions(s.namespaceCommands, s.plugins)
helpPlugin := new(helpPlugin)
helpPlugin.timeLocation = s.config.GetString(config.TimeLocationKey)
helpPlugin.name = s.name
helpPlugin.slackscotVersion = version
helpPlugin.commands = commands
helpPlugin.hearActions = hearActions
helpPlugin.pluginScheduledActions = scheduledActions
helpPlugin.cmdPrefix = s.cmdMatcher.UsagePrefix()
helpPlugin.Plugin = Plugin{Name: helpPluginName, Commands: []ActionDefinition{{
Match: func(m *IncomingMessage) bool {
return strings.HasPrefix(m.NormalizedText, "help")
},
Usage: helpPluginName,
Description: "Reply with usage instructions",
Answer: helpPlugin.showHelp,
}}, HearActions: nil}
return helpPlugin
}
// showHelp generates a message providing a list of all of the slackscot commands and hear actions.
// Note that ActionDefinitions with the flag Hidden set to true won't be included in the list
func (h *helpPlugin) showHelp(m *IncomingMessage) *Answer {
var b strings.Builder
// Get the user's first name using the botservices
userID := m.User
user, err := h.UserInfoFinder.GetUserInfo(userID)
if err != nil {
h.Logger.Debugf("Error getting user info for user id [%s] so skipping mentioning the name (it would be awkward): %v", userID, err)
} else {
fmt.Fprintf(&b, "🤝 Hi, `%s`! ", user.RealName)
}
fmt.Fprintf(&b, "I'm `%s` (engine `v%s`) and I listen to the team's chat and provides automated functions :genie:.\n", h.name, h.slackscotVersion)
if lenCommands(h.commands) > 0 {
fmt.Fprintf(&b, "\nI currently support the following commands:\n")
for n, commands := range h.commands {
appendActions(&b, h.cmdPrefix, n, commands)
}
}
if len(h.hearActions) > 0 {
fmt.Fprintf(&b, "\nAnd listen for the following:\n")
appendActions(&b, "", "", h.hearActions)
}
if len(h.pluginScheduledActions) > 0 {
fmt.Fprintf(&b, "\nAnd do those things periodically:\n")
appendScheduledActions(&b, h.timeLocation, h.pluginScheduledActions)
}
return &Answer{Text: b.String(), Options: []AnswerOption{AnswerInThread()}}
}
// lenCommands returns the length of a map of string to array of values by summing
// up the length of all array values
func lenCommands(entries map[string][]ActionDefinition) (length int) {
length = 0
for _, v := range entries {
length = length + len(v)
}
return length
}
func appendActions(w io.Writer, prefix string, pluginNamespace string, actions []ActionDefinition) {
for _, value := range actions {
if value.Usage != "" && !value.Hidden {
if len(pluginNamespace) > 0 {
fmt.Fprintf(w, "\t• `%s%s %s` - %s\n", prefix, pluginNamespace, value.Usage, value.Description)
} else {
fmt.Fprintf(w, "\t• `%s%s` - %s\n", prefix, value.Usage, value.Description)
}
}
}
}
func appendScheduledActions(w io.Writer, timeLocationName string, scheduledActions []pluginScheduledAction) {
for _, value := range scheduledActions {
if !value.ScheduledActionDefinition.Hidden {
fmt.Fprintf(w, "\t• [`%s`] `%s` (`%s`) - %s\n", value.plugin, value.ScheduledActionDefinition.Schedule, timeLocationName, value.ScheduledActionDefinition.Description)
}
}
}
func findAllActions(namespaceCommands bool, plugins []*Plugin) (commands map[string][]ActionDefinition, hearActions []ActionDefinition, pluginScheduledActions []pluginScheduledAction) {
commands = make(map[string][]ActionDefinition)
hearActions = make([]ActionDefinition, 0)
pluginScheduledActions = make([]pluginScheduledAction, 0)
for _, p := range plugins {
namespace := ""
if namespaceCommands && p.NamespaceCommands {
namespace = p.Name
}
if _, ok := commands[namespace]; !ok {
commands[namespace] = make([]ActionDefinition, 0)
}
commands[namespace] = append(commands[namespace], filterNonHiddenActions(p.Commands)...)
hearActions = append(hearActions, filterNonHiddenActions(p.HearActions)...)
pluginScheduledActions = append(pluginScheduledActions, filterNonHiddenScheduledActions(p.Name, p.ScheduledActions)...)
}
return commands, hearActions, pluginScheduledActions
}
func filterNonHiddenActions(actions []ActionDefinition) (visibleActions []ActionDefinition) {
visibleActions = make([]ActionDefinition, 0)
for _, a := range actions {
if !a.Hidden {
visibleActions = append(visibleActions, a)
}
}
return visibleActions
}
func filterNonHiddenScheduledActions(pluginName string, actions []ScheduledActionDefinition) (visibleActions []pluginScheduledAction) {
visibleActions = make([]pluginScheduledAction, 0)
for _, sa := range actions {
if !sa.Hidden {
visibleActions = append(visibleActions, pluginScheduledAction{plugin: pluginName, ScheduledActionDefinition: sa})
}
}
return visibleActions
}