-
Notifications
You must be signed in to change notification settings - Fork 0
/
msghandler.go
54 lines (51 loc) · 1.7 KB
/
msghandler.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
package main
import (
"fmt"
"git.mrcyjanek.net/p3pch4t/p3pgo/lib/core"
"git.mrcyjanek.net/p3pch4t/p3pgroup/structs"
"github.com/google/shlex"
)
func botMsgHandler(pi *core.PrivateInfoS, ui *core.UserInfo, evt *core.Event, msg *core.Message) {
// - `!create "Group Name" "Group Email [optional]"` - Create a group
// - `!list` - Show list of all groups
// - `!details group_id` - Show details about a group.
// - `!delete group_id` - Delete a group
// - `!help` - Show this very message
cmd, err := shlex.Split(msg.Body)
if err != nil {
pi.SendMessage(ui, core.MessageTypeText, fmt.Sprintf("Unable to process message. Is your syntax valid?.\n\n%s", err.Error()))
return
}
if len(cmd) == 0 {
pi.SendMessage(ui, core.MessageTypeText, "It looks like your message is empty. Please provide a command. Check `!help` for instructions.")
return
}
switch cmd[0] {
case "!help":
pi.SendMessage(ui, core.MessageTypeText, welcomeMsg)
case "!create":
if len(cmd) != 2 && len(cmd) != 3 {
pi.SendMessage(ui, core.MessageTypeText, "Usage: `!create 'Group Name' 'Group Email [optional]'`")
return
}
email := ""
if len(cmd) == 3 {
email = cmd[2]
}
groupId := createGroup(ui.GetKeyID(), cmd[1], email)
pi.SendMessage(ui, core.MessageTypeText, "Group Created. ID: `"+groupId+"`")
case "!list":
var text = "Groups:\n"
for _, info := range structs.Groups {
text += fmt.Sprintf("- %s (`%s`)\n", info.GroupName, info.PI.Endpoint)
}
pi.SendMessage(ui, core.MessageTypeText, text)
case "!details":
if len(cmd) != 2 {
pi.SendMessage(ui, core.MessageTypeText, "Usage: `!details 'groupId'`")
return
}
default:
pi.SendMessage(ui, core.MessageTypeText, "Command not found. Check `!help`")
}
}