-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
59 lines (49 loc) · 1.61 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
package announcebot
import (
"errors"
"fmt"
"strings"
log "github.com/Sirupsen/logrus"
)
// ChatCommand receives a chat message and returns a reply
type ChatCommand func(user string, message string, logger *log.Entry) (reply string)
var errUserAlreadySubscribed = errors.New("User is already subscribed")
var errUserNotSubscribed = errors.New("User is not subscribed")
func (bot *AnnounceBot) subscribeCommand(user string, message string, logger *log.Entry) (reply string) {
result, err := bot.subscribeUser(user)
switch result {
case 201:
reply = "Alright, you're signed up!"
case 400:
reply = "Looks like you are already subscribed!"
case 500:
reply = "Uh oh... something didn't go right. Try again later. :-("
if err != nil {
logger.WithError(err).Error("Error while attempting to subscribe the user")
}
}
return
}
func (bot *AnnounceBot) unsubscribeCommand(user string, message string, logger *log.Entry) (reply string) {
result, err := bot.unsubscribeUser(user)
switch result {
case 204:
reply = "Alright, you've been unsubscribed! I'l miss you..."
case 400:
reply = "Looks like you aren't subscribed!"
case 500:
reply = "Uh oh... something didn't go right. Try again later. :-("
if err != nil {
logger.WithError(err).Error("Error while attempting to unsubscribe the user")
}
}
return
}
func (bot *AnnounceBot) helpCommand(user string, message string, logger *log.Entry) (reply string) {
var validCommands []string
for key := range bot.commands {
validCommands = append(validCommands, key)
}
reply = fmt.Sprintf("Valid commands are: %s", strings.Join(validCommands, ", "))
return
}