-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount.go
More file actions
301 lines (253 loc) · 9.28 KB
/
account.go
File metadata and controls
301 lines (253 loc) · 9.28 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package greenapi
import (
"encoding/json"
)
type AccountCategory struct {
GreenAPI GreenAPIInterface
}
// ------------------------------------------------------------------ GetSettings
// Getting settings of an instance.
//
// https://green-api.com/v3/docs/api/account/GetSettings/
func (c AccountCategory) GetSettings() (*APIResponse, error) {
return c.GreenAPI.Request("GET", "getSettings", nil)
}
// ------------------------------------------------------------------ SetSettings
type RequestSetSettings struct {
WebhookUrl *string `json:"webhookUrl,omitempty"`
WebhookUrlToken *string `json:"webhookUrlToken,omitempty"`
DelaySendMessagesMilliseconds *uint `json:"delaySendMessagesMilliseconds,omitempty"`
MarkIncomingMessagesReaded string `json:"markIncomingMessagesReaded,omitempty"`
MarkIncomingMessagesReadedOnReply string `json:"markIncomingMessagesReadedOnReply,omitempty"`
OutgoingWebhook string `json:"outgoingWebhook,omitempty"`
OutgoingMessageWebhook string `json:"outgoingMessageWebhook,omitempty"`
OutgoingAPIMessageWebhook string `json:"outgoingAPIMessageWebhook,omitempty"`
StateWebhook string `json:"stateWebhook,omitempty"`
IncomingWebhook string `json:"incomingWebhook,omitempty"`
}
type SetSettingsOption func(*RequestSetSettings) error
// URL for sending notifications.
func OptionalWebhookUrl(webhookUrl string) SetSettingsOption {
return func(r *RequestSetSettings) error {
err := ValidateURL(webhookUrl)
if err != nil {
return err
}
r.WebhookUrl = &webhookUrl
return nil
}
}
// Token to access your notification server.
func OptionalWebhookUrlToken(webhookUrlToken string) SetSettingsOption {
return func(r *RequestSetSettings) error {
r.WebhookUrlToken = &webhookUrlToken
return nil
}
}
// Message sending delay.
func OptionalDelaySendMessages(delaySendMessagesMilliseconds uint) SetSettingsOption {
return func(r *RequestSetSettings) error {
r.DelaySendMessagesMilliseconds = &delaySendMessagesMilliseconds
return nil
}
}
// Mark incoming messages as read or not.
func OptionalMarkIncomingMessagesRead(markIncomingMessagesReaded bool) SetSettingsOption {
return func(r *RequestSetSettings) error {
if markIncomingMessagesReaded {
r.MarkIncomingMessagesReaded = "yes"
} else {
r.MarkIncomingMessagesReaded = "no"
}
return nil
}
}
// Mark incoming messages as read when posting a message to the chat via API.
func OptionalMarkIncomingMessagesReadOnReply(markIncomingMessagesReadedOnReply bool) SetSettingsOption {
return func(r *RequestSetSettings) error {
if markIncomingMessagesReadedOnReply {
r.MarkIncomingMessagesReadedOnReply = "yes"
} else {
r.MarkIncomingMessagesReadedOnReply = "no"
}
return nil
}
}
// Get notifications about outgoing messages sending/delivering/reading statuses
func OptionalOutgoingWebhook(outgoingWebhook bool) SetSettingsOption {
return func(r *RequestSetSettings) error {
if outgoingWebhook {
r.OutgoingWebhook = "yes"
} else {
r.OutgoingWebhook = "no"
}
return nil
}
}
// Get notifications about messages sent from the phone.
func OptionalOutgoingMessageWebhook(outgoingMessageWebhook bool) SetSettingsOption {
return func(r *RequestSetSettings) error {
if outgoingMessageWebhook {
r.OutgoingMessageWebhook = "yes"
} else {
r.OutgoingMessageWebhook = "no"
}
return nil
}
}
// Get notifications about messages sent from API.
func OptionalOutgoingAPIMessageWebhook(outgoingAPIMessageWebhook bool) SetSettingsOption {
return func(r *RequestSetSettings) error {
if outgoingAPIMessageWebhook {
r.OutgoingAPIMessageWebhook = "yes"
} else {
r.OutgoingAPIMessageWebhook = "no"
}
return nil
}
}
// Get notifications about the instance authorization state change.
func OptionalStateWebhook(stateWebhook bool) SetSettingsOption {
return func(r *RequestSetSettings) error {
if stateWebhook {
r.StateWebhook = "yes"
} else {
r.StateWebhook = "no"
}
return nil
}
}
// Get notifications about incoming messages and files.
func OptionalIncomingWebhook(incomingWebhook bool) SetSettingsOption {
return func(r *RequestSetSettings) error {
if incomingWebhook {
r.IncomingWebhook = "yes"
} else {
r.IncomingWebhook = "no"
}
return nil
}
}
// Applying settings for an instance.
//
// https://green-api.com/v3/docs/api/account/SetSettings/
//
// Add optional arguments by passing these functions:
//
// OptionalWebhookUrl(webhookUrl string) <- URL for sending notifications.
// OptionalWebhookUrlToken(webhookUrlToken string) <- Token to access your notification server.
// OptionalDelaySendMesssages(delaySendMessagesMilliseconds int) <- Message sending delay.
// OptionalMarkIncomingMessagesRead(markIncomingMessagesReaded bool) <- Mark incoming messages as read or not.
// OptionalMarkIncomingMessagesReadOnReply(markIncomingMessagesReadedOnReply bool) <- Mark incoming messages as read when posting a message to the chat via API.
// OptionalOutgoingWessebhook(outgoingWebhook bool) <- Get notifications about outgoing messages sending/delivering/reading statuses.
// OptionalOutgoingMageWebhook(outgoingMessageWebhook bool) <- Get notifications about messages sent from the phone.
// OptionalOutgoingAPIMessageWebhook(outgoingAPIMessageWebhook bool) <- Get notifications about messages sent from API.
// OptionalStateWebhook(stateWebhook bool) <- Get notifications about the instance authorization state change.
// OptionalIncomingWebhook(incomingWebhook bool) <- Get notifications about incoming messages and files.
func (c AccountCategory) SetSettings(options ...SetSettingsOption) (*APIResponse, error) {
r := &RequestSetSettings{}
for _, o := range options {
err := o(r)
if err != nil {
return nil, err
}
}
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return c.GreenAPI.Request("POST", "setSettings", jsonData)
}
// ------------------------------------------------------------------ GetStateInstance
// Getting state of an instance.
//
// https://green-api.com/v3/docs/api/account/GetStateInstance/
func (c AccountCategory) GetStateInstance() (*APIResponse, error) {
return c.GreenAPI.Request("GET", "getStateInstance", nil)
}
// ------------------------------------------------------------------ GetStatusInstance
// Getting the status of an instance socket connection with MAX.
//
// https://green-api.com/v3/docs/api/account/GetStatusInstance/
func (c AccountCategory) GetStatusInstance() (*APIResponse, error) {
return c.GreenAPI.Request("GET", "getStatusInstance", nil)
}
// ------------------------------------------------------------------ Reboot
// Rebooting an instance.
//
// https://green-api.com/v3/docs/api/account/Reboot/
func (c AccountCategory) Reboot() (*APIResponse, error) {
return c.GreenAPI.Request("GET", "reboot", nil)
}
// ------------------------------------------------------------------ Logout
// Logging out an instance.
//
// https://green-api.com/v3/docs/api/account/Logout/
func (c AccountCategory) Logout() (*APIResponse, error) {
return c.GreenAPI.Request("GET", "logout", nil)
}
// ------------------------------------------------------------------ QR
// Get a QR code
//
// https://green-api.com/v3/docs/api/account/qr/
func (c AccountCategory) Qr() (*APIResponse, error) {
return c.GreenAPI.Request("GET", "qr", nil)
}
// ------------------------------------------------------------------ StartAuthorization
type RequestStartAuthorization struct {
PhoneNumber int `json:"phoneNumber"`
}
// The method is deprecated. Please use qr.
//
// https://green-api.com/v3/en/docs/api/account/StartAuthorization/
func (c AccountCategory) StartAuthorization(phoneNumber int) (*APIResponse, error) {
r := &RequestStartAuthorization{
PhoneNumber: phoneNumber,
}
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return c.GreenAPI.Request("POST", "startAuthorization", jsonData)
}
// ------------------------------------------------------------------ SendAuthorizationCode
type RequestSendAuthorizationCode struct {
Code string `json:"code"`
}
// The method is deprecated. Please use qr.
//
// https://green-api.com/v3/en/docs/api/account/StartAuthorization/
func (c AccountCategory) SendAuthorizationCode(code string) (*APIResponse, error) {
r := &RequestSendAuthorizationCode{
Code: code,
}
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return c.GreenAPI.Request("POST", "sendAuthorizationCode", jsonData)
}
// ------------------------------------------------------------------ SetProfilePicture
type RequestSetProfilePicture struct {
File string `json:"file"`
}
// Setting a profile picture.
//
// https://green-api.com/v3/docs/api/account/SetProfilePicture/
func (c AccountCategory) SetProfilePicture(filepath string) (*APIResponse, error) {
r := &RequestSetProfilePicture{
File: filepath,
}
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return c.GreenAPI.Request("POST", "setProfilePicture", jsonData, WithFormData(true))
}
// ------------------------------------------------------------------ GetAccountSettings
// Getting information about the MAX account
//
// https://green-api.com/v3/docs/api/account/GetAccountSettings/
func (c AccountCategory) GetAccountSettings() (*APIResponse, error) {
return c.GreenAPI.Request("GET", "getAccountSettings", nil)
}