Skip to content

Commit e017eff

Browse files
committed
Merge branch 'share_chats_refactor' into dev
2 parents f9e6ff6 + b826f64 commit e017eff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+531
-325
lines changed

commands/delete.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
if($query->rowCount() == 0) {
3131
$msg = '<b>' . getTranslation('no_active_raids_found') . '</b>';
32-
send_message($update['message']['chat']['id'], $msg);
32+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);
3333
exit;
3434
}
3535

@@ -66,4 +66,4 @@
6666
$callback_response = 'OK';
6767

6868
// Send message.
69-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
69+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/events.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
$keys[][] = button(getTranslation('done'), ['exit', 'd' => '1']);
2626

2727
// Send message.
28-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
28+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/exreport.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@
8585
$msg = '<b>EX Raid Report</b>';
8686

8787
// Send message.
88-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
88+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/friendsearch.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
}else {
2424
$msg = $searchterm.CR. getTranslation('trainer_not_found');
2525
}
26-
send_message($update['message']['chat']['id'], $msg, [], ['reply_markup' => ['selective' => true]]);
26+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, [], ['reply_markup' => ['selective' => true]]);

commands/get.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
// Any configs allowed?
4343
if(empty($allowed)) {
44-
send_message($update['message']['chat']['id'], getTranslation('not_supported'));
44+
send_message(create_chat_object([$update['message']['chat']['id']]), getTranslation('not_supported'));
4545
exit;
4646
}
4747
foreach($json as $cfg_name => $cfg_value) {
@@ -69,4 +69,4 @@
6969
$msg .= CR;
7070
}
7171
}
72-
send_message($update['message']['chat']['id'], $msg);
72+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);

commands/gym.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
$msg.= $keys_and_gymarea['gymareaTitle'];
2121

2222
// Send message.
23-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);
23+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);

commands/gymname.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
}
5151

5252
// Send message.
53-
send_message($update['message']['chat']['id'], $msg, [], ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);
53+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, [], ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);

commands/help.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
}
5959
$keys[][] = button(getTranslation('next'), 'tutorial');
6060
$photo = $tutorial[0]['photo'];
61-
send_photo($update['message']['from']['id'],$photo, false, $msg, $keys, ['disable_web_page_preview' => 'true']);
61+
send_photo(create_chat_object([$update['message']['from']['id']]),$photo, false, $msg, $keys, ['disable_web_page_preview' => 'true']);
6262
exit();
6363

6464
// No help for the user.
@@ -67,4 +67,4 @@
6767
}
6868

6969
// Send message.
70-
send_message($update['message']['from']['id'], $msg);
70+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);

commands/history.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
$keys = $msg_keys[1];
2121
}
2222

23-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);
23+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);

commands/list.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
// Did we get any raids?
4646
if(count($raids) == 0) {
4747
$msg = '<b>' . getTranslation('no_active_raids_found') . '</b>';
48-
send_message($update['message']['chat']['id'], $msg, [], ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
48+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, [], ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
4949
exit();
5050
}
5151

@@ -98,4 +98,4 @@
9898
$msg .= '<b>' . getTranslation('select_gym_name') . '</b>' . CR;
9999

100100
// Send message.
101-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
101+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/listall.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
$msg.= $keys_and_gymarea['gymareaTitle'];
2121

2222
// Send message.
23-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);
23+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);

commands/overview.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
// Create keys array.
1313
$keys[][] = button(getTranslation('overview_share'), 'overview_share');
1414
$keys[][] = button(getTranslation('overview_delete'), 'overview_delete');
15+
$keys[][] = button(getTranslation('abort'), ['exit', 'd' => '0']);
1516

1617
// Set message.
1718
$msg = '<b>' . getTranslation('raids_share_overview') . ':</b>';
1819

1920
// Send message.
20-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
21+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/pokedex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
$keys[][] = button(getTranslation('abort'), 'exit');
5151

5252
// Send message.
53-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
53+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/pokemon.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
if($query->rowCount() == 0) {
3131
$msg = '<b>' . getTranslation('no_active_raids_found') . '</b>';
32-
send_message($update['message']['chat']['id'], $msg);
32+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);
3333
exit;
3434
}
3535

@@ -72,4 +72,4 @@
7272
$msg .= '<b>' . getTranslation('select_gym_name') . '</b>' . CR;
7373

7474
// Send message.
75-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
75+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/raid_from_webhook.php

+76-29
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function isPointInsidePolygon($point, $vertices) {
4949
$webhook_raids_received_total->incBy(count($update));
5050
}
5151
foreach($update as $raid) {
52+
if($raid['type'] != 'raid') continue;
5253
// Skip posting if create only -mode is set or raid time is greater than value set in config
5354
$no_auto_posting = ($config->WEBHOOK_CREATE_ONLY or ($raid['message']['end']-$raid['message']['start']) > ($config->WEBHOOK_EXCLUDE_AUTOSHARE_DURATION * 60));
5455

@@ -311,61 +312,107 @@ function isPointInsidePolygon($point, $vertices) {
311312
'shadow' => (in_array($level, RAID_LEVEL_SHADOW) ? 1 : 0),
312313
]);
313314

314-
$chats_geofence = $chats_raidlevel = $webhook_chats = $chats_by_pokemon = [];
315+
$chats_geofence = $chats_raidlevel = $webhook_chats = $chats_by_pokemon = $chats = [];
315316
if($send_updates == true) {
317+
// Update raid polls and send alerts of updates
316318
require_once(LOGIC_PATH .'/update_raid_poll.php');
317319
$tg_json = update_raid_poll($raid_id, $raid, false, $tg_json, true);
318320
if($wasBossUpdated) $tg_json = alarm($raid, false, 'new_boss', '', $tg_json);
319-
if(!empty($config->WEBHOOK_CHATS_BY_POKEMON[0]) && !$no_auto_posting) {
320-
foreach($config->WEBHOOK_CHATS_BY_POKEMON as $rule) {
321-
if(isset($rule['pokemon_id']) && $rule['pokemon_id'] == $pokemon && (!isset($rule['form_id']) or (isset($rule['form_id']) && $rule['form_id'] == $form))) {
322-
foreach($rule['chats'] as $rule_chat) {
323-
// If the raid isn't already posted to the chats specified in WEBHOOK_CHATS_BY_POKEMON, we add it to the array
324-
if(!isset($cleanup_data[$raid_id]) or !in_array($rule_chat, $cleanup_data[$raid_id])) {
325-
$chats_by_pokemon[] = $rule_chat;
321+
// Post hatched Pokemon to their respective chats if configured
322+
// Start share_chats backwards compatibility
323+
if(!isset($config->CHATS_SHARE)) {
324+
if(!empty($config->WEBHOOK_CHATS_BY_POKEMON[0]) && !$no_auto_posting) {
325+
foreach($config->WEBHOOK_CHATS_BY_POKEMON as $rule) {
326+
if(isset($rule['pokemon_id']) && $rule['pokemon_id'] == $pokemon && (!isset($rule['form_id']) or (isset($rule['form_id']) && $rule['form_id'] == $form))) {
327+
foreach($rule['chats'] as $rule_chat) {
328+
// If the raid isn't already posted to the chats specified in WEBHOOK_CHATS_BY_POKEMON, we add it to the array
329+
if(!isset($cleanup_data[$raid_id]) or !in_array($rule_chat, $cleanup_data[$raid_id])) {
330+
$chats_by_pokemon[] = create_chat_object([$rule_chat]);
331+
}
332+
}
333+
}
334+
}
335+
}
336+
// End chats_share backwards compatibility
337+
}else {
338+
if(!empty($config->CHATS_SHARE['webhook']['by_pokemon']) && !$no_auto_posting) {
339+
foreach($config->CHATS_SHARE['webhook']['by_pokemon'] as $rule) {
340+
if(isset($rule['pokemon_id']) && $rule['pokemon_id'] == $pokemon && (!isset($rule['form_id']) or (isset($rule['form_id']) && $rule['FORM_ID'] == $form))) {
341+
foreach($rule['chats'] as $rule_chat) {
342+
if(!isset($cleanup_data[$raid_id]) or !in_array($rule_chat['id'], $cleanup_data[$raid_id])) {
343+
$chats_by_pokemon[] = $rule_chat;
344+
}
326345
}
327346
}
328347
}
329348
}
330349
}
350+
331351
if(empty($chats_by_pokemon)) continue;
332352
}else {
333-
// Get chats to share to by raid level and geofence id
334-
if($geofences != false) {
335-
foreach($inside_geofences as $geofence_id) {
336-
$const_geofence = 'WEBHOOK_CHATS_LEVEL_' . $level . '_' . $geofence_id;
337-
$const_geofence_chats = $config->{$const_geofence} ?? [];
338-
339-
if(!empty($const_geofence_chats)) {
340-
$chats_geofence = explode(',', $const_geofence_chats);
353+
// Start share_chats backwards compatibility
354+
if(!isset($config->CHATS_SHARE)) {
355+
// Get chats to share to by raid level and geofence id
356+
if($geofences != false) {
357+
foreach($inside_geofences as $geofence_id) {
358+
$const_geofence = 'WEBHOOK_CHATS_LEVEL_' . $level . '_' . $geofence_id;
359+
$const_geofence_chats = $config->{$const_geofence} ?? [];
360+
361+
if(!empty($const_geofence_chats)) {
362+
$chats_geofence = explode(',', $const_geofence_chats);
363+
}
341364
}
342365
}
343-
}
344366

345-
// Get chats to share to by raid level
346-
$const = 'WEBHOOK_CHATS_LEVEL_' . $level;
347-
$const_chats = $config->{$const} ?? [];
367+
// Get chats to share to by raid level
368+
$const = 'WEBHOOK_CHATS_LEVEL_' . $level;
369+
$const_chats = $config->{$const} ?? [];
348370

349-
if(!empty($const_chats)) {
350-
$chats_raidlevel = explode(',', $const_chats);
351-
}
371+
if(!empty($const_chats)) {
372+
$chats_raidlevel = explode(',', $const_chats);
373+
}
352374

353-
// Get chats
354-
if(!empty($config->WEBHOOK_CHATS_ALL_LEVELS)) {
355-
$webhook_chats = explode(',', $config->WEBHOOK_CHATS_ALL_LEVELS);
375+
// Get chats
376+
if(!empty($config->WEBHOOK_CHATS_ALL_LEVELS)) {
377+
$webhook_chats = explode(',', $config->WEBHOOK_CHATS_ALL_LEVELS);
378+
}
379+
$chats_combined = array_merge($chats_geofence, $chats_raidlevel, $webhook_chats);
380+
foreach($chats_combined as $chat) {
381+
$chats[] = create_chat_object([$chat]);
382+
}
383+
// End chats_share backwards compatibility
384+
}else {
385+
if($geofences != false) {
386+
foreach($inside_geofences as $geofence_id) {
387+
$geofence_chats_all = $config->CHATS_SHARE['webhook']['geofences'][$geofence_id]['all'] ?? [];
388+
$geofence_chats_by_level = $config->CHATS_SHARE['webhook']['geofences'][$geofence_id][$level] ?? [];
389+
390+
if(!empty($geofence_chats_all)) {
391+
$chats_geofence = array_merge($chats_geofence, $geofence_chats_all);
392+
}
393+
if(!empty($geofence_chats_by_level)) {
394+
$chats_geofence = array_merge($chats_geofence, $geofence_chats_by_level);
395+
}
396+
}
397+
}
398+
// Get chats to share to by raid level
399+
$chats_raidlevel = $config->CHATS_SHARE['webhook'][$level] ?? [];
400+
401+
// Get chats
402+
$webhook_chats = $config->CHATS_SHARE['webhook']['all'] ?? [];
403+
$chats = array_merge($chats_geofence, $chats_raidlevel, $webhook_chats);
356404
}
357405
}
358406

359-
$chats = array_merge($chats_geofence, $chats_raidlevel, $webhook_chats, $chats_by_pokemon);
360-
361407
require_once(LOGIC_PATH .'/send_raid_poll.php');
362408
if($metrics) {
363409
$webhook_raids_posted_total->inc();
364410
}
365411
if(count($chats) > 0) {
366412
$tg_json = send_raid_poll($raid_id, $chats, $raid, $tg_json);
413+
}elseif(count($chats_by_pokemon) > 0) {
414+
$tg_json = send_raid_poll($raid_id, $chats_by_pokemon, $raid, $tg_json);
367415
}
368416
}
369-
370417
// Telegram multicurl request.
371418
curl_json_multi_request($tg_json);

commands/set.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@
177177
}
178178

179179
// Send message.
180-
send_message($update['message']['chat']['id'], $msg);
180+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);

commands/start.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require('list.php');
1717
}else {
1818
$response_msg = '<b>' . getTranslation('bot_access_denied') . '</b>';
19-
send_message($update['message']['from']['id'], $response_msg);
19+
send_message(create_chat_object([$update['message']['chat']['id']]), $response_msg);
2020
}
2121
exit;
2222
}
@@ -60,4 +60,4 @@
6060
}
6161

6262
// Send message.
63-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
63+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/trainer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@
6767
$keys[] = $nav_keys;
6868

6969
// Send message.
70-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
70+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/tutorial.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
$msg = ($new_user) ? $tutorial[0]['msg_new'] : $tutorial[0]['msg'];
1919
$keys[][] = button(getTranslation('next'), ['tutorial', 'p' => 1]);
2020
$photo = $tutorial[0]['photo'];
21-
send_photo($update['message']['from']['id'], $photo, false, $msg, $keys, ['disable_web_page_preview' => 'true'],false);
21+
send_photo(create_chat_object([$update['message']['from']['id']]), $photo, false, $msg, $keys, ['disable_web_page_preview' => 'true'],false);

core/bot/cleanup_collect.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,30 @@
1818
// Get chat_id and message_id
1919
$chat_id = $message['chat']['id'];
2020
$message_id = $message['message_id'];
21+
$thread_id = $message['message_thread_id'] ?? NULL;
2122
if(isset($message['reply_markup']['inline_keyboard'])) {
22-
$split_data = explode(':', $message['reply_markup']['inline_keyboard'][0][0]['callback_data']);
23-
$cleanup_id = $split_data[0];
23+
$splitData = explode('|', $message['reply_markup']['inline_keyboard'][0][0]['callback_data']);
24+
// Search for raid id in the first button of the message
25+
for($i=1;$i<count($splitData);$i++) {
26+
$splitVariable = explode('=', $splitData[$i], 2);
27+
if(count($splitVariable) == 2 && $splitVariable[0] == 'r' && preg_match("^[0-9]+^", $splitVariable[1])) {
28+
$cleanup_id = $splitVariable[1];
29+
break;
30+
}
31+
}
2432
}else {
2533
// Get id from text.
26-
$cleanup_id = substr($message['text'],strpos($message['text'], substr(strtoupper($config->BOT_ID), 0, 1) . '-ID = ') + 7);
34+
$idFromText = substr($message['text'],strpos($message['text'], substr(strtoupper($config->BOT_ID), 0, 1) . '-ID = ') + 7);
35+
if(preg_match("^[0-9]+^", $idFromText)) {
36+
$cleanup_id = $idFromText;
37+
}
2738
}
2839

2940
// Write cleanup info to database.
30-
cleanup_log('Calling cleanup preparation now!');
31-
cleanup_log('Cleanup_ID: ' . $cleanup_id);
3241
if($cleanup_id != 0) {
42+
cleanup_log('Calling cleanup preparation now!');
43+
cleanup_log('Cleanup_ID: ' . $cleanup_id);
3344
require_once(LOGIC_PATH . '/insert_cleanup.php');
34-
insert_cleanup($chat_id, $message_id, $cleanup_id, 'inline_poll_text');
45+
insert_cleanup($chat_id, $message_id, $thread_id, $cleanup_id, 'inline_poll_text');
3546
}
3647
}

core/bot/commands.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414

1515
if(isset($update['message']['chat']['id']) && new_user($update['message']['chat']['id']) && $com != 'start' && $com != 'tutorial') {
16-
send_message($update['message']['chat']['id'], getTranslation("tutorial_command_failed"));
16+
send_message(create_chat_object([$update['message']['chat']['id']]), getTranslation("tutorial_command_failed"));
1717
exit();
1818
}
1919

@@ -33,6 +33,6 @@
3333
// Include start file and exit.
3434
include_once($startcommand);
3535
} else {
36-
send_message($update['message']['chat']['id'], '<b>' . getTranslation('not_supported') . '</b>');
36+
send_message(create_chat_object([$update['message']['chat']['id']]), '<b>' . getTranslation('not_supported') . '</b>');
3737
}
3838
}

core/bot/importal.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Invalid input or unknown bot - send message and end.
66
$msg = '<b>' . getTranslation('invalid_input') . '</b>';
77
$msg .= CR . CR . getTranslation('not_supported') . SP . getTranslation('or') . SP . getTranslation('internal_error');
8-
send_message($update['message']['from']['id'], $msg);
8+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);
99
exit();
1010
}
1111

core/bot/user.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function denyAccess() {
176176
$response_msg = '<b>' . getTranslation('bot_access_denied') . '</b>';
177177
// Edit message or send new message based on type of received call
178178
if ($update['type'] != 'callback_query') {
179-
send_message($update['message']['from']['id'], $response_msg);
179+
send_message(create_chat_object([$update['message']['chat']['id']]), $response_msg);
180180
exit;
181181
}
182182
$keys = [];

0 commit comments

Comments
 (0)