diff --git a/bin/publish-latest-branch-build.php b/bin/publish-latest-branch-build.php index 29763fb..61ceb4d 100755 --- a/bin/publish-latest-branch-build.php +++ b/bin/publish-latest-branch-build.php @@ -1,225 +1,232 @@ #!/usr/bin/env php getTelegramCommentLines() as $apk_line) { + $telegram_apk_lines[] = $apk_line; + $telegram_apk_lines[] = ""; + } foreach ($generated_apk->getPhorgeCommentLines() as $apk_line) { $phorge_apk_lines[] = $apk_line; $phorge_apk_lines[] = ""; } } -$phorge_apk_lines_str = implode("\n", $phorge_apk_lines); +$telegram_apk_lines_str = implode("\n", $telegram_apk_lines); +$phorge_apk_lines_str = implode("\n", $phorge_apk_lines); // add a comment to the build URL if (!empty($phorge_document_id) && empty(getenv('NO_COMMENT'))) { // We have a Differentials revision under review! $telegram_chat_content = implode("\n", [ - "⚙ Dear Android hackers, please review this promising patch:", - "https://gitpull.it/$phorge_document_id", + // In Telegram, normal text must be escaped. Link destinations must not be escaped. + TelegramStupidSDK::escapeMarkdownV2("⚙ Dear Android hackers, please review this promising patch:"), + TelegramStupidSDK::escapeMarkdownV2("https://gitpull.it/$phorge_document_id"), "", - "Here the related fresh test builds:", + TelegramStupidSDK::escapeMarkdownV2("Here the related fresh test builds:"), "", - $phorge_apk_lines_str, + // The texts in these links are already escaped. + $telegram_apk_lines_str, ]); // Send message to Telegram groups. echo "DEBUG TELEGRAM CONTENT\n"; echo "$telegram_chat_content\n"; // 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. $telegram_chat_content = implode("\n", [ "🌚 New commit: *$git_commit_title*", "https://gitpull.it/R4:$git_commit", "", - $phorge_apk_lines_str, + "Here the related fresh test builds:", "", - "APK fingerprint sha256:", - "`$apk_sha256`" + $telegram_apk_lines_str, ]); echo "DEBUG TELEGRAM CONTENT\n"; echo "$telegram_chat_content\n"; // Send message to your default Telegram group. // See the TELEGRAM_* environment variables. (new TelegramStupidSDK())->sendMessage($telegram_chat_content); } // delete very old files print_log_info("Cleaning old artifacts"); shell_exec(sprintf( "find %s -type f -mtime +120 -delete", escapeshellarg($ARTIFACTS_PATH) )); print_log_info("Done"); diff --git a/include/APK.php b/include/APK.php index 0ced1f5..7566293 100644 --- a/include/APK.php +++ b/include/APK.php @@ -1,53 +1,78 @@ name = $name; $this->url = $url; $this->sha256 = $sha256; } /** * @return array */ public function getPhorgeCommentLines() { return [ $this->getPhorgeDownloadLink(), - //'', // No Empty newline between download link and signature $this->getPhorgeAPKSignatureLine() ]; } + /** + * @return array + */ + public function getTelegramCommentLines() { + return [ + $this->getTelegramDownloadLink(), + $this->getPhorgeAPKSignatureLine() + ]; + } + + /** + * Get a very generic download link for Phorge. + * @return string + */ + private function getPhorgeDownloadLink(): string { + return sprintf( + "[%s](%s)", + "Download {$this->name}", + $this->url + ); + } + /** * Get a very generic download link for Phorge. * @return string */ - private function getPhorgeDownloadLink() { + private function getTelegramDownloadLink(): string { + // In Telegram it seems we must escape the link text from special characters. + // The link, instead, can be returned as-is. + // So the Text from: "Download asd.apk" + // to: "Download asd\\.apk" return sprintf( - "[Download %s](%s)", - $this->name, + "[%s](%s)", + TelegramStupidSDK::escapeMarkdownV2("Download {$this->name}"), $this->url ); } /** * Get a very generic line about the sha256sum of this APK. * @return string */ private function getPhorgeAPKSignatureLine() { + // NOTE: if you write a ":" here, Telegram will crash. Sanitize it for Telegram asd. return sprintf( - "APK sha256: `%s`", + "APK sha256 `%s`", $this->sha256 ); } } - diff --git a/include/TelegramStupidSDK.php b/include/TelegramStupidSDK.php index 97ba96d..61ca06c 100644 --- a/include/TelegramStupidSDK.php +++ b/include/TelegramStupidSDK.php @@ -1,103 +1,106 @@ 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 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. - // LOOOOL it's absolutely not clear what we should escape here. - $message_safe = $message; -// $message_safe = $this->escapeMarkdownV2($message); + // Note that you should escape some things by yourself, e.g. the text in the links, like: + // [Download asd\\.apk](https://duck.ai/asd.apk/) $data = [ 'parse_mode' => 'MarkdownV2', 'chat_id' => $chatId, - 'text' => $message_safe, + 'text' => $message, ]; // 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); + // TODO: crash on server error asd. + print_r($response); + // Return the response return $response; } /** * Escape a Telegram message body for MarkdownV2. * In MarkdownV2, these characters must be escaped: _*[]()~`>#+-=|{}.! + * but only if these are parts of a "normal" text body (not in links, etc.) * https://core.telegram.org/bots/api#markdownv2-style */ - private function escapeMarkdownV2(string $input): string { + public static function escapeMarkdownV2(string $input): string { // Characters that need to be escaped in MarkdownV2 $specialChars = ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!']; // Create a search and replace array $search = []; $replace = []; foreach ($specialChars as $char) { $search[] = $char; $replace[] = '\\' . $char; } // Replace all special characters with their escaped versions return str_replace($search, $replace, $input); } }