-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
177 lines (151 loc) ยท 4.26 KB
/
api.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package api
import (
"fmt"
"net/http"
"sync"
"time"
"github.com/botscommunity/vkgo/internal/pkg/consts"
"github.com/botscommunity/botsgo"
"go.uber.org/zap"
)
// Bot โจ is an object that defines the properties of a bot
// ๐๏ธ The ID is the unique identifier of the bot. It contains either a community ID or a user page ID
// ๐ก A token is a key to access your application (bot)
// โ Version - VK API version, it is strongly recommended to set 5.154000
// ๐บ IsGroup is set automatically, guided by the information about the created bot
// ๐งข Logger is a pointer to the Logger object of the zap library. Used during development to detect problems during operation
// ๐ ContentType a simpler format of response data โ https://en.wikipedia.org/wiki/MessagePack
// ๐ Limit - limiting the number of requests per second depending on the type of bot
// ๐ฟ errorHandler performs the task of transmitting an error for further processing.
type Bot struct {
ID int `json:"id"`
Token string `json:"token"`
Version float32 `json:"version"`
IsGroup bool `json:"is_group"`
Deprecated Deprecated
ContentType string
Limit int
mutex sync.Mutex
time time.Time
rps int
*botsgo.Bot
}
func NewBot(token string, communityID int, properties ...any) (*Bot, error) {
return New(&Bot{
ID: communityID,
Token: token,
Limit: consts.CommunityAPILimit,
}, properties)
}
func NewUser(token string, userID int, properties ...any) (*Bot, error) {
return New(&Bot{
ID: userID,
Token: token,
Limit: consts.CommunityAPILimit,
}, properties)
}
// New ๐บ creating a page/community bot
// ๐๏ธ String - token
// ๐ฎ Float32 - version
// ๐ฌ Int - page/community ID
// ๐ Bot{} - advanced settings
// ๐ญ Returns an instance of the *Bot.
func New(properties ...any) (*Bot, error) {
client, err := botsgo.NewBot("https://api.vk.com/method")
if err != nil {
return nil, err
}
client.Logger = zap.NewNop()
bot := &Bot{
Version: consts.DefaultAPIVersion,
ContentType: "application/msgpack",
Limit: consts.UserAPILimit,
Bot: client,
}
for _, property := range properties {
switch property := property.(type) {
case *Bot:
botProperties(bot, property)
case int:
bot.ID = property
case string:
bot.Token = property
case float32:
bot.Version = property
case *zap.Logger:
bot.Bot.Logger = property
}
}
if bot.ID == 0 {
bot.defineID()
}
bot.Deprecated.Bot = bot
return bot, nil
}
func botProperties(bot, property *Bot) {
if property.ID != 0 {
bot.ID = property.ID
}
if property.Token != "" {
bot.Token = property.Token
}
if property.Version != 0 {
bot.Version = property.Version
}
if property.ContentType != "" {
bot.ContentType = property.ContentType
}
if property.Limit != 0 {
bot.Limit = property.Limit
}
if property.Bot != nil {
if property.Bot.HTTPClient != nil {
bot.Bot.HTTPClient = property.Bot.HTTPClient
}
if property.Bot.API != "" {
bot.Bot.API = property.Bot.API
}
if property.Bot.Logger != nil {
bot.Bot.Logger = property.Bot.Logger
}
}
}
// Call ๐ฅ๐
sending an API request
// ๐โ The first argument is the name of the method,
// ๐บ๐ผ๐น the second is the URL string,
// ๐ฆ๐น๐ฒ then usually a pointer to the structure where the response will be written
// ๐๐๐ May return an error.
func (bot *Bot) Call(method string, queryParams string, responseStruct any) error {
// code from VK SDK, thanks <3
if bot.Limit > 0 {
bot.mutex.Lock()
sleepTime := time.Second - time.Since(bot.time)
if sleepTime < 0 {
bot.time = time.Now()
bot.rps = 0
} else if bot.rps == bot.Limit {
time.Sleep(sleepTime)
bot.time = time.Now()
bot.rps = 0
}
bot.rps++
bot.mutex.Unlock()
}
if bot.ContentType == "application/msgpack" {
method += ".msgpack"
}
return bot.Bot.Call(botsgo.CallOptions{
Method: http.MethodPost,
Path: fmt.Sprintf("%s?access_token=%s&v=%f&%s", method, bot.Token, bot.Version, queryParams),
Response: responseStruct,
})
}
func (bot *Bot) defineID() {
if group := bot.GetGroup(); group.Error == nil {
bot.ID = group.ID
bot.IsGroup = true
bot.Limit = consts.CommunityAPILimit
} else if user := bot.GetUser(); user.Error == nil {
bot.ID = user.ID
}
}