diff --git a/logic/collectCleanup.php b/logic/collectCleanup.php index 1e6854ae..719905b6 100644 --- a/logic/collectCleanup.php +++ b/logic/collectCleanup.php @@ -21,10 +21,12 @@ function collectCleanup($response, $request, $identifier = false) // Set chat and message_id $chat_id = $response['result']['chat']['id']; $message_id = $response['result']['message_id']; + $thread_id = $response['result']['message_thread_id'] ?? null; + debug_log('Return data: Chat id: '.$chat_id.', message_id: '.$message_id.', type: '.(is_array($identifier) ? print_r($identifier,true) : $identifier)); if($identifier == 'trainer') { debug_log('Adding trainermessage info to database now!'); - insert_trainerinfo($chat_id, $message_id); + insert_trainerinfo($chat_id, $message_id, $thread_id); return $response; } if ($identifier == 'overview') { @@ -32,7 +34,7 @@ function collectCleanup($response, $request, $identifier = false) $chat_title = $response['result']['chat']['title']; $chat_username = $response['result']['chat']['username'] ?? ''; - insert_overview($chat_id, $message_id, $chat_title, $chat_username); + insert_overview($chat_id, $message_id, $thread_id, $chat_title, $chat_username); return $response; } @@ -73,7 +75,7 @@ function collectCleanup($response, $request, $identifier = false) ); } $raid_id = is_array($identifier) ? $identifier['id'] : $identifier; - insert_cleanup($chat_id, $message_id, $raid_id, $type, $unique_id); + insert_cleanup($chat_id, $message_id, $thread_id, $raid_id, $type, $unique_id); // Return response. return $response; } diff --git a/logic/config_chats.php b/logic/config_chats.php new file mode 100644 index 00000000..820a1ddf --- /dev/null +++ b/logic/config_chats.php @@ -0,0 +1,77 @@ +CHATS_SHARE)) { + $chat_vars = ['TRAINER_CHATS','SHARE_CHATS','WEBHOOK_CHATS']; + $chatsTemp = []; + foreach(array_keys($config) as $var) { + foreach($chat_vars as $start) { + if(!strpos($var, $start)) continue; + if(is_string($config->{$var})) { + array_merge($chatsTemp, explode(',', $config->{$var})); + continue; + } + array_merge($chatsTemp, $config->{$var}); + } + } + $chats = []; + foreach($chatsTemp as $chat) { + $chats[] = create_chat_object($chat); + } + }else { + $chats = []; + if(isset($config->CHATS_SHARE['manual_share'])) { + foreach($config->CHATS_SHARE['manual_share'] as $chatGroup) { + foreach($chatGroup as $chat) { + $chats = add_chat($chats, $chat); + } + } + } + if(isset($config->CHATS_SHARE['after_attendance'])) { + foreach($config->CHATS_SHARE['after_attendance'] as $chat) { + $chats = add_chat($chats, $chat); + } + } + if(isset($config->CHATS_SHARE['webhook'])) { + if(isset($config->CHATS_SHARE['webhook']['all'])) { + foreach($config->CHATS_SHARE['webhook']['all'] as $chat) { + $chats = add_chat($chats, $chat); + } + } + if(isset($config->CHATS_SHARE['webhook']['by_pokemon'])) { + foreach($config->CHATS_SHARE['webhook']['by_pokemon'] as $chatGroup) { + foreach($chatGroup['chats'] as $chat) { + $chats = add_chat($chats, $chat); + } + } + } + if(isset($config->CHATS_SHARE['webhook']['geofences'])) { + foreach($config->CHATS_SHARE['webhook']['geofences'] as $geofence) { + foreach($geofence as $chatGroup) { + foreach($chatGroup as $chat) { + $chats = add_chat($chats, $chat); + } + } + } + } + } + } + return $chats; +} + +function add_chat($chats, $chatToAdd) { + foreach($chats as $chat) { + if( + $chat['id'] == $chatToAdd['id'] && !isset($chat['thread']) || + $chat['id'] == $chatToAdd['id'] && isset($chat['thread']) && isset($chatToAdd['thread']) && $chat['thread'] == $chatToAdd['thread'] + ) return $chats; + } + $chats[] = $chatToAdd; + return $chats; +} + +function get_config_chat_by_short_id($id) { + $chats = list_config_chats_by_short_id(); + return $chats[$id]; +} diff --git a/logic/insert_cleanup.php b/logic/insert_cleanup.php index 4835cb74..6d3f04ef 100644 --- a/logic/insert_cleanup.php +++ b/logic/insert_cleanup.php @@ -1,18 +1,20 @@ $raid_id, ':chat_id' => $chat_id, ':message_id' => $message_id, + ':thread_id' => $thread_id, ':type' => $type, ':media_unique_id' => $photo_id, ] diff --git a/logic/insert_overview.php b/logic/insert_overview.php index dedbcfc8..60a6b553 100644 --- a/logic/insert_overview.php +++ b/logic/insert_overview.php @@ -1,12 +1,13 @@ $chat_id, 'message_id' => $message_id, + 'thread_id' => $thread_id, 'chat_title' => $chat_title, 'chat_username' => $chat_username, ] diff --git a/logic/insert_trainerinfo.php b/logic/insert_trainerinfo.php index f485d682..351a4dd6 100644 --- a/logic/insert_trainerinfo.php +++ b/logic/insert_trainerinfo.php @@ -1,10 +1,11 @@ RAID_PICTURE_HIDE_LEVEL); @@ -120,8 +121,8 @@ function update_raid_poll($raid_id, $raid = false, $update = false, $tg_json = f $media_content = get_raid_picture($raid, true); $raid['standalone_photo'] = true; // Inject this into raid array so we can pass it all the way to photo cache // Resend raid poll as text message. - send_photo($chat, $media_content[1], $media_content[0], '', [], [], false, $raid); - send_message($chat, $text['full'], $keys, ['disable_web_page_preview' => 'true'], false, $raid_id); + send_photo(create_chat_object([$chat, $thread_id]), $media_content[1], $media_content[0], '', [], [], false, $raid); + send_message(create_chat_object([$chat, $thread_id]), $text['full'], $keys, ['disable_web_page_preview' => 'true'], false, $raid_id); continue; } $media_content = get_raid_picture($raid); diff --git a/mods/refresh_polls.php b/mods/refresh_polls.php index c0179c46..acac4e23 100644 --- a/mods/refresh_polls.php +++ b/mods/refresh_polls.php @@ -8,7 +8,7 @@ if(strlen($data['id']) > 5) $where_chat = 'chat_id = '.$data['id']; else $where_chat = 'chat_id != 0'; if(!empty($config->RAID_POLL_HIDE_BUTTONS_RAID_LEVEL)) $level_exclude = 'AND raids.level NOT IN ('.$config->RAID_POLL_HIDE_BUTTONS_RAID_LEVEL.')'; else $level_exclude = ''; $query_messages = my_query(' - SELECT cleanup.raid_id, cleanup.chat_id, cleanup.message_id, cleanup.type, cleanup.media_unique_id + SELECT cleanup.raid_id, cleanup.chat_id, cleanup.thread_id, cleanup.message_id, cleanup.type, cleanup.media_unique_id FROM cleanup LEFT JOIN raids ON cleanup.raid_id = raids.id diff --git a/mods/trainer_add.php b/mods/trainer_add.php index d335bc01..d83ac3f0 100644 --- a/mods/trainer_add.php +++ b/mods/trainer_add.php @@ -1,6 +1,7 @@ TRAINER_CHATS ? -if(!empty($config->TRAINER_CHATS)) { - $chat_list = $config->TRAINER_CHATS; - debug_log($chat_list, 'Added trainer chats to the chat list:'); -} - -// $config->SHARE_CHATS ? -if(!empty($config->SHARE_CHATS) && !empty($chat_list)) { - $chat_list .= ',' . $config->SHARE_CHATS; - debug_log($chat_list, 'Added share chats to the chat list:'); -} else if(!empty($config->SHARE_CHATS) && empty($chat_list)) { - $chat_list = $config->SHARE_CHATS; - debug_log($chat_list, 'Added share chats to the chat list:'); -} - -// Get chats from config and add to keys. -for($i = 1; $i <= 10; $i++) { - // Raid level adjustment - if($i == 10) { - $raid_level = 'X'; - } else { - $raid_level = $i; - } - $const = 'SHARE_CHATS_LEVEL_' . $raid_level; - $const_chats = $config->{$const}; - - // Sharing keys for this raid level? - if(!empty($const_chats)) { - debug_log('Found chats by level, adding them'); - // Add chats. - if(!empty($chat_list)) { - $chat_list .= ',' . $const_chats; - debug_log($chat_list, 'Added ' . $const . ' chats to the chat list:'); - } else { - $chat_list = $const_chats; - debug_log($chat_list, 'Added ' . $const . ' chats to the chat list:'); - } - } -} - -// Delete duplicate chats. -debug_log($chat_list, 'Searching and removing duplicates from chat list:'); -$chat_list = explode(',', $chat_list); -$chats = array_unique($chat_list); +$chats = list_config_chats_by_short_id(); // Get chats already in the database. debug_log('Searching and removing chats already having the trainer message'); $rs = my_query(' - SELECT chat_id + SELECT chat_id, thread_id FROM trainerinfo '); -$chats_db = []; -while ($row = $rs->fetch()) { - $chats_db[] = $row['chat_id']; +$chats_db = $rs->fetchAll(); +for($i=0;$i $chat) { + // Get chat object + debug_log("Getting chat object for '" . $chat['id'] . "'"); + $chat_obj = get_chat($chat['id']); + + // Check chat object for proper response. + if ($chat_obj['ok'] != true) { + info_log($chat, 'Invalid chat id in your configuration:'); + continue; + } + $chatTitle = $chat['title'] ?? $chat_obj['result']['title']; + debug_log('Proper chat object received, continuing to add key for this chat: ' . $chatTitle); + $shareData = [0 => 'trainer_share', 'c' => $chatShortId]; + $keys[][] = button(getTranslation('share_with') . ' ' . $chatTitle, $shareData); } // Add abort key. diff --git a/mods/trainer_share.php b/mods/trainer_share.php index 0f4d3156..8433890d 100644 --- a/mods/trainer_share.php +++ b/mods/trainer_share.php @@ -3,6 +3,7 @@ debug_log('trainer_share()'); require_once(LOGIC_PATH . '/keys_trainerinfo.php'); require_once(LOGIC_PATH . '/show_trainerinfo.php'); +require_once(LOGIC_PATH . '/config_chats.php'); // For debug. //debug_log($update); @@ -11,8 +12,8 @@ // Access check. $botUser->accessCheck('trainer-share'); -// Get chat id. -$chat = $data['c']; +// Get chat object by chat short id +$chat = get_config_chat_by_short_id($data['c']); // Get text and keys. $text = show_trainerinfo($update); diff --git a/sql/pokemon-raid-bot.sql b/sql/pokemon-raid-bot.sql index 69cc460c..ee7ad515 100644 --- a/sql/pokemon-raid-bot.sql +++ b/sql/pokemon-raid-bot.sql @@ -23,6 +23,7 @@ CREATE TABLE `cleanup` ( `raid_id` int(10) unsigned NOT NULL, `chat_id` bigint(20) NOT NULL, `message_id` bigint(20) unsigned NOT NULL, + `thread_id` int(10) UNSIGNED NULL, `type` VARCHAR(20) NULL, `date_of_posting` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `media_unique_id` varchar(45) DEFAULT NULL, @@ -61,6 +62,7 @@ CREATE TABLE `overview` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `chat_id` bigint(20) NOT NULL, `message_id` bigint(20) unsigned NOT NULL, + `thread_id` int(10) UNSIGNED NULL, `chat_title` varchar(128) DEFAULT NULL, `chat_username` varchar(32) DEFAULT NULL, `updated` date DEFAULT NULL, @@ -131,6 +133,7 @@ CREATE TABLE `trainerinfo` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `chat_id` bigint(20) NOT NULL, `message_id` bigint(20) unsigned NOT NULL, + `thread_id` int(10) UNSIGNED NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE `user_input` ( diff --git a/sql/upgrade/5.sql b/sql/upgrade/5.sql index 5e8e717e..3aa0ab5a 100644 --- a/sql/upgrade/5.sql +++ b/sql/upgrade/5.sql @@ -7,8 +7,13 @@ ALTER TABLE `photo_cache` ADD COLUMN IF NOT EXISTS `end_time` DATETIME DEFAULT N ALTER TABLE `photo_cache` ADD COLUMN IF NOT EXISTS `start_time` DATETIME DEFAULT NULL AFTER `end_time`; ALTER TABLE `cleanup` ADD COLUMN IF NOT EXISTS `media_unique_id` varchar(45) DEFAULT NULL AFTER `date_of_posting`; +ALTER TABLE `cleanup` ADD COLUMN `thread_id` INT UNSIGNED NULL AFTER `message_id`; CREATE UNIQUE INDEX IF NOT EXISTS `unique_chat_msg` ON `cleanup` (chat_id, message_id); +ALTER TABLE `overview` ADD COLUMN `thread_id` INT UNSIGNED NULL AFTER `message_id`; + +ALTER TABLE `trainerinfo` ADD COLUMN `thread_id` INT UNSIGNED NULL AFTER `message_id`; + ALTER TABLE `raids` CHANGE COLUMN IF EXISTS `level` `level` TINYINT UNSIGNED DEFAULT NULL; ALTER TABLE `raid_bosses` CHANGE COLUMN IF EXISTS `raid_level` `raid_level` TINYINT UNSIGNED DEFAULT NULL;