-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
143 lines (122 loc) · 3.4 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"errors"
"math/rand"
"strings"
"github.com/slack-go/slack"
)
type command struct {
db storer
slack slacker
helper
}
func getRandomColor() string {
letters := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
color := "#"
for i := 0; i < 6; i++ {
color += letters[rand.Intn(len(letters))]
}
return color
}
//params manage bot reponse for slack params
func (c *command) params(msg slack.Msg) error {
botCommand := map[string]string{
"add": "add problem with possible solution\n examples\nshort : `@bot add sources.LazyJDBCSource http://github.com`\n long: `@bot add \"ProxySQL Error: Access denied for user\" \"recreate container\"`",
"del": "del single row\n example: `@bot del \"single row\"`",
"list": "list all problems and solutions",
"fix-all": "Delete all problems",
"help": "Help message",
}
commandParam := strings.Split(msg.Text, " ")
commandParamLong := strings.Split(msg.Text, "\"")
switch commandParam[1] {
case "help":
fields := make([]slack.AttachmentField, 0, len(botCommand))
for k, v := range botCommand {
fields = append(fields, slack.AttachmentField{
Title: k,
Value: v,
})
}
attachment := slack.Attachment{
Pretext: "Bot command list",
Color: getRandomColor(),
Fields: fields,
}
err := c.slack.postMsg(msg, attachment)
if err != nil {
return err
}
case "add":
var description string
titleIndex := 2 //default index for value
descriptionIndex := 3 //default index for description
//if command params are using " " use long format
if len(commandParamLong) > 2 {
commandParam = commandParamLong
titleIndex = 1 //long param default index for value
descriptionIndex = 3 //long param default index for description
}
if len(commandParam) < 4 {
c.slack.simpleMsg(msg, ":niedobrze: Not enough number of parameters. Try `help` command")
return errors.New("Not enough number of parameter")
}
title := commandParam[titleIndex]
description = commandParam[descriptionIndex]
if _, ok := c.message[title]; !ok {
err := c.db.addRow(title, description)
if err != nil {
return err
}
c.message[title] = description
c.slack.simpleMsg(msg, ":thumbsup: Thanks for support. This problem will not bother anymore!")
}
case "del":
if len(commandParamLong) < 2 {
c.slack.simpleMsg(msg, ":niedobrze: Not enough number of parameters. Try `help` command")
return errors.New("Not enough number of parameter")
}
title := commandParamLong[1]
if _, ok := c.message[title]; ok {
err := c.db.deleteRow(title)
if err != nil {
return err
}
delete(c.message, title)
c.slack.simpleMsg(msg, ":thumbsup: Problem fixed")
}
case "list":
fields := make([]slack.AttachmentField, 0)
err := c.db.getRow()
if err != nil {
return err
}
for k, v := range c.message {
fields = append(fields, slack.AttachmentField{
Title: k,
Value: v,
})
}
attachment := slack.Attachment{
Pretext: "Matching words",
Color: getRandomColor(),
Fields: fields,
}
err = c.slack.postMsg(msg, attachment)
if err != nil {
return err
}
case "fix-all":
err := c.db.deleteAll()
if err != nil {
return err
}
for k := range c.message {
delete(c.message, k)
}
c.slack.simpleMsg(msg, ":thumbsup: All problems fixed")
default:
c.slack.simpleMsg(msg, "Try `help` command")
}
return nil
}