diff --git a/bin/publish-latest-branch-build.php b/bin/publish-latest-branch-build.php index 1278cb9..a91df05 100755 --- a/bin/publish-latest-branch-build.php +++ b/bin/publish-latest-branch-build.php @@ -1,251 +1,249 @@ #!/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"; - shell_exec(sprintf( - "~/bin/telegram-comment.sh %s %s %s", - escapeshellarg($TELEGRAM_BOT_TOKEN), - escapeshellarg($TELEGRAM_CHAT_ID), - escapeshellarg($telegram_chat_content) - )); + + // Send message to Telegram. + TelegramStupidSDK::createFromBotToken($TELEGRAM_BOT_TOKEN) + ->sendMessageToChat($telegram_chat_content, $TELEGRAM_CHAT_ID); // 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/bin/telegram-comment.sh b/bin/telegram-comment.sh deleted file mode 100755 index 5b9f114..0000000 --- a/bin/telegram-comment.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh -e - -# TODO: avoid this file. Rewrite in PHP. - -if [ -z "$1" ]; then - echo "Missing Telegram Bot ID" - exit 1 -fi - -if [ -z "$2" ]; then - echo "Missing Chat ID" - exit 1 -fi - -if [ -z "$3" ]; then - echo "Missing message text" - exit 1 -fi - -echo "TELEGRAM COMMENT" "$3" -exit - -curl \ - --data parse_mode=MarkdownV2 \ - --data chat_id="$2" \ - --data text="$3" \ - --request POST https://api.telegram.org/bot"$1"/sendMessage diff --git a/include/TelegramStupidSDK.php b/include/TelegramStupidSDK.php new file mode 100644 index 0000000..3090f0c --- /dev/null +++ b/include/TelegramStupidSDK.php @@ -0,0 +1,97 @@ +botToken = $bot_token; + } + + /** + * Get an action URL. + */ + private function getActionURL(string $action): string { + return "https://api.telegram.org/bot{$this->botToken}/{$action}"; + } + + /** + * Send a Telegram message to a Chat ID. + */ + public function sendMessageToChat(string $message, int $chatId) { + // 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; + } +} diff --git a/include/autoload.php b/include/autoload.php index 8edbd24..be4bd05 100644 --- a/include/autoload.php +++ b/include/autoload.php @@ -1,13 +1,14 @@