$chatId, 'text' => $text, 'parse_mode' => $parseMode, 'disable_web_page_preview' => true, ]; return telegram_post($url, $payload); } /** * $kind: 'photo' or 'document' * - photo → shows as image preview * - document → shows as file (any type: pdf/zip/etc.) */ function telegram_send_file(string $chatId, string $filePath, string $kind = 'document', ?string $caption = null, string $parseMode = 'HTML'): array { global $TELEGRAM_BOT_TOKEN; if (!file_exists($filePath)) { throw new RuntimeException("File not found: $filePath"); } $method = $kind === 'photo' ? 'sendPhoto' : 'sendDocument'; $field = $kind === 'photo' ? 'photo' : 'document'; $url = 'https://api.telegram.org/bot'.$TELEGRAM_BOT_TOKEN.'/'.$method; $payload = [ 'chat_id' => $chatId, $field => new CURLFile(realpath($filePath)), ]; if ($caption !== null) { $payload['caption'] = $caption; $payload['parse_mode'] = $parseMode; } return telegram_post($url, $payload, /*multipart=*/true); } /* ---- internals ---- */ function telegram_post(string $url, array $payload, bool $multipart = false): array { global $TELEGRAM_BOT_TOKEN; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $payload, // if contains CURLFile, cURL uses multipart automatically ]); $resp = curl_exec($ch); if ($resp === false) { $err = curl_error($ch); curl_close($ch); throw new RuntimeException("cURL error: $err"); } curl_close($ch); $data = json_decode($resp, true); if (!is_array($data) || empty($data['ok'])) { throw new RuntimeException("Telegram error: $resp"); } return $data; }