-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_sending.cpp
More file actions
380 lines (336 loc) · 14 KB
/
Copy pathtest_sending.cpp
File metadata and controls
380 lines (336 loc) · 14 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#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{ "api.green-api.com","api.green-api.com","710701676160","36f20dae0c964139a06f9d8223ea479e3722e557bc234be2a0" };
/*
* 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","71234567890@c.us" },
{ "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","71234567890@c.us" },
{ "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","71234567890@c.us" },
{ "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","71234567890@c.us" },
{ "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","71234567890@c.us" },
{ "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","71234567890@c.us" },
{ "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","71234567890@c.us" },
{ "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","71234567890@c.us" },
{ "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","71234567890@c.us" },
{"nameLocation", "nameLocation"},
{"address", "address"},
{"latitude", 20.24},
{"longitude", 20.24},
{ "quotedMessageId","" }
};
* https://green-api.com/en/docs/api/sending/SendLocation/
*/
nlohmann::json sendLocationJson{
{ "chatId","71234567890@c.us" },
{"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","71234567890@c.us" },
{"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","71234567890@c.us" },
{"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","71234567890@c.us" },
{ "chatIdFrom","71234567890@c.us" },
{"messages", {
"BAE5DBB8DEABDA22",
"BAE5BBA9BE3142D8"
}
}
};
* https://green-api.com/en/docs/api/sending/ForwardMessages/
*/
nlohmann::json forwardMessagesJson{
{ "chatId","71234567890@c.us" },
{ "chatIdFrom","71234567890@c.us" },
{"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;
}
/*
* Example of using the sendInteractiveButtons method
* The method is aimed for sending a message with interactive buttons (types: copy, call, url) to a personal chat.
* Up to 3 buttons per message, button text up to 25 characters.
* @param message - nlohmann::json object with message parameters
* example json object nlohmann::json requestMessage{
{ "chatId","71234567890@c.us" },
{ "header","Header text" },
{ "body","Message body text" },
{ "footer","Footer text" },
{ "buttons", {
{{"type","url"}, {"buttonId","1"}, {"buttonText","Open website"}, {"url","https://green-api.com"}},
{{"type","call"}, {"buttonId","2"}, {"buttonText","Call us"}, {"phoneNumber","71234567890"}},
{{"type","copy"}, {"buttonId","3"}, {"buttonText","Copy code"}, {"copyCode","ABC123"}}
}
}
};
* https://green-api.com/en/docs/api/sending/SendInteractiveButtons/
*/
nlohmann::json sendInteractiveButtonsJson{
{ "chatId","71234567890@c.us" },
{ "header","Choose an action" },
{ "body","Press the button you need" },
{ "footer","GREEN API" },
{ "buttons", {
{{"type","url"}, {"buttonId","1"}, {"buttonText","Open website"}, {"url","https://green-api.com"}},
{{"type","call"}, {"buttonId","2"}, {"buttonText","Call us"}, {"phoneNumber","71234567890"}},
{{"type","copy"}, {"buttonId","3"}, {"buttonText","Copy code"}, {"copyCode","ABC123"}}
}
}
};
greenapi::Response sendInteractiveButtons = instance1101000001.sending.sendInteractiveButtons(sendInteractiveButtonsJson);
if (sendInteractiveButtons.error) {
std::cout << "sendInteractiveButtons error: {status code: " << sendInteractiveButtons.status_code << ", request time: " << sendInteractiveButtons.total_time << ", body: " << sendInteractiveButtons.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tidMessage: " << sendInteractiveButtons.bodyJson["idMessage"] << "\n" << std::endl;
}
/*
* Example of using the sendInteractiveButtonsReply method
* The method is aimed for sending a message with reply buttons (text buttons, each can be pressed once) to a personal chat.
* Up to 3 buttons per message, button text up to 25 characters. Beta version.
* @param message - nlohmann::json object with message parameters
* example json object nlohmann::json requestMessage{
{ "chatId","71234567890@c.us" },
{ "header","Survey" },
{ "body","Do you like our service?" },
{ "footer","Choose one option" },
{ "buttons", {
{{"buttonId","1"}, {"buttonText","Yes"}},
{{"buttonId","2"}, {"buttonText","No"}},
{{"buttonId","3"}, {"buttonText","Not sure"}}
}
}
};
* https://green-api.com/en/docs/api/sending/SendInteractiveButtonsReply/
*/
nlohmann::json sendInteractiveButtonsReplyJson{
{ "chatId","71234567890@c.us" },
{ "header","Survey" },
{ "body","Do you like our service?" },
{ "footer","Choose one option" },
{ "buttons", {
{{"buttonId","1"}, {"buttonText","Yes"}},
{{"buttonId","2"}, {"buttonText","No"}},
{{"buttonId","3"}, {"buttonText","Not sure"}}
}
}
};
greenapi::Response sendInteractiveButtonsReply = instance1101000001.sending.sendInteractiveButtonsReply(sendInteractiveButtonsReplyJson);
if (sendInteractiveButtonsReply.error) {
std::cout << "sendInteractiveButtonsReply error: {status code: " << sendInteractiveButtonsReply.status_code << ", request time: " << sendInteractiveButtonsReply.total_time << ", body: " << sendInteractiveButtonsReply.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tidMessage: " << sendInteractiveButtonsReply.bodyJson["idMessage"] << "\n" << std::endl;
}
return 0;
}