/** * Bot.php – Telegram Bot API wrapper (PHP 8.4) * * Ushbu sinf Telegram Bot API bilan oddiy HTTP so‘rovlar orqali * o‘zaro aloqani ta'minlaydi. cURL kutubxonasidan foydalanadi va * JSON formatidagi natijalarni qaytaradi. */ class Bot { private string $token; private string $apiBase; public function __construct(string $botToken) { $this->token = $botToken; $this->apiBase = "https://api.telegram.org/bot{$this->token}/"; } /** * Telegramga oddiy POST so‘rovi yuboradi. */ private function request(string $method, array $params = []): array { $url = $this->apiBase . $method; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $params, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0, ]); $response = curl_exec($ch); $err = curl_error($ch); curl_close($ch); if ($err) { // oddiy log – real loyihada faylga yozish tavsiya etiladi error_log('Telegram API error: ' . $err); return []; } return json_decode($response, true) ?? []; } public function sendMessage(int $chatId, string $text, array $replyMarkup = []): void { $params = [ 'chat_id' => $chatId, 'text' => $text, 'parse_mode' => 'HTML', ]; if ($replyMarkup) { $params['reply_markup'] = json_encode($replyMarkup); } $this->request('sendMessage', $params); } public function answerCallbackQuery(string $callbackQueryId, string $text = ''): void { $params = [ 'callback_query_id' => $callbackQueryId, ]; if ($text) { $params['text'] = $text; } $this->request('answerCallbackQuery', $params); } public function answerPreCheckoutQuery(string $preCheckoutId, bool $ok, string $errorMessage = ''): void { $params = [ 'pre_checkout_query_id' => $preCheckoutId, 'ok' => $ok ? 'true' : 'false', ]; if (!$ok && $errorMessage) { $params['error_message'] = $errorMessage; } $this->request('answerPreCheckoutQuery', $params); } /** * Telegram Payments uchun invoiceni yaratadi. */ public function sendInvoice(int $chatId, int $priceUzs, string $payload, string $title = 'Ovoz berish', string $description = 'Open Budget loyihasiga ovoz berish') { // Telegramga to‘lov valyutasi – UZS emas, lekin biz “UZS” ni kamroq ishlatamiz. // Realda USD/EUR kerak bo‘ladi, lekin soddalashtirish maqsadida. $prices = [ [ 'label' => $title, 'amount' => $priceUzs * 100, // Telegram smallest currency unit, so‘mda 100 ], ]; $params = [ 'chat_id' => $chatId, 'title' => $title, 'description' => $description, 'payload' => $payload, 'provider_token' => $GLOBALS['uzcard']['merchant_id'], // placeholder – uzcard token 'currency' => 'UZS', 'prices' => json_encode($prices), ]; $this->request('sendInvoice', $params); /** * Send a formatted notification with a leading emoji. This standardises all push messages. * @param int $chatId Telegram chat ID * @param string $message Message body (HTML allowed) * @param string $emoji Emoji prefix, default 🔔 */ public function sendNotification(int $chatId, string $message, string $emoji = "🔔"): void { $this->sendMessage($chatId, $emoji . ' ' . $message); } /** * Wrapper for sending email notifications (optional, uses PHP mail()). * Configured via `config.php['smtp']` if present. */ public function sendEmail(string $to, string $subject, string $htmlBody): void { $cfg = require __DIR__ . '/config.php'; if (empty($cfg['smtp'])) { // No SMTP config – ignore silently. return; } $headers = "MIME-Version: 1.0\r\n" . "Content-Type: text/html; charset=UTF-8\r\n" . "From: {$cfg['smtp']['from_name']} <{$cfg['smtp']['from_email']}>\r\n"; // In real deployment you would use a proper library (PHPMailer), but for simplicity we call mail(). @mail($to, $subject, $htmlBody, $headers); } } ?>