-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
127 lines (114 loc) · 2.75 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Star Nepgear BOT
===
High-Speed Group Protective BOT for LINE.
Copyright(c) 2020 Star Inc. All Rights Reserved.
The software is licensed under Apache License 2.0.
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
api "github.com/star-inc/NepCoreO"
core "github.com/star-inc/olsb_cores/libs/NepCoreO"
"gopkg.in/yaml.v2"
)
type configInterface struct {
LINE *struct {
Server *struct {
ClientPath string `yaml:"Command_Path"`
ListenPath string `yaml:"LongPoll_path"`
} `yaml:"Server"`
Account *struct {
AuthToken string `yaml:"X-Line-Access"`
} `yaml:"Account"`
} `yaml:"LINE"`
}
const (
commandPrefix = "$"
configPath = "config.yaml"
routineTimeout = 100
fetchOpeartionsLength = 50
)
var (
config *configInterface
client *api.ClientInterface
listen *api.ClientInterface
)
func main() {
declare()
readConfig()
connect()
ctx, _ := api.SetRoutine(routineTimeout)
revision, _ := client.TalkServiceClient.GetLastOpRevision(ctx)
for {
ctx, _ = api.SetRoutine(routineTimeout)
ops, _ := listen.TalkServiceClient.FetchOperations(ctx, revision, fetchOpeartionsLength)
go func(ops []*core.Operation) {
for _, task := range ops {
switch task.Type {
case core.OpType_RECEIVE_MESSAGE:
go messageHandle(task)
break
}
}
}(ops)
if len(ops) > 1 {
revision = func(ops []*core.Operation) int64 {
if ops[len(ops)-1].Revision > ops[len(ops)-2].Revision {
return ops[len(ops)-1].Revision
}
return ops[len(ops)-2].Revision
}(ops)
}
}
}
func declare() {
fmt.Println("")
fmt.Println("Star Nepgear BOT")
fmt.Println("===")
fmt.Println("High-Speed Group Protective BOT for LINE.")
fmt.Println("\nCopyright(c) 2020 Star Inc. All Rights Reserved.")
fmt.Println("The software is licensed under Apache License 2.0.")
fmt.Println("")
fmt.Println("")
}
func readConfig() {
yamlFile, _ := os.Open(configPath)
defer yamlFile.Close()
srcYAML, _ := ioutil.ReadAll(yamlFile)
_ = yaml.Unmarshal(srcYAML, &config)
}
func sendToWho(op *core.Operation) string {
switch op.Message.ToType {
case core.MIDType_USER:
return op.Message.From_
case core.MIDType_ROOM:
case core.MIDType_GROUP:
return op.Message.To
}
return ""
}
func connect() {
client = api.NewClientInterface(config.LINE.Server.ClientPath)
listen = api.NewClientInterface(config.LINE.Server.ListenPath)
client.Authorize(config.LINE.Account.AuthToken)
listen.Authorize(config.LINE.Account.AuthToken)
}
func messageHandle(op *core.Operation) {
switch op.Message.ContentType {
case core.ContentType_NONE:
text(op)
break
}
}
func text(op *core.Operation) {
msg := strings.Split(op.Message.Text, " ")
switch msg[0] {
case commandPrefix + "help":
client.SendText(sendToWho(op), "Star Nepgear BOT")
break
}
}