forked from d4NY0H/raid-pokemon-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
executable file
·543 lines (450 loc) · 13.9 KB
/
functions.php
File metadata and controls
executable file
·543 lines (450 loc) · 13.9 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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
/**
* Send message.
* @param $chat_id
* @param array $text
*/
function sendMessage($chat_id, $text = array())
{
// Create response content array.
$reply_content = [
'method' => 'sendMessage',
'chat_id' => $chat_id,
'parse_mode' => 'HTML',
'text' => $text
];
if (isset($inline_keyboard)) {
$reply_content['reply_markup'] = ['inline_keyboard' => $inline_keyboard];
}
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Send request to telegram api.
curl_json_request($reply_json);
}
/**
* Send message.
* @param $chat_id
* @param array $text
* @param mixed $inline_keyboard
* @param array $merge_args
*/
function send_message($chat_id, $text = array(), $inline_keyboard = false, $merge_args = [])
{
// Create response content array.
$reply_content = [
'method' => 'sendMessage',
'chat_id' => $chat_id,
'parse_mode' => 'HTML',
'text' => $text
];
// Write to log.
debug_log('KEYS');
debug_log($inline_keyboard);
if (isset($inline_keyboard)) {
$reply_content['reply_markup'] = ['inline_keyboard' => $inline_keyboard];
}
if (is_array($merge_args) && count($merge_args)) {
$reply_content = array_merge_recursive($reply_content, $merge_args);
}
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Send request to telegram api.
return curl_json_request($reply_json);
}
/**
* Send location.
* @param $chat_id
* @param $lat
* @param $lon
* @param bool $inline_keyboard
* @return mixed
*/
function send_location($chat_id, $lat, $lon, $inline_keyboard = false)
{
// Create reply content array.
$reply_content = [
'method' => 'sendLocation',
'chat_id' => $chat_id,
'latitude' => $lat,
'longitude' => $lon
];
// Write to log.
debug_log('KEYS');
debug_log($inline_keyboard);
if (is_array($inline_keyboard)) {
$reply_content['reply_markup'] = ['inline_keyboard' => $inline_keyboard];
}
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Send request to telegram api and return response.
return curl_json_request($reply_json);
}
/**
* Echo message.
* @param $chat_id
* @param $text
*/
function sendMessageEcho($chat_id, $text)
{
// Create reply content array.
$reply_content = [
'method' => 'sendMessage',
'chat_id' => $chat_id,
'parse_mode' => 'HTML',
'text' => $text
];
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Echo json.
echo($reply_json);
}
/**
* Answer callback query.
* @param $query_id
* @param $text
*/
function answerCallbackQuery($query_id, $text)
{
// Create response array.
$response = [
'method' => 'answerCallbackQuery',
'callback_query_id' => $query_id,
'text' => $text
];
// Encode response to json format.
$json_response = json_encode($response);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($json_response, '>');
// Send request to telegram api.
curl_json_request($json_response);
}
/**
* Answer inline query.
* @param $query_id
* @param $contents
*/
function answerInlineQuery($query_id, $contents)
{
// Init empty result array.
$results = array();
// For each content.
foreach ($contents as $key => $row) {
// Get raid poll.
$text = show_raid_poll($row);
// Get inline keyboard.
$inline_keyboard = keys_vote($row);
// Create input message content array.
$input_message_content = [
'parse_mode' => 'HTML',
'message_text' => $text,
'disable_web_page_preview' => true
];
// Fill results array.
$results[] = [
'type' => 'article',
'id' => $query_id . $key,
'title' => ucfirst($row['pokemon']) . ' ' . unix2tz($row['ts_end'], $row['timezone']),
'description' => strval($row['gym_name']),
'input_message_content' => $input_message_content,
'reply_markup' => [
'inline_keyboard' => $inline_keyboard
]
];
}
// Create reply content array.
$reply_content = [
'method' => 'answerInlineQuery',
'inline_query_id' => $query_id,
'is_personal' => true,
'cache_time' => 10,
'results' => $results
];
// Encode to json and send request to telegram api.
curl_json_request(json_encode($reply_content));
}
/**
* Edit message text.
* @param $id_val
* @param $text_val
* @param $markup_val
* @param null $chat_id
* @param mixed $merge_args
*/
function editMessageText($id_val, $text_val, $markup_val, $chat_id = NULL, $merge_args = false)
{
// Create response array.
$response = [
'method' => 'editMessageText',
'text' => $text_val,
'parse_mode' => 'HTML',
'reply_markup' => [
'inline_keyboard' => $markup_val
]
];
if ($markup_val == false) {
unset($response['reply_markup']);
$response['remove_keyboard'] = true;
}
// Valid chat id.
if ($chat_id != null) {
$response['chat_id'] = $chat_id;
$response['message_id'] = $id_val;
} else {
$response['inline_message_id'] = $id_val;
}
// Write to log.
debug_log($merge_args, 'K');
debug_log($response, 'K');
if (is_array($merge_args) && count($merge_args)) {
$response = array_merge_recursive($response, $merge_args);
}
// Write to log.
debug_log($response, 'K');
// Encode response to json format.
$json_response = json_encode($response);
// Write to log.
debug_log($response, '<-');
// Send request to telegram api.
curl_json_request($json_response);
}
/**
* Edit message reply markup.
* @param $id_val
* @param $markup_val
* @param $chat_id
*/
function editMessageReplyMarkup($id_val, $markup_val, $chat_id)
{
// Create response array.
$response = [
'method' => 'editMessageReplyMarkup',
'reply_markup' => [
'inline_keyboard' => $markup_val
]
];
// Valid chat id.
if ($chat_id != null) {
$response['chat_id'] = $chat_id;
$response['message_id'] = $id_val;
} else {
$response['inline_message_id'] = $id_val;
}
// Encode response to json format.
$json_response = json_encode($response);
// Write to log.
debug_log($response, '->');
// Send request to telegram api.
curl_json_request($json_response);
}
/**
* Edit message keyboard.
* @param $id_val
* @param $markup_val
* @param $chat_id
*/
function edit_message_keyboard($id_val, $markup_val, $chat_id)
{
// Create response array.
$response = [
'method' => 'editMessageReplyMarkup',
'reply_markup' => [
'inline_keyboard' => $markup_val
]
];
// Valid chat id.
if ($chat_id != null) {
$response['chat_id'] = $chat_id;
$response['message_id'] = $id_val;
} else {
$response['inline_message_id'] = $id_val;
}
// Encode response to json format.
$json_response = json_encode($response);
// Write to log.
debug_log($response, '->');
// Send request to telegram api.
curl_json_request($json_response);
}
/**
* Edit message.
* @param $update
* @param $message
* @param $keys
* @param bool $merge_args
*/
function edit_message($update, $message, $keys, $merge_args = false)
{
if (isset($update['callback_query']['inline_message_id'])) {
editMessageText($update['callback_query']['inline_message_id'], $message, $keys, NULL, $merge_args);
} else {
editMessageText($update['callback_query']['message']['message_id'], $message, $keys, $update['callback_query']['message']['chat']['id'], $merge_args);
}
}
/**
* Delete message
* @param $chat_id
* @param $message_id
*/
function delete_message($chat_id, $message_id)
{
// Create response content array.
$reply_content = [
'method' => 'deleteMessage',
'chat_id' => $chat_id,
'message_id' => $message_id,
'parse_mode' => 'HTML',
];
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Send request to telegram api.
return curl_json_request($reply_json);
}
/**
* GetChat
* @param $chatid
*/
function get_chat($chat_id)
{
// Create response content array.
$reply_content = [
'method' => 'getChat',
'chat_id' => $chat_id,
'parse_mode' => 'HTML',
];
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Send request to telegram api.
return curl_json_request($reply_json);
}
/**
* GetChatAdministrators
* @param $chatid
*/
function get_admins($chat_id)
{
// Create response content array.
$reply_content = [
'method' => 'getChatAdministrators',
'chat_id' => $chat_id,
'parse_mode' => 'HTML',
];
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Send request to telegram api.
return curl_json_request($reply_json);
}
/**
* Send request to telegram api.
* @param $json
* @return mixed
*/
function curl_json_request($json)
{
$curl = curl_init('https://api.telegram.org/bot' . API_KEY . '/');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
// Use Proxyserver for curl if configured
if (CURL_USEPROXY == true) {
curl_setopt($curl, CURLOPT_PROXY, CURL_PROXYSERVER);
}
// Write to log.
debug_log($json, '->');
// Execute curl request.
$json_response = curl_exec($curl);
// Write to log.
debug_log($json_response, '<-');
// Decode json response.
$response = json_decode($json_response, true);
// Validate response.
if ($response['ok'] != true || isset($response['update_id'])) {
// Write error to log.
debug_log('ERROR: ' . $json . "\n\n" . $json_response . "\n\n");
} else {
// Result seems ok, get message_id and chat_id if supergroup or channel message
if ($response['result']['chat']['type'] == ("channel" || "supergroup")) {
// Init raid_id
$raid_id = 0;
// Set chat and message_id
$chat_id = $response['result']['chat']['id'];
$message_id = $response['result']['message_id'];
// Get raid id from $json
$json_message = json_decode($json, true);
// Check if callback_data is present to get the raid_id and reply_to_message_id is set to filter only raid messages
if (!empty($json_message['reply_markup']['inline_keyboard']['0']['0']['callback_data']) && !empty($json_message['reply_to_message_id'])) {
$split_callback_data = explode(':', $json_message['reply_markup']['inline_keyboard']['0']['0']['callback_data']);
$raid_id = $split_callback_data[0];
debug_log('Found Raid_ID for cleanup preparation from callback_data!');
debug_log('Raid_ID: ' . $raid_id);
debug_log('Chat_ID: ' . $chat_id);
debug_log('Message_ID: ' . $message_id);
// Trigger cleanup preparation process when necessary id's are not empty and numeric
if (!empty($chat_id) && !empty($message_id) && !empty($raid_id)) {
debug_log('Calling cleanup preparation now!');
insert_cleanup($chat_id, $message_id, $raid_id);
} else {
debug_log('Missing input! Cannot call cleanup preparation!');
}
}
// Check if text starts with getTranslation('raid_overview_for_chat') and inline keyboard is empty
$translation = getTranslation('raid_overview_for_chat');
$translation_length = strlen($translation);
$text = substr($response['result']['text'], 0, $translation_length);
if ($text == $translation && empty($json_message['reply_markup']['inline_keyboard'])) {
debug_log('Detected overview message!');
debug_log('Chat_ID: ' . $chat_id);
debug_log('Message_ID: ' . $message_id);
// Write raid overview data to database
debug_log('Adding overview info to database now!');
insert_overview($chat_id, $message_id);
}
}
}
// Return response.
return $response;
}
/**
* Gets a table translation out of the json file.
* @param $text
* @return translation
*/
function getTranslation($text)
{
debug_log($text);
$str = file_get_contents('./language.json');
$json = json_decode($str, true);
$translation = $json[$text][LANGUAGE];
return $translation;
}