-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiving.go
100 lines (78 loc) · 2.67 KB
/
receiving.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
package greenapi
import (
"encoding/json"
"fmt"
)
type ReceivingCategory struct {
GreenAPI GreenAPIInterface
}
// ------------------------------------------------------------------ ReceiveNotification
type RequestReceiveNotification struct {
ReceiveTimeout int `json:"receiveTimeout,omitempty"`
}
type ReceiveNotificationOption func(*RequestReceiveNotification) error
// Notification waiting timeout, takes a value from 5 to 60 seconds (5 seconds by default)
func OptionalReceiveTimeout(seconds int) ReceiveNotificationOption {
return func(r *RequestReceiveNotification) error {
r.ReceiveTimeout = seconds
return nil
}
}
// Receiving one incoming notification from the notifications queue.
//
// https://green-api.com/en/docs/api/receiving/technology-http-api/ReceiveNotification/
//
// Add optional arguments by passing these functions:
// OptionalReceiveTimeout(seconds int) <- Notification waiting timeout, takes a value from 5 to 60 seconds (5 seconds by default)
func (c ReceivingCategory) ReceiveNotification(options ...ReceiveNotificationOption) (*APIResponse, error) {
r := &RequestReceiveNotification{}
for _, o := range options {
err := o(r)
if err!=nil {
return nil, err
}
}
var addUrl string
if r.ReceiveTimeout != 0 {
addUrl = fmt.Sprintf("?receiveTimeout=%v", r.ReceiveTimeout)
}
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return c.GreenAPI.Request("GET", "receiveNotification", jsonData, WithGetParams(addUrl))
}
// ------------------------------------------------------------------ DeleteNotification
type RequestDeleteNotification struct {
ReceiptId int `json:"receiptId"`
}
// Deleting an incoming notification from the notification queue.
//
// https://green-api.com/en/docs/api/receiving/technology-http-api/DeleteNotification/
func (c ReceivingCategory) DeleteNotification(receiptId int) (*APIResponse, error) {
addUrl := fmt.Sprintf("/%v", receiptId)
return c.GreenAPI.Request("DELETE", "deleteNotification", nil, WithGetParams(addUrl))
}
// ------------------------------------------------------------------ DownloadFile
type RequestDownloadFile struct {
ChatId string `json:"chatId"`
IdMessage string `json:"idMessage"`
}
// Downloading incoming and outgoing files from a chat.
//
// https://green-api.com/en/docs/api/receiving/files/DownloadFile/
func (c ReceivingCategory) DownloadFile(chatId, idMessage string) (*APIResponse, error) {
err := ValidateChatId(chatId)
if err!=nil {
return nil, err
}
r := &RequestDownloadFile{
ChatId: chatId,
IdMessage: idMessage,
}
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return c.GreenAPI.Request("POST", "downloadFile", jsonData)
}