This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandParser.go
77 lines (67 loc) · 2.88 KB
/
commandParser.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
package main
import (
"fmt"
"strings"
"github.com/sbstjn/allot"
)
var commandDescription map[string]func(allot.MatchInterface, User) (*genericMessage, error)
var commandList map[allot.Command]func(allot.MatchInterface, User) (*genericMessage, error)
var fortunes []string
func init() {
commandDescription = map[string]func(allot.MatchInterface, User) (*genericMessage, error){
"echo (.*)": handleEcho,
"welcome <user:string>": handleWelcome,
"annoy me about <alertgroup:string> alerts": handleAddToAlertGroup,
"don't bug me about <alertgroup:string> alerts": handleDelFromAlertGroup,
}
commandList = make(map[allot.Command]func(allot.MatchInterface, User) (*genericMessage, error))
for comm, handler := range commandDescription {
newCommand := allot.New(comm)
commandList[newCommand] = handler
}
}
func handleRequest(incomingMessage *genericMessage) (*genericMessage, error) {
log.Debugf("Incoming Message: %v#", incomingMessage)
request := strings.TrimSpace(incomingMessage.ContentText)
for cmd, handler := range commandList {
match, err := cmd.Match(request)
if err == nil {
message, err := handler(match, incomingMessage.Sender)
message.Thread = incomingMessage.Thread
message.MessagePath = incomingMessage.MessagePath
if err != nil {
return message, err
}
return message, nil
}
}
messageText := "Your message did not match any command.\nPossible case-sensitive commands are:\n"
for key := range commandDescription {
messageText += fmt.Sprintf(" %s\n", key)
}
return &genericMessage{ContentText: messageText, Thread: incomingMessage.Thread, MessagePath: incomingMessage.MessagePath}, nil
}
func handleEcho(match allot.MatchInterface, User User) (*genericMessage, error) {
echo, err := match.Match(0)
if err != nil {
return &genericMessage{ContentText: "I had issues when parsing your words"}, err
}
return &genericMessage{ContentText: fmt.Sprintf("What you said: \"%s\"", echo)}, nil
}
func handleWelcome(match allot.MatchInterface, User User) (*genericMessage, error) {
echo, err := match.String("user")
if err != nil {
return &genericMessage{ContentText: "I had issues identifying who to welcome"}, err
}
return &genericMessage{ContentText: fmt.Sprintf("Welcome %s - nice to meet you here :)", echo)}, nil
}
func handleAddToAlertGroup(match allot.MatchInterface, User User) (*genericMessage, error) {
alertGroup, err := match.String("alertgroup")
err = User.addToAlertGroup(alertGroup)
return &genericMessage{ContentText: fmt.Sprintf("User %s added to alert group %s", User.getUserinfo().FriendlyName, alertGroup)}, err
}
func handleDelFromAlertGroup(match allot.MatchInterface, User User) (*genericMessage, error) {
alertGroup, err := match.String("alertgroup")
err = User.delFromAlertGroup(alertGroup)
return &genericMessage{ContentText: fmt.Sprintf("User %s removed from alert group %s", User.getUserinfo().FriendlyName, alertGroup)}, err
}