-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (72 loc) · 1.84 KB
/
main.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
package main
// This is the entry point for the chat programm where all the
// packages are called to interact with each other.
import (
"strings"
"github.com/MaiLinhGroup/chatbot/auth"
"github.com/MaiLinhGroup/chatbot/chat"
// standard libs
// 3rd party libs
log "github.com/goinggo/tracelog"
)
func main() {
log.Start(log.LevelInfo)
defer log.Stop()
chatbot, err := chat.New()
if err != nil {
log.Error(err, "main", "chat.New()")
return
}
userRq := make(chan chat.Message)
userFb := make(chan chat.Message)
go ChatHandler(userRq, userFb)
chatbot.Chat(userRq, userFb)
}
// ChatHandler ...
func ChatHandler(userRequest, userFeedback chan chat.Message) error {
a, err := auth.CreateAdminUser()
if err != nil {
return err
}
for msg := range userRequest {
if a.Admin(msg.UserID) {
msg.Reply = "Hi Admin! Here my reply:\n"
msg.Reply += ProcessingUserRequest(msg.Request)
} else {
msg.Reply = "Hi " + msg.UserName + "! Sorry, currently I only talk to my admin."
}
userFeedback <- msg
}
return nil
}
// ReverseMessage takes a message and returns it in reverse order.
// One or more leading and trailing whitespaces got to be removed,
// but no further modification will be performed on the original message.
func ReverseMessage(msg string) (reverseMsg string) {
msg = strings.TrimSpace(msg)
for i := len(msg) - 1; i >= 0; i-- {
reverseMsg += string(msg[i])
}
return
}
// ProcessingUserRequest ...
func ProcessingUserRequest(request map[string]string) string {
var reply string
for cmd, arg := range request {
if arg == "" {
reply = "Hello World!"
break
}
switch cmd {
case "rev":
reply = ReverseMessage(arg)
case "":
// plain message no command, just echoing message
reply = arg
default:
// unknown command, ignoring argument
reply = "Sorry, unknown command: /" + cmd
}
}
return reply
}