diff --git a/bin/publish-latest-branch-build.php b/bin/publish-latest-branch-build.php index a91df05..59be2a8 100755 --- a/bin/publish-latest-branch-build.php +++ b/bin/publish-latest-branch-build.php @@ -1,249 +1,248 @@ #!/usr/bin/env php sha1sum.txt'); chdir('..'); $apix = '`'; // add a comment to the build URL if (!empty($phorge_document_id) && empty(getenv('NO_COMMENT'))) { // We have a Differentials revision under review! $phorge_document_id_escaped = escapeshellarg($phorge_document_id); $phorge_comment_header_lines = [ "⚙ Dear Android hackers, please review this promising patch:", "https://gitpull.it/$phorge_document_id_escaped", "", "Here the related fresh test builds:", ]; // Build Phorge comment. // There is a fixed header, and multiple lines about each APK. $phorge_apk_lines = []; foreach ($generated_apks as $generated_apk) { foreach ($generated_apk->getPhorgeCommentLines() as $apk_line) { $phorge_apk_lines[] = $apk_line; } } $telegram_chat_content = implode("\n", array_merge([ $phorge_comment_header_lines, $phorge_apk_lines, ])); // Send message to Telegram groups. echo "DEBUG TELEGRAM CONTENT\n"; echo "$telegram_chat_content\n"; - // Send message to Telegram. - TelegramStupidSDK::createFromBotToken($TELEGRAM_BOT_TOKEN) - ->sendMessageToChat($telegram_chat_content, $TELEGRAM_CHAT_ID); + // Send message to your default Telegram group. + // See the TELEGRAM_* environment variables. + (new TelegramStupidSDK())->sendMessage($telegram_chat_content); // Send message to Phorge. echo "Adding bipbop Comment to $phorge_document_id\n"; shell_exec(sprintf( "~/bin/phab-comment.php %s %s %s", escapeshellarg($phorge_document_id), escapeshellarg($apk_destination_url), escapeshellarg($apk_sha256) )); } else { // We have NOT a Differentials revision under review! // So this is an atomic commit. $git_commit_title_escaped = escapeshellarg($git_commit_title); $telegram_chat_content = implode("\n", [ "🌚 New commit: *$git_commit_title_escaped*", "https://gitpull.it/R4:$git_commit", "", "Try this fresh test APK:", "[Download $apk_destination_name_escaped]($apk_destination_url_escaped)", "", "APK fingerprint sha256:", "$apix$apk_sha256$apix" ]); echo "DEBUG TELEGRAM CONTENT\n"; echo "$telegram_chat_content\n"; shell_exec(sprintf( "~/bin/telegram-comment.sh %s %s %s", escapeshellarg($TELEGRAM_BOT_TOKEN), escapeshellarg($TELEGRAM_CHAT_ID), escapeshellarg($telegram_chat_content) )); } // delete very old files print_log_info("Cleaning old artifacts"); shell_exec(sprintf( "find %s -type f -mtime +120 -delete", escapeshellarg($EXPECTED_DEPLOY_PATH) )); // clean old APK in the repository to never re-use it by mistake // as it happened when we moved everything to the /app directory unlink($apk_path); print_log_info("Done"); diff --git a/include/TelegramStupidSDK.php b/include/TelegramStupidSDK.php index 3090f0c..f4d3ab0 100644 --- a/include/TelegramStupidSDK.php +++ b/include/TelegramStupidSDK.php @@ -1,97 +1,107 @@ botToken = $bot_token; } /** * Get an action URL. */ private function getActionURL(string $action): string { return "https://api.telegram.org/bot{$this->botToken}/{$action}"; } + /** + * Get the default chat ID from the environment variable. + */ + private function getDefaultChatID(): int { + return require_env('TELEGRAM_CHAT_ID'); + } + /** * Send a Telegram message to a Chat ID. */ - public function sendMessageToChat(string $message, int $chatId) { + public function sendMessage(string $message, int $chatId = null) { + // Assume a sane default. + if ($chatId === null) { + $chatId = $this->getDefaultChatID(); + } + // Prepare the URL for the Telegram API. // Initialize cURL. $url = $this->getActionURL('sendMessage'); $ch = curl_init($url); // Prepare the data to be sent $message_safe = $this->escapeMessage($message); $data = [ 'parse_mode' => 'MarkdownV2', 'chat_id' => $chatId, 'text' => $message_safe, ]; // Set cURL options curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute the request $response = curl_exec($ch); // Check for errors if (curl_errno($ch)) { throw new Exception(sprintf( "Error running cURL: %s", curl_error($ch) )); } // Close cURL session curl_close($ch); // Return the response return $response; } - /** - * Handy constructor, with a nice name. - */ - public static function createFromBotToken(string $bot_token): TelegramStupidSDK { - return new self($bot_token); - } - /** * Escape a Telegram message body. * https://core.telegram.org/bots/api * «Any character with code between 1 and 126 inclusively can be escaped anywhere with a preceding '\' character, * in which case it is treated as an ordinary character and not a part of the markup. * This implies that '\' character usually must be escaped with a preceding '\' character.» */ private function escapeMessage(string $input) { $escapedString = ''; // Iterate through each character in the input string for ($i = 0; $i < strlen($input); $i++) { $char = $input[$i]; $asciiValue = ord($char); // Check if the ASCII value is between 1 and 126 if ($asciiValue >= 1 && $asciiValue <= 126) { // Escape the character by prepending a backslash $escapedString .= '\\' . $char; } else { // If not in the range, just append the character as is $escapedString .= $char; } } return $escapedString; } }