This repository was archived by the owner on Feb 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
133 lines (107 loc) · 2.49 KB
/
main.go
File metadata and controls
133 lines (107 loc) · 2.49 KB
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
128
129
130
131
132
133
package main
import (
"flag"
"fmt"
"github.com/BurntSushi/toml"
"github.com/SevereCloud/vksdk/v2/api"
"github.com/SevereCloud/vksdk/v2/object"
"github.com/go-co-op/gocron"
log "github.com/inconshreveable/log15"
"strings"
"time"
)
type config struct {
ServiceToken string
MessageToken string
Admins []uint
Groups []int
Period string
Message string
}
var (
conf config
countList []int
serviceVK *api.VK
botVK *api.VK
)
var logger = log.New("service", "vkonity")
func main() {
configFile := flag.String("config", "config.toml", "path to the config file")
flag.Parse()
var err error
if _, err = toml.DecodeFile(*configFile, &conf); err != nil {
panic(err)
}
serviceVK = api.NewVK(conf.ServiceToken)
botVK = api.NewVK(conf.MessageToken)
s := gocron.NewScheduler(time.UTC)
_, err = s.Every(conf.Period).Do(check)
if err != nil {
panic(err)
}
s.StartAsync()
s.StartBlocking()
}
func check() {
for i, groupId := range conf.Groups {
group := get(groupId)
count := group.Count
// If not current group in list
if len(countList) < i+1 {
countList = append(countList, count)
continue
}
previewCount := countList[i]
countList[i] = count
if count <= previewCount {
continue
}
post := getPost(group.Items)
send(generateMessageText(post), getAttachment(post))
}
}
func getAttachment(post object.WallWallpost) string {
attachments := post.Attachments
if len(attachments) < 1 {
return ""
}
var attachmentsParsed [10]string
for i, attachment := range attachments {
attachmentsParsed[i] = attachment.Photo.ToAttachment()
}
return strings.Join(attachmentsParsed[:], ",")
}
func generateMessageText(post object.WallWallpost) string {
groupId := post.OwnerID
postId := post.ID
text := post.Text
return fmt.Sprintf(conf.Message, -groupId, groupId, postId, text)
}
func getPost(items []object.WallWallpost) (post object.WallWallpost) {
post = items[0]
if post.IsPinned {
post = items[1]
}
return post
}
func send(text string, attachment string) {
_, err := botVK.MessagesSendPeerIDs(api.Params{
"user_ids": conf.Admins,
"message": text,
"random_id": 0,
"attachment": attachment,
})
if err != nil {
logger.Error("Failed to send message", "admins", conf.Admins, "text", text, "attachment", attachment)
}
}
func get(groupId int) api.WallGetResponse {
group, err := serviceVK.WallGet(api.Params{
"count": 2,
"owner_id": -groupId,
})
if err != nil {
logger.Error("Failed to get posts", "owner_id", -groupId)
}
return group
}