Skip to content

Commit

Permalink
Cleanup collection rewrite
Browse files Browse the repository at this point in the history
- Refactored the way cleanup info is collected
- Created new functions send_raid_poll() and update_raid_poll() for posting and updating raid polls
- Added the config value `RAID_PICTURE_AUTOEXTEND`
- Removed some unused functions from core/telegram/functions.php
- Lots of minor code cleanups
  • Loading branch information
Ninjasoturi committed Aug 18, 2021
1 parent bafdfa2 commit 0b4b3e3
Show file tree
Hide file tree
Showing 53 changed files with 550 additions and 925 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ Set `RAID_LATE_TIME` to the amount of minutes the local community will may be wa

Set `RAID_POLL_HIDE_USERS_TIME` to the amount of minutes when a previous raid slot should be hidden. For example if there are 2 slots, 18:00 and 18:15, and you set the time to 10 minutes the first group of participants from 18:00 will be hidden once we reach 18:10. This helps to keep the raid poll message smaller and clearer if there are multiple groups. Set the value to 0 to always show all slots.

Set `RAID_POLL_UI_TEMPLATE` to the customize the order of the buttons for the raid polls. Supported elementa are `alone, extra, extra_alien, remote, inv_plz, can_inv, ex_inv, teamlvl, time, pokemon, refresh, alarm, here, late, done, cancel`. Some elements may be hidden by some other config values even if they are set in the template.
Edit `RAID_POLL_UI_TEMPLATE` to customize the order of the buttons for the raid polls. Supported elementa are `alone, extra, extra_alien, remote, inv_plz, can_inv, ex_inv, teamlvl, time, pokemon, refresh, alarm, here, late, done, cancel`. Some elements may be hidden by some other config values even if they are set in the template.

Set `RAID_POLL_HIDE_BUTTONS_RAID_LEVEL` to the raid levels (1-5) for which the voting buttons under the raid poll should be hidden. For example a level 1 raid can be done by a single player, but it is maybe interesting to be shared as some pokemon are only available in raids.

Expand Down Expand Up @@ -1171,6 +1171,7 @@ To keep local data, such as `pokemon` table and Pokemon icons directory, up to d
| RAID_LATE_TIME| How many minutes to advise waiting in `RAID_LATE_MSG` |
| RAID_LOCATION| Bool, Send a separate attached location message in addition to a raid poll |
| RAID_PICTURE | Bool, enable picture based raid polls instead of default text mode |
| RAID_PICTURE_AUTOEXTEND | Bool, send the picture and poll as separate messages |
| RAID_PICTURE_STORE_GYM_IMAGES_LOCALLY | Bool, option to store gym photos in `images/gyms` instead of fetching them from cloud every time |
| RAID_PICTURE_BG_COLOR| List of RGB values for `RAID_PICTURE` poll background color, e.g. "0,0,0" for black |
| RAID_PICTURE_FILE_FORMAT| Format for raid pictures for `RAID_PICTURE`, valid values are gif, jpg, jpeg, png |
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.230.1
2.1.230.2
2 changes: 1 addition & 1 deletion commands/addgym.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$msg = '<b>' . getTranslation('invalid_input') . '</b>' . CR . CR;
$msg .= getTranslation('gym_coordinates_format_error') . CR;
$msg .= getTranslation('gym_coordinates_format_example');
sendMessage($update['message']['chat']['id'], $msg);
send_message($update['message']['chat']['id'], $msg);
exit();
}

Expand Down
2 changes: 1 addition & 1 deletion commands/gymgps.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
$msg = '<b>' . getTranslation('invalid_input') . '</b>' . CR . CR;
$msg .= getTranslation('gym_gps_coordinates_format_error') . CR;
$msg .= getTranslation('gym_gps_example');
sendMessage($update['message']['chat']['id'], $msg);
send_message($update['message']['chat']['id'], $msg);
exit();
}

Expand Down
2 changes: 1 addition & 1 deletion commands/help.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
}

// Send message.
sendMessage($update['message']['from']['id'], $msg);
send_message($update['message']['from']['id'], $msg);

?>

52 changes: 5 additions & 47 deletions commands/raid_from_webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,32 +237,10 @@ function isPointInsidePolygon($point, $vertices) {
}
}

// Get raid data.
$raid = get_raid($raid_id);

// Set text.
$text = show_raid_poll($raid);

// Set keys.
$keys = keys_vote($raid);

if($send_updates == true) {
$cleanup_query = '
SELECT *
FROM cleanup
WHERE raid_id = :id
';
$cleanup_statement = $dbh->prepare( $cleanup_query );
$cleanup_statement->execute(['id' => $raid_id]);
while ($row = $cleanup_statement->fetch()) {
if($config->RAID_PICTURE) {
require_once(LOGIC_PATH . '/raid_picture.php');
$picture_url = raid_picture_url($raid);
$tg_json[] = editMessageMedia($row['message_id'], $text['short'], $keys, $row['chat_id'], ['disable_web_page_preview' => 'true'],true, $picture_url);
}else {
$tg_json[] = editMessageText($row['message_id'], $text['full'], $keys, $row['chat_id'], ['disable_web_page_preview' => 'true'],true);
}
}
require_once(LOGIC_PATH .'/update_raid_poll.php');
$update = update_raid_poll($raid_id, false, false, $tg_json); // update_raid_poll() will return false if the raid isn't shared to any chat
if($update != false) $tg_json = $update;
}else {
// Get chats to share to by raid level and geofence id
$chats_geofence = [];
Expand Down Expand Up @@ -293,28 +271,8 @@ function isPointInsidePolygon($point, $vertices) {
}
$chats = array_merge($chats_geofence, $chats_raidlevel, $webhook_chats);

// Post raid polls.
foreach ($chats as $chat) {
debug_log('Posting poll to chat: ' . $chat);

// Send location.
if ($config->RAID_LOCATION) {

$msg_text = !empty($raid['address']) ? $raid['address'] . ', ' . substr(strtoupper($config->BOT_ID), 0, 1) . '-ID = ' . $raid['id'] : $raid['pokemon'] . ', ' . substr(strtoupper($config->BOT_ID), 0, 1) . '-ID = ' . $raid['id']; // DO NOT REMOVE ' ID = ' --> NEEDED FOR $config->CLEANUP PREPARATION!
$loc = send_venue($chat, $raid['lat'], $raid['lon'], '', $msg_text, true);
$tg_json[] = $loc;
debug_log($loc, 'Location:');
}

// Send the message.
if($config->RAID_PICTURE) {
require_once(LOGIC_PATH . '/raid_picture.php');
$picture_url = raid_picture_url($raid);
$tg_json[] = send_photo($chat, $picture_url, $text['short'], $keys, ['disable_web_page_preview' => 'true'], true);
} else {
$tg_json[] = send_message($chat, $text['full'], $keys, ['disable_web_page_preview' => 'true'], true);
}
}
require_once(LOGIC_PATH .'/send_raid_poll.php');
$tg_json = send_raid_poll($raid_id, $chats, $tg_json);
}
}

Expand Down
4 changes: 2 additions & 2 deletions commands/team.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
);

// Send the message.
sendMessage($update['message']['chat']['id'], getTranslation('gym_team_set_to') . ' ' . ucfirst($teams[$gym_team]));
send_message($update['message']['chat']['id'], getTranslation('gym_team_set_to') . ' ' . ucfirst($teams[$gym_team]));

// Invalid team name.
} else {
// Send the message.
sendMessage($update['message']['chat']['id'], getTranslation('invalid_team'));
send_message($update['message']['chat']['id'], getTranslation('invalid_team'));
}

?>
2 changes: 1 addition & 1 deletion config/config.json.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"VERSION":"2.1.230.1",
"VERSION":"2.1.230.2",
"DB_HOST":"localhost",
"DB_NAME":"your_database_name",
"DB_USER":"your_database_user",
Expand Down
7 changes: 4 additions & 3 deletions config/defaults-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"BOT_ID":"R",
"BOT_NAME":"",
"BOT_ADMINS":"",
"LANGUAGE_PRIVATE":"EN",
"LANGUAGE_PRIVATE":"",
"LANGUAGE_PUBLIC":"EN",
"TIMEZONE":"UTC",
"MAPS_LOOKUP": false,
Expand Down Expand Up @@ -52,9 +52,10 @@
"RAID_WANT_INVITE": true,
"RAID_CODE_POKEMON":"1,7,4,25,12,143,19,16,52",
"RAID_PICTURE": false,
"RAID_PICTURE_AUTOEXTEND": false,
"RAID_AUTOMATIC_ALARM": false,
"SHOW_GYM_NAME_IN_ADDRESS": false,
"RAID_PICTURE_HIDE_LEVEL":"X",
"RAID_PICTURE_HIDE_LEVEL":"",
"RAID_PICTURE_HIDE_POKEMON":"",
"RAID_PICTURE_BG_COLOR":"0,0,0",
"RAID_PICTURE_TEXT_COLOR":"255,255,255",
Expand All @@ -70,7 +71,7 @@
"RAID_PICTURE_POKEMON_ICONS":"pokemon,PokeMiners,ZeChrales",
"RAID_PICTURE_POKEMON_FALLBACK":"images/shadow_pika.png",
"RAID_PICTURE_POKEMON_TYPES": true,
"RAID_POLL_UI_TEMPLATE":[["alone","extra","extra_alien","remote","inv_plz","can_inv","ex_inv"],["teamlvl"],["time"],["pokemon"],["refresh","alarm","here","late","done","cancel"]],
"RAID_POLL_UI_TEMPLATE":[["alone","extra","extra_alien","can_inv","remote","inv_plz","ex_inv"],["teamlvl"],["time"],["pokemon"],["refresh","alarm","here","late","done","cancel"]],
"RAID_POLL_HIDE_USERS_TIME":"10",
"RAID_POLL_HIDE_BUTTONS_RAID_LEVEL":"1,2,3",
"RAID_POLL_HIDE_BUTTONS_POKEMON":"",
Expand Down
42 changes: 17 additions & 25 deletions core/bot/cleanup_collect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,26 @@

// Channel
if(isset($update['channel_post']['text'])) {
// Get chat_id and message_id
$chat_id = $update['channel_post']['chat']['id'];
$message_id = $update['channel_post']['message_id'];

// Get id from text.
$cleanup_id = substr(strrchr($update['channel_post']['text'], substr(strtoupper($config->BOT_ID), 0, 1) . '-ID = '), 7);
$message = $update['channel_post'];

// Supergroup
} else if (isset($update['message']['text']) && $update['message']['chat']['type'] == "supergroup") {
// Get chat_id and message_id
$chat_id = $update['message']['chat']['id'];
$message_id = $update['message']['message_id'];

} else if (isset($update['message']['text']) && ($update['message']['chat']['type'] == "supergroup" || $update['message']['chat']['type'] == "group")) {
$message = $update['message'];
}
// Get chat_id and message_id
$chat_id = $message['chat']['id'];
$message_id = $message['message_id'];
if(isset($message['reply_markup']['inline_keyboard'])) {
$split_data = explode(':', $message['reply_markup']['inline_keyboard'][0][0]['callback_data']);
$cleanup_id = $split_data[0];
}else {
// Get id from text.
$cleanup_id = substr(strrchr($update['message']['text'], substr(strtoupper($config->BOT_ID), 0, 1) . '-ID = '), 7);
$cleanup_id = substr($message['text'],strpos($message['text'], substr(strtoupper($config->BOT_ID), 0, 1) . '-ID = ') + 7);
}

if (function_exists('insert_cleanup')) {
// Write cleanup info to database.
cleanup_log('Calling cleanup preparation now!');
cleanup_log('Cleanup_ID: ' . $cleanup_id);
if($cleanup_id != 0) {
insert_cleanup($chat_id, $message_id, $cleanup_id);
}
} else {
info_log('No function found to insert cleanup data to database!', 'ERROR:');
info_log('Add a function named "insert_cleanup" to add cleanup info to the database!', 'ERROR:');
info_log('Arguments of that function need to be the chat_id $chat_id, the message_id $message_id and the cleanup id $cleanup_id.', 'ERROR:');
info_log('For example: function insert_cleanup($chat_id, $message_id, $cleanup_id)', 'ERROR:');
// Write cleanup info to database.
cleanup_log('Calling cleanup preparation now!');
cleanup_log('Cleanup_ID: ' . $cleanup_id);
if($cleanup_id != 0) {
insert_cleanup($chat_id, $message_id, $cleanup_id, 'inline_poll_text');
}

4 changes: 2 additions & 2 deletions core/bot/commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}

if($config->TUTORIAL_MODE && isset($update['message']['chat']['id']) && new_user($update['message']['chat']['id']) && $com != 'start') {
sendMessage($update['message']['chat']['id'], getTranslation("tutorial_command_failed"));
send_message($update['message']['chat']['id'], getTranslation("tutorial_command_failed"));
$dbh = null;
exit();
}
Expand Down Expand Up @@ -52,6 +52,6 @@
// Include start file and exit.
include_once($startcommand);
} else {
sendMessage($update['message']['chat']['id'], '<b>' . getTranslation('not_supported') . '</b>');
send_message($update['message']['chat']['id'], '<b>' . getTranslation('not_supported') . '</b>');
}
}
2 changes: 1 addition & 1 deletion core/bot/importal.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
// Invalid input or unknown bot - send message and end.
$msg = '<b>' . getTranslation('invalid_input') . '</b>';
$msg .= CR . CR . getTranslation('not_supported') . SP . getTranslation('or') . SP . getTranslation('internal_error');
sendMessage($update['message']['from']['id'], $msg);
send_message($update['message']['from']['id'], $msg);
exit();
}

Expand Down
2 changes: 1 addition & 1 deletion core/bot/logic/bot_access_check.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ function bot_access_check($update, $permission = 'access-bot', $return_result =
// Telegram multicurl request.
curl_json_multi_request($tg_json);
} else {
sendMessage($update[$update_type]['from']['id'], $response_msg);
send_message($update[$update_type]['from']['id'], $response_msg);
}
exit;
}
Expand Down
12 changes: 6 additions & 6 deletions core/bot/logic/language.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Call the translation function with override parameters.
* @param $text
* @return translation
* @param string $text
* @return string translation
*/
function getPublicTranslation($text)
{
Expand All @@ -14,10 +14,10 @@ function getPublicTranslation($text)

/**
* Gets a table translation out of the json file.
* @param $text
* @param $override
* @param $override_language
* @return translation
* @param string $text
* @param bool $override
* @param string $override_language
* @return string translation
*/
function getTranslation($text, $override = false, $override_language = '')
{
Expand Down
2 changes: 1 addition & 1 deletion core/commands/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@
$msg .= getTranslation('not_supported');
}

sendMessage($update['message']['chat']['id'], $msg);
send_message($update['message']['chat']['id'], $msg);

?>
2 changes: 1 addition & 1 deletion core/commands/set.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@
}

// Send message.
sendMessage($update['message']['chat']['id'], $msg);
send_message($update['message']['chat']['id'], $msg);

?>
Loading

0 comments on commit 0b4b3e3

Please sign in to comment.