-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjournals.go
160 lines (129 loc) · 3.81 KB
/
journals.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
package greenapi
import (
"encoding/json"
"fmt"
)
type JournalsCategory struct {
GreenAPI GreenAPIInterface
}
// ------------------------------------------------------------------ GetChatHistory
type RequestGetChatHistory struct {
ChatId string `json:"chatId"`
Count int `json:"count,omitempty"`
}
type GetChatHistoryOption func(*RequestGetChatHistory) error
// The number of messages to get. The default is 100
func OptionalCount(count int) GetChatHistoryOption {
return func(r *RequestGetChatHistory) error {
r.Count = count
return nil
}
}
// Getting a chat messages history.
//
// https://green-api.com/en/docs/api/journals/GetChatHistory/
//
// Add optional arguments by passing these functions:
// OptionalCount(count int) <- The number of messages to get. The default is 100
func (c JournalsCategory) GetChatHistory(chatId string, options ...GetChatHistoryOption) (*APIResponse, error) {
err := ValidateChatId(chatId)
if err!=nil {
return nil, err
}
r := &RequestGetChatHistory{
ChatId: chatId,
}
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", "getChatHistory", jsonData)
}
// ------------------------------------------------------------------ GetMessage
type RequestGetMessage struct {
ChatId string `json:"chatId"`
IdMessage string `json:"idMessage"`
}
// Getting a message information.
//
// https://green-api.com/en/docs/api/journals/GetMessage/
func (c JournalsCategory) GetMessage(chatId, idMessage string) (*APIResponse, error) {
err := ValidateChatId(chatId)
if err!=nil {
return nil, err
}
r := &RequestGetMessage{
ChatId: chatId,
IdMessage: idMessage,
}
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return c.GreenAPI.Request("POST", "getMessage", jsonData)
}
// ------------------------------------------------------------------ LastIncomingMessages + LastOutgoingMessages
type RequestLastMessages struct {
Minutes int `json:"minutes,omitempty"`
}
type LastMessagesOption func(*RequestLastMessages) error
// Time in minutes for which the messages should be displayed (default is 1440 minutes)
func OptionalMinutes(minutes int) LastMessagesOption {
return func(r *RequestLastMessages) error {
r.Minutes = minutes
return nil
}
}
// Getting the last incoming messages of the account.
//
// https://green-api.com/en/docs/api/journals/LastIncomingMessages/
//
// Add optional arguments by passing these functions:
// OptionalMinutes(minutes int) <- Time in minutes for which the messages should be displayed (default is 1440 minutes)
func (c JournalsCategory) LastIncomingMessages(options ...LastMessagesOption) (*APIResponse, error) {
r := &RequestLastMessages{}
for _, o := range options {
err := o(r)
if err!=nil {
return nil, err
}
}
var addUrl string
if r.Minutes != 0 {
addUrl = fmt.Sprintf("?minutes=%v", r.Minutes)
}
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return c.GreenAPI.Request("GET", "lastIncomingMessages", jsonData, WithGetParams(addUrl))
}
// Getting the last outgoung messages of the account.
//
// https://green-api.com/en/docs/api/journals/LastOutgoingMessages/
//
// OptionalMinutes(minutes int) <- Time in minutes for which the messages should be displayed (default is 1440 minutes)
func (c JournalsCategory) LastOutgoingMessages(options ...LastMessagesOption) (*APIResponse, error) {
r := &RequestLastMessages{}
for _, o := range options {
err := o(r)
if err!=nil {
return nil, err
}
}
var addUrl string
if r.Minutes != 0 {
addUrl = fmt.Sprintf("?minutes=%v", r.Minutes)
}
jsonData, err := json.Marshal(r)
if err != nil {
return nil, err
}
return c.GreenAPI.Request("GET", "lastOutgoingMessages", jsonData, WithGetParams(addUrl))
}