diff --git a/include/TelegramStupidSDK.php b/include/TelegramStupidSDK.php index f4d3ab0..b094904 100644 --- a/include/TelegramStupidSDK.php +++ b/include/TelegramStupidSDK.php @@ -1,107 +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 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); + //$message_safe = $this->escapeMessage($message); $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); // Return the response return $response; } /** * 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; } }