This repository has been archived by the owner on Jul 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (58 loc) · 1.68 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
package main
import (
"encoding/json"
"github.com/tidwall/gjson"
"log"
"math/rand"
"strings"
"time"
)
const ConfigFilePath string = "config.json"
const ServersFilePath string = "server.tsv"
const VkApiVersion = "5.85"
var Config *ConfigData
var VkCommands map[string]func(vk *Vk, object *LongPollMessage)
func onLongPollMessage(vk *Vk, object gjson.Result) {
msg := new(LongPollMessage)
_ = json.Unmarshal([]byte(object.Raw), msg)
Command := gjson.Get(msg.Payload, "command")
if Command.Exists() {
if event, ok := VkCommands[Command.String()]; ok {
event(vk, msg)
}
return
}
for _, i := range Config.StartCommands {
if strings.EqualFold(msg.Text, i) {
CommandStart(vk, msg)
return
}
}
vk.SendMessage(msg.FromId, Config.WelcomeMessage)
}
func CommandStart(vk *Vk, object *LongPollMessage) {
kb := VkKeyboard{OneTime: false}
kb.AddRow(kb.TxtBtn("🔍 Find Rust servers", "secondary", `{"command":"rustFind"}`))
kb.AddRow(kb.TxtBtn("📣 Example command", "secondary", `{"command":"example"}`))
vk.SendKeyboard(object.FromId, "Select a menu item:", &kb)
}
func CommandExample(vk *Vk, object *LongPollMessage) {
vk.SendMessage(object.FromId, "Example command response")
}
func main() {
rand.Seed(time.Now().UnixNano())
var err error
if Config, err = ConfigRead(ConfigFilePath); err != nil {
log.Fatalf("Config read error: %s\n", err.Error())
}
LoadRustServers()
VkCommands = map[string]func(vk *Vk, object *LongPollMessage){
"start": CommandStart,
"rustFind": CommandRustFind,
"example": CommandExample,
}
vk := Vk{accessToken: Config.Token, version: VkApiVersion}
vk.GroupPoll(Config.GroupId, onLongPollMessage)
ch := make(chan int)
<-ch
}