-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
96 lines (80 loc) · 2.37 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
90
91
92
93
94
95
96
package main
import (
"bufio"
"fmt"
"github.com/gookit/color"
"github.com/olivia-ai/olivia-console/files"
"github.com/olivia-ai/olivia-kit-go/chat"
"os"
"strings"
)
const (
logFileName = "logfile.log"
configFileName = "config.json"
)
// RequestMessage is the structure that uses entry connections to chat with the websocket
type RequestMessage struct {
Type int `json:"type"` // 0 for handshakes and 1 for messages
Content string `json:"content"`
Token string `json:"user_token"`
Information map[string]interface{} `json:"information"`
Locale string `json:"locale"`
}
// ResponseMessage is the structure used to reply to the user through the websocket
type ResponseMessage struct {
Content string `json:"content"`
Tag string `json:"tag"`
Information map[string]interface{} `json:"information"`
}
func main() {
// Setup the logs and the config file
files.SetupLog(logFileName)
config := files.SetupConfig(configFileName)
files.SetupLogLevel(*config)
var information map[string]interface{}
client, err := chat.NewClient(
fmt.Sprintf("%s:%s", config.Host, config.Port),
config.SSL,
&information,
)
if err != nil {
panic(err)
}
defer client.Close()
fmt.Println(color.Magenta.Render("Enter message to " + config.BotName + " or type:"))
fmt.Printf("- %s to quit\n", color.Green.Render("/quit"))
fmt.Printf("- %s to change the language\n", color.Green.Render("/lang <en|fr|es...>"))
fmt.Println()
scanner := bufio.NewScanner(os.Stdin)
Loop:
for {
fmt.Print(">")
scanner.Scan()
text := scanner.Text()
switch {
case strings.TrimSpace(text) == "":
fmt.Println(
color.Red.Render("Please enter a message"),
)
continue Loop
case text == "/quit":
os.Exit(0)
case strings.HasPrefix(text, "/lang"):
arguments := strings.Split(text, " ")[1:]
if len(arguments) != 1 {
fmt.Println(
color.Red.Render("Wrong number of arguments, language command should contain only the locale"),
)
continue Loop
}
client.Locale = arguments[0]
fmt.Printf("Language changed to %s.\n", color.Magenta.Render(arguments[0]))
continue Loop
}
response, err := client.SendMessage(text)
if err != nil {
continue
}
fmt.Printf("%s> %s\n", color.Magenta.Render(config.BotName), response.Content)
}
}