-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_sending.cpp
296 lines (260 loc) · 10.6 KB
/
test_sending.cpp
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
#include "../include/greenapi.hpp"
/*
* Examples of sending methods working
* Rename the function to main to use
* https://green-api.com/en/docs/api/sending/
*/
int main_sending() {
/*
* Examples of sending methods working
* You need to provide your instance details from your personal account.
* Be sure to use the apiUrl and mediaUrl parameters specifically for the higher instance, so you will get the most stable API operation and minimal method response time.
* https://console.green-api.com
*/
greenapi::GreenApi instance1101000001{ "https://api.green-api.com","https://media.green-api.com","1101123456","87be9e9532fc49748f2a44b9242e55f2e89f4bf97ed6498f80" };
/*
* Example of using the sendMessage method
* The method is aimed for sending a text message to a personal or a group chat.
* @param message - nlohmann::json object with message parameters
* example json object nlohmann::json requestMessage{
{ "chatId","[email protected]" },
{ "message","I use GREEN-API to send this message to you!" },
{ "quotedMessageId","3EB0C767D097B7C7C030" },
{ "linkPreview",true }
};
* https://green-api.com/en/docs/api/sending/SendMessage/
*/
nlohmann::json sendMessageJson{
{ "chatId","[email protected]" },
{ "message","I use GREEN-API to send this message to you!" }
};
greenapi::Response sendMessage = instance1101000001.sending.sendMessage(sendMessageJson);
if (sendMessage.error) {
std::cout << "sendMessage error: {status code: " << sendMessage.status_code << ", request time: " << sendMessage.total_time << ", body: " << sendMessage.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tidMessage: " << sendMessage.bodyJson["idMessage"] << "\n" << std::endl;
}
/*
* Example of using the sendPoll method
* This method is intended for sending messages with a poll to a private or group chat.
* @param message - nlohmann::json object with message parameters
* example json object nlohmann::json requestMessage{
{ "chatId","[email protected]" },
{ "message","Please choose the color:" },
{"options", {
{{"optionName", "green"}},
{{"optionName", "red"}},
{{"optionName", "blue"}}
}},
{ "multipleAnswers",true },
{ "quotedMessageId","3EB0C767D097B7C7C030" }
};
* https://green-api.com/en/docs/api/sending/SendPoll/
*/
nlohmann::json sendPollJson{
{ "chatId","[email protected]" },
{ "message","Please choose the color:" },
{"options", {
{{"optionName", "green"}},
{{"optionName", "red"}},
{{"optionName", "blue"}}
}
},
};
greenapi::Response sendPoll = instance1101000001.sending.sendPoll(sendPollJson);
if (sendPoll.error) {
std::cout << "sendPoll error: {status code: " << sendPoll.status_code << ", request time: " << sendPoll.total_time << ", body: " << sendPoll.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tidMessage: " << sendPoll.bodyJson["idMessage"] << "\n" << std::endl;
}
/*
* Example of using the SendFileByUpload method
* The method is aimed for sending a file uploaded by form (form-data). The message will be added to the send queue, in the response you will receive a link to the downloaded file.
* @param file - nlohmann::json obj containing file fields
* @param text - nlohmann::json obj containing text fields
* example json object nlohmann::json requestFile{{ "file","C:/1.png" }};
* example json object nlohmann::json requestMessage{
{ "chatId","[email protected]" },
{ "caption","I use GREEN-API to send this message to you!" },
{ "fileName","1.png" },
{ "quotedMessageId","" }
};
* https://green-api.com/en/docs/api/sending/SendFileByUpload/
*/
nlohmann::json sendFileByUploadFileJson{
{ "file","C:/1.png" }
};
nlohmann::json sendFileByUploadJson{
{ "chatId","[email protected]" },
{ "caption","I use GREEN-API to send this message to you!" },
{ "fileName","1.png" }
};
greenapi::Response sendFileByUpload = instance1101000001.sending.sendFileByUpload(sendFileByUploadFileJson, sendFileByUploadJson);
if (sendFileByUpload.error) {
std::cout << "sendFileByUpload error: {status code: " << sendFileByUpload.status_code << ", request time: " << sendFileByUpload.total_time << ", body: " << sendFileByUpload.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tidMessage: " << sendFileByUpload.bodyJson["idMessage"] << std::endl;
std::cout << "\turlFile: " << sendFileByUpload.bodyJson["urlFile"] << "\n" << std::endl;
}
/*
* Example of using the SendFileByUrl method
* The method is aimed for sending a file uploaded by Url.
* @param text - nlohmann::json obj containing text fields
* example json object nlohmann::json requestMessage{
{ "chatId","[email protected]" },
{ "urlFile","https://storage/1101123456/13238852123456.png" },
{ "fileName","1.png" },
{ "caption","I use GREEN-API to send this message to you!" },
{ "quotedMessageId","" }
};
* https://green-api.com/en/docs/api/sending/SendFileByUrl/
*/
nlohmann::json SendFileByUrlJson{
{ "chatId","[email protected]" },
{ "urlFile","https://sw-media-1101.storage.yandexcloud.net/1101123456s/13238852-be73-4f8c-a973-966d2730ce15.png" },
{ "fileName","1.png" },
{ "caption","I use GREEN-API to send this message to you!" }
};
greenapi::Response sendFileByUrl = instance1101000001.sending.sendFileByUrl(SendFileByUrlJson);
if (sendFileByUrl.error) {
std::cout << "sendFileByUrl error: {status code: " << sendFileByUrl.status_code << ", request time: " << sendFileByUrl.total_time << ", body: " << sendFileByUrl.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tidMessage: " << sendFileByUrl.bodyJson["idMessage"] << std::endl;
}
/*
* Example of using the UploadFile method
* The method is designed to upload a file to the cloud storage, which can be sent using the sendFileByUrl method.
* @param file - nlohmann::json obj containing file fields
* @param header - nlohmann::json obj containing text fields
* example json object nlohmann::json requestFile{{ "file","C:/1.png" }};
* example json object nlohmann::json requestHeader{
{ "GA-Filename","picture.png" },
{ "Content-Type","image/png" }
};
* https://green-api.com/en/docs/api/sending/UploadFile/
*/
nlohmann::json uploadFileFileJson{
{ "file","C:/1.png" }
};
greenapi::Response uploadFile = instance1101000001.sending.uploadFile(uploadFileFileJson);
if (uploadFile.error) {
std::cout << "uploadFile error: {status code: " << uploadFile.status_code << ", request time: " << uploadFile.total_time << ", header: " << uploadFile.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\turlFile: " << uploadFile.bodyJson["urlFile"] << "\n" << std::endl;
}
/*
* Example of using the getFileSaveTime method
* The method allows you to get the time of uploading a file to the GREEN API storage and its deletion
* @param url - GREEN API link to the downloaded file
*/
greenapi::Response getFileSaveTime = instance1101000001.sending.getFileSaveTime("https://sw-media-out.storage.yandexcloud.net/1101123456/1234567890123456789.png");
if (getFileSaveTime.error) {
std::cout << "getFileSaveTime error: {status code: " << getFileSaveTime.status_code << ", request time: " << getFileSaveTime.total_time << ", header: " << getFileSaveTime.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tcurrentDate: " << getFileSaveTime.headers.currentDate << "\n" << std::endl;
std::cout << "\tuploadDate: " << getFileSaveTime.headers.uploadDate << "\n" << std::endl;
std::cout << "\texpirationDate: " << getFileSaveTime.headers.expirationDate << "\n" << std::endl;
}
/*
* Example of using the SendLocation method
* The method is aimed for sending location message.
* @param text - nlohmann::json obj containing text fields
* example json object nlohmann::json requestMessage{
{ "chatId","[email protected]" },
{"nameLocation", "nameLocation"},
{"address", "address"},
{"latitude", 20.24},
{"longitude", 20.24},
{ "quotedMessageId","" }
};
* https://green-api.com/en/docs/api/sending/SendLocation/
*/
nlohmann::json sendLocationJson{
{ "chatId","[email protected]" },
{"nameLocation", "nameLocation"},
{"address", "address"},
{"latitude", 20.24},
{"longitude", 20.24}
};
greenapi::Response sendLocation = instance1101000001.sending.sendLocation(sendLocationJson);
if (sendLocation.error) {
std::cout << "sendLocation error: {status code: " << sendLocation.status_code << ", request time: " << sendLocation.total_time << ", body: " << sendLocation.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tidMessage: " << sendLocation.bodyJson["idMessage"] << std::endl;
}
/*
* Example of using the SendContact method
* The method is aimed for sending a contact message.
* @param text - nlohmann::json obj containing text fields
* example json object nlohmann::json requestMessage{
{ "chatId","[email protected]" },
{"contact", {
{{"phoneContact", 71234567890}},
{{"firstName", "firstName"}},
{{"middleName", "middleName"}},
{{"lastName", "lastName"}},
{{"company", "company"}}
}
},
{ "quotedMessageId","" }
};
* https://green-api.com/en/docs/api/sending/SendContact/
*/
nlohmann::json sendContactJson{
{ "chatId","[email protected]" },
{"contact", {
{"phoneContact", 71234567890},
{"firstName", "firstName"},
{"middleName", "middleName"},
{"lastName", "lastName"},
{"company", "company"}
}
}
};
greenapi::Response sendContact = instance1101000001.sending.sendContact(sendContactJson);
if (sendContact.error) {
std::cout << "sendContact error: {status code: " << sendContact.status_code << ", request time: " << sendContact.total_time << ", body: " << sendContact.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tidMessage: " << sendContact.bodyJson["idMessage"] << std::endl;
}
/*
* Example of using the ForwardMessages method
* The method is intended for forwarding messages to a personal or group chat.
* @param text - nlohmann::json obj containing text fields
* example json object nlohmann::json requestMessage{
{ "chatId","[email protected]" },
{ "chatIdFrom","[email protected]" },
{"messages", {
"BAE5DBB8DEABDA22",
"BAE5BBA9BE3142D8"
}
}
};
* https://green-api.com/en/docs/api/sending/ForwardMessages/
*/
nlohmann::json forwardMessagesJson{
{ "chatId","[email protected]" },
{ "chatIdFrom","[email protected]" },
{"messages", {
"optionName",
"optionName"
}
}
};
greenapi::Response forwardMessages = instance1101000001.sending.forwardMessages(forwardMessagesJson);
if (forwardMessages.error) {
std::cout << "forwardMessages error: {status code: " << forwardMessages.status_code << ", request time: " << forwardMessages.total_time << ", body: " << forwardMessages.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tmessages: " << forwardMessages.bodyJson["messages"] << std::endl;
}
return 0;
}