-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbot.cpp
executable file
·293 lines (271 loc) · 6.05 KB
/
tbot.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
// Written on mac environment
#include <iostream>
#include <curl/curl.h>
#include <string>
#include <fstream>
#include <string.h>
#include <chrono>
#include <openssl/hmac.h>
#include <iomanip>
#include <sstream>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include <cstdlib>
#include <unistd.h>
#include <regex>
using json = nlohmann::json;
using namespace std;
const string baseUrl = "https://api.telegram.org/bot";
const string token = "5842114288:AAGMs2IbneIU-3BP0YHEfWAzxD_RjrS3wHM";
const string chatID = "1010606030";
bool FirstRun = true;
struct error
{
int typeNULL = 0;
};
error handler;
static string gs_strLastResponse;
json TempJForm;
/* concatenate the query parameters to &<key>=<value> */
string joinQueryParameters(const unordered_map<string, string> ¶meters)
{
string queryString = "";
for (auto it = parameters.cbegin(); it != parameters.cend(); ++it)
{
if (it == parameters.cbegin())
{
queryString += it->first + '=' + it->second;
}
else
{
queryString += '&' + it->first + '=' + it->second;
}
}
return queryString;
}
bool ErrorHandler(string message)
{
bool error = true;
json data = message;
if (!json::accept(message)) // prevents wierd nlohmann parse error due to binance reaspones
{
error = true;
cout << "parse error\n";
try
{
json j = json::parse(message);
}
catch (json::parse_error &e)
{
std::cout << e.what() << std::endl;
}
exit(3);
}
else if (data.is_null()) // is null
{
error = true;
cout << "null detected. Reinitializing request...." << endl;
}
/*is only square brackets returned ?*/
else if (message[0] == '[' && message[1] == ']' && handler.typeNULL < 20)
{
error = true;
cout << "empty square bracket detected. Reinitializing request...." << endl;
handler.typeNULL++;
cout << handler.typeNULL << endl;
}
/* sqaure bracket handler ended here*/
else
{
error = false;
}
return error;
}
/*Sending the HTTP Request and printing the response*/
void executeHTTPRequest(CURL *curl)
{
CURLcode res;
gs_strLastResponse = "";
/* Perform the request, res will get the return code */
do
{
res = curl_easy_perform(curl);
ofstream logger("tbot_log.txt", ios::app);
logger << "dumping " << gs_strLastResponse << "\n";
logger.close();
} while (ErrorHandler(gs_strLastResponse));
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
/*Write the server response to string for display*/
size_t WriteCallback(char *contents, size_t size, size_t nmemb, void *userp)
{
gs_strLastResponse = (const char *)contents;
gs_strLastResponse += '\n';
return size * nmemb;
}
void sendPublicRequest(CURL *curl, string urlPath, unordered_map<string, string> ¶meters)
{
string url = baseUrl + token + urlPath + '?';
if (!parameters.empty())
{
url += joinQueryParameters(parameters);
}
// cout << "url:" << url << endl;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
parameters.clear();
executeHTTPRequest(curl);
}
static string PrevMessage = "-1";
static int PrevID = -1;
static string command = "";
bool Listner()
{
CURL *curl;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
bool NewMessage = false;
unordered_map<string, string> parameters;
if (curl)
{
parameters.insert(
{"offset", "-1"});
sendPublicRequest(curl, "/getUpdates", parameters);
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
TempJForm = json::parse(gs_strLastResponse);
int i = 0;
string message;
auto TempMessage = TempJForm["result"][0]["message"]["text"];
auto TempID = TempJForm["result"][0]["update_id"];
message = TempMessage;
int ID = TempID;
command = message;
if ((!FirstRun && PrevMessage != command) || (PrevID != ID && PrevMessage == command))
{
NewMessage = true;
PrevMessage = command;
}
else if (PrevMessage == "-1" || PrevID == -1)
{
PrevMessage = command;
FirstRun = false;
}
PrevID = ID;
return NewMessage;
}
bool SendMessage(string message)
{
bool MessageStatus = false;
CURL *curl;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
bool NewMessage = false;
unordered_map<string, string> parameters;
if (curl)
{
parameters.insert(
{{"chat_id", chatID},
{"text", message}});
sendPublicRequest(curl, "/sendMessage", parameters);
MessageStatus = true;
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return MessageStatus;
}
string ToEscape(string text)
{
string escaped = "";
for (int i = 0; i < text.length(); i++)
{
if (text[i] == ' ')
escaped += "+";
else if (text[i] == '\n')
escaped += "%0A";
else
escaped += text[i];
}
return escaped;
}
void CommandExe()
{
usleep(2000000);
cout << "\t\tstarted" << endl;
while (true)
{
if (Listner())
{
if (command == "/stat")
{
int res = system("ps -C spot");
if (res == 0)
{
SendMessage("running");
}
else
SendMessage("Stopped");
}
else if (command == "/startcrypto")
{
system("nohup ./spot>log.txt & ");
int res = system("ps -C spot");
if (res == 0)
{
SendMessage(ToEscape("executed succussfuly"));
}
else
SendMessage(ToEscape("not executed succussfuly"));
}
else if (command == "/stopcrypto")
{
system("kill 18");
}
else if (command == "/profits")
{
double total;
int counter = 0;
string temp;
ifstream profit("profits.arb");
while (profit >> temp)
{
total += stod(temp);
counter++;
}
string pass = ToEscape("gained profit is " + to_string(total) + "\ntotal num of opportunities are " + to_string(counter));
SendMessage(pass);
profit.close();
}
else if (command == "/founds")
{
ifstream profit("profits.arb");
string total;
string temp;
while (profit >> temp)
{
total += temp + "\n";
}
if (total.empty())
{
total = "no opportunity found";
}
SendMessage(ToEscape(total));
profit.close();
}
else
{
SendMessage(ToEscape("unknown command"));
}
}
}
}
int main()
{
CommandExe();
}