-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_serviceMethods.cpp
More file actions
212 lines (171 loc) · 8.96 KB
/
Copy pathtest_serviceMethods.cpp
File metadata and controls
212 lines (171 loc) · 8.96 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
#include "../include/greenapi.hpp"
/*
* Examples of service methods working
* Rename the function to main to use
* https://green-api.com/en/docs/api/service/
*/
int main_serviceMethods() {
/*
* Examples of service 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 checkWhatsapp method
* The method checks WhatsApp account availability on a phone number.
* https://green-api.com/en/docs/api/service/CheckWhatsapp/
* @param settings - phone number in the format 71234567890
*/
const unsigned long long phoneNumber = 71234567890;
greenapi::Response checkWhatsapp = instance1101000001.serviceMethods.checkWhatsapp(phoneNumber);
if (checkWhatsapp.error) {
std::cout << "checkWhatsapp error: {status code: " << checkWhatsapp.status_code << ", request time: " << checkWhatsapp.total_time << ", body: " << checkWhatsapp.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\texistsWhatsapp: " << checkWhatsapp.bodyJson["existsWhatsapp"] << "\n" << std::endl;
}
/*
* The method returns a user or a group chat avatar.
* https://green-api.com/en/docs/api/service/GetAvatar/
* @param message - data with chat number
*/
nlohmann::json messageGetAvatar{ {"chatId","71234567890@c.us"} };
greenapi::Response getAvatar = instance1101000001.serviceMethods.getAvatar(messageGetAvatar);
if (getAvatar.error) {
std::cout << "getAvatar error: {status code: " << getAvatar.status_code << ", request time: " << getAvatar.total_time << ", body: " << getAvatar.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\turlAvatar: " << getAvatar.bodyJson["urlAvatar"] << "\n" << std::endl;
std::cout << "\tavailable: " << getAvatar.bodyJson["available"] << "\n" << std::endl;
}
/*
* The method is aimed for getting a list of the current account contacts.
* https://green-api.com/en/docs/api/service/GetContacts/
*/
greenapi::Response getContacts = instance1101000001.serviceMethods.getContacts();
if (getContacts.error) {
std::cout << "getContacts error: {status code: " << getContacts.status_code << ", request time: " << getContacts.total_time << ", body: " << getContacts.bodyStr << "}" << "\n" << std::endl;
}
else {
if (getContacts.bodyJson.is_array() && getContacts.bodyJson[0].contains("name") && getContacts.bodyJson[0].contains("id")) {
std::cout << "\tid: " << getContacts.bodyJson[0]["id"].get<std::string>() << "\n" << std::endl;
std::cout << "\tname: " << getContacts.bodyJson[0]["name"].get<std::string>() << "\n" << std::endl;
}
else {
std::cerr << "JSON is not an array or the first object does not contain 'name' or 'id'." << std::endl;
}
}
/*
* The method is aimed for getting information on a contact.
* https://green-api.com/en/docs/api/service/GetContactInfo/
* @param message - data with chat number
*/
nlohmann::json messageGetContactInfo{ {"chatId","71234567890@c.us"} };
greenapi::Response getContactInfo = instance1101000001.serviceMethods.getContactInfo(messageGetContactInfo);
if (getContactInfo.error) {
std::cout << "getContactInfo error: {status code: " << getContactInfo.status_code << ", request time: " << getContactInfo.total_time << ", body: " << getContactInfo.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tgetContactInfoStr: " << getContactInfo.bodyStr << "\n" << std::endl;
std::cout << "\tavatar: " << getContactInfo.bodyJson["avatar"] << "\n" << std::endl;
}
/*
* The method deletes a message from a chat.
* https://green-api.com/en/docs/api/service/deleteMessage/
* @param message - data with chat number and message
*/
nlohmann::json messageDeleteMessage{ {"chatId","71234567890@c.us"}, {"idMessage","BAE5F4886F6F2D05"} };
greenapi::Response deleteMessage = instance1101000001.serviceMethods.deleteMessage(messageDeleteMessage);
if (deleteMessage.error) {
std::cout << "deleteMessage error: {status code: " << deleteMessage.status_code << ", request time: " << deleteMessage.total_time << ", body: " << deleteMessage.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tdeleteMessage: true" << "\n" << std::endl;
}
/*
* The method archives a chat. One can archive chats that have at least one incoming message.
* https://green-api.com/en/docs/api/service/archiveChat/
* @param message - data with chat number
*/
nlohmann::json messageaAchiveChat{ {"chatId","71234567890@c.us"} };
greenapi::Response archiveChat = instance1101000001.serviceMethods.archiveChat(messageaAchiveChat);
if (archiveChat.error) {
std::cout << "archiveChat error: {status code: " << archiveChat.status_code << ", request time: " << archiveChat.total_time << ", body: " << archiveChat.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tarchiveChat: true" << "\n" << std::endl;
}
/*
* The method archives a chat. One can archive chats that have at least one incoming message.
* https://green-api.com/en/docs/api/service/archiveChat/
* @param message - data with chat number
*/
nlohmann::json messageaUnarchiveChat{ {"chatId","71234567890@c.us"} };
greenapi::Response unarchiveChat = instance1101000001.serviceMethods.unarchiveChat(messageaUnarchiveChat);
if (unarchiveChat.error) {
std::cout << "unarchiveChat error: {status code: " << unarchiveChat.status_code << ", request time: " << unarchiveChat.total_time << ", body: " << unarchiveChat.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tunarchiveChat: true" << "\n" << std::endl;
}
/*
* The method is aimed for changing settings of disappearing messages in chats.
* The standard settings of the application are to be used: 0 (off), 86400 (24 hours), 604800 (7 days), 7776000 (90 days).
* https://green-api.com/en/docs/api/service/SetDisappearingChat/
* @param message - data with chat number and time
*/
nlohmann::json messageaSetDisappearingChat{ {"chatId","71234567890@c.us"}, {"ephemeralExpiration", 0} };
greenapi::Response setDisappearingChat = instance1101000001.serviceMethods.setDisappearingChat(messageaSetDisappearingChat);
if (setDisappearingChat.error) {
std::cout << "setDisappearingChat error: {status code: " << setDisappearingChat.status_code << ", request time: " << setDisappearingChat.total_time << ", body: " << setDisappearingChat.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tchatId: " << setDisappearingChat.bodyJson["chatId"] << "\n" << std::endl;
std::cout << "\tephemeralExpiration: " << setDisappearingChat.bodyJson["ephemeralExpiration"] << "\n" << std::endl;
std::cout << "\tdisappearingMessagesInChat: " << setDisappearingChat.bodyJson["disappearingMessagesInChat"] << "\n" << std::endl;
}
/*
* Example of using the getChats method
* The method returns the list of chats of the current account in chronological order.
* Updates at most once per minute.
* https://green-api.com/en/docs/api/service/GetChats/
* @param count - number of chats to return (0 means all chats, default 0)
*/
// Get all chats
greenapi::Response getChats = instance1101000001.serviceMethods.getChats();
if (getChats.error) {
std::cout << "getChats error: {status code: " << getChats.status_code << ", request time: " << getChats.total_time << ", body: " << getChats.bodyStr << "}" << "\n" << std::endl;
}
else {
if (getChats.bodyJson.is_array() && !getChats.bodyJson.empty()) {
std::cout << "\tid: " << getChats.bodyJson[0]["id"] << "\n" << std::endl;
std::cout << "\tname: " << getChats.bodyJson[0]["name"] << "\n" << std::endl;
std::cout << "\ttype: " << getChats.bodyJson[0]["type"] << "\n" << std::endl;
}
else {
std::cout << "\tgetChats response: " << getChats.bodyStr << "\n" << std::endl;
}
}
/*
* Example of using the sendTyping method
* The method shows a "typing" or "recording audio" indicator in the specified chat.
* https://green-api.com/en/docs/api/service/SendTyping/
* @param message - nlohmann::json object with message parameters
* Required fields: chatId
* Optional fields: typingTime (1000-20000 ms), typingType ("recording" for audio)
* example json objects:
* Typing indicator: nlohmann::json message{ {"chatId","71234567890@c.us"}, {"typingTime",5000} };
* Recording indicator: nlohmann::json message{ {"chatId","71234567890@c.us"}, {"typingTime",3000}, {"typingType","recording"} };
*/
nlohmann::json messageSendTyping{ {"chatId","71234567890@c.us"}, {"typingTime", 5000} };
greenapi::Response sendTyping = instance1101000001.serviceMethods.sendTyping(messageSendTyping);
if (sendTyping.error) {
std::cout << "sendTyping error: {status code: " << sendTyping.status_code << ", request time: " << sendTyping.total_time << ", body: " << sendTyping.bodyStr << "}" << "\n" << std::endl;
}
else {
std::cout << "\tsendTyping: success" << "\n" << std::endl;
}
return 0;
}