$token, 'chat_id' => $chat, ); } /** * Send alert via Telegram Bot API. * * @param string $subject Alert subject / title. * @param string $body Alert body text. * @param array $context Channel context (token, chat_id). * @return bool True on HTTP 2xx response. */ public static function actually_send( string $subject, string $body, array $context ): bool { $token = (string) ( $context['token'] ?? '' ); $chat = (string) ( $context['chat_id'] ?? '' ); if ( '' === $token || '' === $chat ) { return false; } // Telegram MarkdownV2 has many escaped chars; use plain text mode for safety. $text = "🚨 {$subject}\n\n{$body}"; // Telegram message limit = 4096 chars. $text = substr( $text, 0, 4000 ); $url = sprintf( 'https://api.telegram.org/bot%s/sendMessage', rawurlencode( $token ) ); $resp = wp_remote_post( $url, array( 'headers' => array( 'Content-Type' => 'application/json' ), 'body' => wp_json_encode( array( 'chat_id' => $chat, 'text' => $text, ) ), 'timeout' => 5, 'blocking' => true, ) ); if ( is_wp_error( $resp ) ) { return false; } $code = (int) wp_remote_retrieve_response_code( $resp ); return $code >= 200 && $code < 300; } }