-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot_easy_api.cpp
482 lines (445 loc) · 12 KB
/
bot_easy_api.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include <curl/curl.h>
#include <stdio.h>
#include <string>
#include "writefn_data.h"
#include "botkey.h"
#include "termcolor.h"
#include "tgtypes.h"
#include <assert.h>
#include <string.h>
//
// Bot easy API
//
int easy_get_http_code(CURL *c)
{
int st = 0;
int st2 = curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &st);
return st2 == CURLE_OK ? st : - st2;
}
/// @brief Prints HTTP code and response.
void easy_print_http_code(CURL *c, writefn_data *d = 0)
{
int status = 0;
curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &status);
printf("HTTP status: %s(%d)\n",
(status < 400) ? GREEN("OK") : RED("FAILED"), status);
if (!d) {
return;
}
printf("%s\n", d->ptr);
}
int easy_perform_commandstr(CURL *c, const char *url, writefn_data *data,
bool print_result = true)
{
assert(data);
printf("Perform command %s\n", url);
curl_easy_setopt(c, CURLOPT_URL, url);
curl_easy_setopt(c, CURLOPT_WRITEDATA, data);
CURLcode result = curl_easy_perform(c);
if (result != CURLE_OK) {
long code = 0;
curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &code);
fprintf(stderr, RED("Failed to answer the user: ")
"HTTP status " RED("%ld") ", curl err code: %d"
", description: %s\n", code, result, curl_easy_strerror(result));
// return result;
return -result;
}
if (print_result) {
easy_print_http_code(c);
}
return result;
}
int easy_perform_commandstr(CURL *c, const char *url)
{
writefn_data d;
writefn_data_init(d);
int result = easy_perform_commandstr(c, url, &d);
writefn_data_free(d);
return result;
}
int easy_perform_getUpdates(CURL *c, writefn_data *d, size_t poll_time = 0, TgInteger update_offset = 0)
{
std::string s = BOT_URL "getUpdates";
bool que = false;
if (update_offset != 0) {
que = true;
s += "?offset=" + std::to_string(update_offset);
}
if (poll_time != 0) {
s += (que ? "&timeout=": "?timeout=") + std::to_string(poll_time);
}
int r = easy_perform_commandstr(c, s.c_str(), d);
return r;
}
int easy_perform_getUpdates_auto(CURL *c)
{
writefn_data d;
writefn_data_init(d);
int r = easy_perform_getUpdates(c, &d);
easy_print_http_code(c);
writefn_data_free(d);
return r;
}
int easy_perform_sendMessage(CURL *c, const char *chat_id,
const char *msg, TgMessageParseMode mode, TgInteger reply_id,
const char *additional = 0, writefn_data *d2 = 0)
{
static const char *modes[] = {
"Markdown",
"HTML",
};
std::string query = BOT_URL "sendMessage";
query += "?chat_id=" + std::string(chat_id) + "&text=" + std::string(msg);
writefn_data d;
writefn_data_init(d);
if (mode != TgMessageParse_Normal) {
query += "&parse_mode=";
query += modes[mode - 1];
}
if (additional) {
query += additional;
}
if (reply_id) {
query += "&reply_to_message_id=" + std::to_string(reply_id);
}
int r = easy_perform_commandstr(c, query.c_str(), &d);
if (d2 == 0) {
writefn_data_free(d);
} else {
*d2 = d;
}
return r;
}
int easy_perform_sendMessage(CURL *c, TgInteger chat_id,
const char *msg, TgMessageParseMode mode, TgInteger reply_id,
const char *additional = 0, writefn_data *d2 = 0)
{
return easy_perform_sendMessage(c, std::to_string(chat_id).c_str(),
msg, mode, reply_id, additional, d2);
}
int easy_perform_sendMessage_s(CURL *c, const char *chat_id,
const char *msg, bool markdown,
const char *additional = 0, TgInteger reply_id = 0,
writefn_data *d2 = 0)
{
return easy_perform_sendMessage(c, chat_id, msg,
markdown ? TgMessageParse_Markdown :
TgMessageParse_Normal, reply_id, additional, d2);
}
int easy_perform_sendMessage(CURL *c, TgInteger chat_id, const char *msg,
bool markdown, const char *additional = 0,
TgInteger reply_id = 0,
writefn_data *d = 0)
{
return easy_perform_sendMessage_s(c, std::to_string(chat_id).c_str(),
msg, markdown, additional, reply_id, d);
}
int easy_perform_sendChatAction(CURL *c, TgInteger chat_id,
const char *action)
{
std::string s = BOT_URL "sendChatAction?chat_id=" +
std::to_string(chat_id) + "&action=" +
std::string(action);
writefn_data d;
writefn_data_init(d);
int res = easy_perform_commandstr(c, s.c_str(), &d);
printf("%s\n",d.ptr);
writefn_data_free(d);
return res;
}
int easy_perform_sendSticker(CURL *c, TgInteger chat_id, const char *sticker_id,
TgInteger reply_id = 0, const char *additional = 0)
{
writefn_data d;
writefn_data_init(d);
std::string query = BOT_URL "sendSticker";
query += "?chat_id=" + std::to_string(chat_id) + "&sticker=";
query += sticker_id;
if (additional) {
query += additional;
}
if (reply_id) {
query += "&reply_to_message_id=" + std::to_string(reply_id);
}
int r = easy_perform_commandstr(c, query.c_str(), &d);
writefn_data_free(d);
return r;
}
int easy_perform_sendLocation(CURL *c, TgInteger chat_id,
const TgLocation &loc,
TgInteger reply_id = 0, writefn_data *d = 0)
{
std::string s = BOT_URL + std::string("sendLocation?latitude=") +
std::to_string(loc.latitude) + "&longitude=" +
std::to_string(loc.longitude) + "&chat_id=" +
std::to_string(chat_id);
if (reply_id) {
s += "&reply_to_message_id=" + std::to_string(reply_id);
}
return easy_perform_commandstr(c, s.c_str(), d);
// return 0;
}
int easy_perform_sendVenue(CURL *c, TgInteger chat_id,
const TgLocation &loc,
const char *title, const char *address,
TgInteger reply_id = 0, writefn_data *d = 0)
{
std::string s = BOT_URL "sendVenue";
s += std::string("?chat_id=") + std::to_string(chat_id);
char *t = curl_easy_escape(c, title, 0);
char *a = curl_easy_escape(c, address, 0);
s += std::string("&title=") + std::string(t) + "&address="
+ std::string(a) + "&latitude="
+ std::to_string(loc.latitude) + "&longitude="
+ std::to_string(loc.longitude);
curl_free(t);
curl_free(a);
if (reply_id) {
s += "&reply_to_message_id=" + std::to_string(reply_id);
}
return easy_perform_commandstr(c, s.c_str(), d);
}
int easy_perform_leaveChat(CURL *c, TgInteger chatId)
{
return easy_perform_commandstr(c,
(std::string(BOT_URL "leaveChat?chat_id=")+std::to_string(chatId)).c_str());
}
int easy_perform_forwardMessage_(CURL *c, TgInteger chatId,
TgInteger messageId, const char *channel)//,
// writefn_data *d = 0)
{
writefn_data data;
writefn_data_init(data);
std::string s = BOT_URL "forwardMessage?from_chat_id="
+ std::string(channel) + "&chat_id="
+ std::to_string(chatId)
+ "&message_id=" + std::to_string(messageId);
int res = easy_perform_commandstr(c, s.c_str(), &data);
printf("Result: %s", data.ptr);
writefn_data_free(data);
if (res != CURLE_OK) {
return -res;
}
return easy_get_http_code(c);
}
int easy_perform_forwardMessage(CURL *c, TgInteger chatId,
TgInteger messageId, const char *channel)
{
std::string ch = "@";
ch += channel;
return easy_perform_forwardMessage_(c, chatId, messageId,
ch.c_str());
}
int easy_perform_forwardMessage(CURL *c, TgInteger chatId,
TgInteger messageId, TgInteger fromChatId)
{
return easy_perform_forwardMessage_(c, chatId, messageId,
std::to_string(fromChatId).c_str());
}
int easy_perform_deleteMessage(CURL *c, TgInteger chatId,
TgInteger messageId)
{
easy_perform_commandstr(c, (std::string(BOT_URL)
+ "deleteMessage?chat_id=" + std::to_string(chatId)
+ "&message_id=" + std::to_string(messageId)).c_str() );
return 0;
}
int easy_perform_deleteMessage(CURL *c, const char *groupName,
TgInteger messageId)
{
return 1; // easy_perform_deleteMessage(c, , messageId);
}
int easy_perform_sendGame(CURL *c, TgInteger chat_id,
const char *game_name, TgInteger reply_id,
const char *additional, writefn_data *d = 0)
{
std::string query = BOT_URL "sendGame?chat_id=" +
std::to_string(chat_id) + "&game_short_name=" +
std::string(game_name);
if (reply_id) {
query += "&reply_to_message=" + std::to_string(reply_id);
}
if (additional) {
query += additional;
}
int res = easy_perform_commandstr(c, query.c_str(), d);
return res == CURLE_OK ? easy_get_http_code(c) : -res;
}
//
// chunked msg
//
bool ishexnum(int c)
{
return (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F');
}
bool find_percent_enc(char *buf, size_t &cur_off, size_t max_offset)
{
size_t old_off = cur_off;
while (ishexnum(buf[cur_off])) {
if (cur_off == 0) {
cur_off = old_off;
return false;
}
cur_off--;
}
if (buf[cur_off] == '%') {
return true;
}
return false;
}
typedef bool (*chunk_perform_callback)(const char *buf, size_t sz,
void *param);
bool easy_perform_chunked_message(char *buf, size_t bufsz,
const size_t MAX_MSG_SIZE, chunk_perform_callback cb,
void *param)
{
char *chunk = buf;
char old;
size_t counter = MAX_MSG_SIZE;
if (bufsz < counter) {
cb(buf, bufsz, param);
return true;
}
for (size_t i = 0; i < bufsz; i += counter, bufsz -= counter) {
counter = MAX_MSG_SIZE;
if (!find_percent_enc(chunk, counter, MAX_MSG_SIZE)) {
// assert(0);
}
old = chunk[counter];
chunk [counter] = '\0';
cb(chunk, bufsz, param);
chunk [counter] = old;
chunk += counter;
}
cb(chunk, bufsz, param);
return true;
}
int easy_perform_sendEscapedLongMessage(CURL *curl, TgInteger chatId,
const char *msg, size_t length,
TgMessageParseMode parseMode, TgInteger replyId,
const char *additional, writefn_data *d)
{
char *a = curl_easy_escape(curl, msg, length);
struct sndmsg_chunk_ctx {
CURL *c;
TgInteger chat;
TgMessageParseMode mode;
TgInteger reply_id;
const char *additional;
writefn_data *d;
static bool snd(const char *buf,
size_t sz, void *p){
sndmsg_chunk_ctx *c =
(sndmsg_chunk_ctx*)p;
easy_perform_sendMessage(c->c, c->chat, buf, c->mode,
c->reply_id, c->additional, c->d);
return true;
}
} c = {
curl,
chatId,
parseMode,
replyId,
additional,
d
};
int result = easy_perform_chunked_message(a, strlen(a),
4096, c.snd, &c);
curl_free(a);
return result;
}
bool easy_bot_check_command(const char *cmd, size_t sz, const char *name,
size_t name_sz, size_t *cmd_end_off, bool *shortcmd = 0)
{
if (cmd[0] != '/') {
return false;
}
const char *tok = strpbrk(cmd + 1, "@ ");
if (!tok) {
if (cmd_end_off) {
*cmd_end_off = sz;
}
if (shortcmd) {
*shortcmd = true;
}
return true;
}
switch (*tok) {
case '@':
if (strncmp(tok + 1, name, name_sz) == 0) {
if (cmd_end_off) {
*cmd_end_off = tok - cmd;
}
if (shortcmd) {
*shortcmd = false;
}
return true;
}
return false;
case ' ':
if (cmd_end_off) {
*cmd_end_off = tok - cmd;
}
if (shortcmd) {
*shortcmd = true;
}
return true;
}
return true;
}
#if 0
static void test_cmd(CURL *c, TgInteger chatId)
{
struct {
const_string cmd;
bool ret, shrt;
size_t off;
} expected[] = {
{ MAKE_CONST_STR("huita"), false, false, 0 },
{ MAKE_CONST_STR("/h@bot2"), false, false, 0 },
{ MAKE_CONST_STR("/h@bot2 h2"), false, false, 0 },
{ MAKE_CONST_STR("/h"), true, true, 2 },
{ MAKE_CONST_STR("/h h2"), true, true, 2 },
{ MAKE_CONST_STR("/h@bot"), true, false, 6 },
{ MAKE_CONST_STR("/h@bot h2"), true, false, 6 }
};
const_string name = MAKE_CONST_STR("bot");
size_t off = 0;
bool shrt = 0;
bool r = 0;
std::string msg = "Test suite.\n";
for (size_t i = 0; i < COUNTOF(expected); i++) {
msg += "Test #*" + std::to_string(i) + "*. Test string: `"
+ std::string(expected[i].cmd.str) + "`, expected retval: " + std::string(expected[i].ret ? "true" : "false") ;
if (expected[i].ret) {
msg += ", shortflag: " + std::string(expected[i].shrt ? "true" : "false") + ", offset: " + std::to_string(expected[i].off);
}
bool r = easy_bot_check_command(expected[i].cmd.str, expected[i].cmd.len,
name.str, name.len, &off, &shrt);
msg += ", actual: retval: " + ((expected[i].ret == r) ?
std::string(r ? "true" : "false") :
std::string(r ? "*TRUE*" : "*FALSE*"));
if (r) {
msg += ", shortflag: " + ((expected[i].shrt == shrt) ?
std::string(shrt ? "true" : "false") :
std::string(shrt ? "*TRUE*" : "*FALSE*"));
bool boldoff = off != expected[i].off;
msg += ", off: ";
if (boldoff) {
msg += "*";
}
msg += std::to_string(off);
if (boldoff) {
msg += "*";
}
}
msg += "\n";
}
char *s = curl_easy_escape(c, msg.c_str(), msg.length());
easy_perform_sendMessage(c, chatId, s, true);
curl_free(s);
}
#endif