| Server IP : 95.46.96.79 / Your IP : 216.73.216.84 Web Server : Apache/2.4.41 (Ubuntu) System : Linux zaimer.uz 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : akslabs_uz_usr ( 1001) PHP Version : 7.4.3-4ubuntu2.29 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/sugurtatrip__usr95/data/www/sugurtatrip.uz/apiv1/user/Services/ |
Upload File : |
<?php
require_once __DIR__ . '/../Core/HttpClient.php';
require_once __DIR__ . '/../database/Database.php';
// require_once __DIR__ . '/../Core/Logger.php';
require_once __DIR__ . '/../Models/ErrorCodes.php';
require_once __DIR__ . '/../Models/ErrorMessages.php';
class TelegramService
{
public function __construct() {
}
public function createClientRequest($data) {
$error_message = '';
$request_saved = $this->saveRequest($data);
$notification_sent = $this->sendTGNotification($data);
if ($request_saved && $notification_sent) {
return [
'success' => true,
'data' => $request_saved,
];
}
if (!$request_saved) {
$error_message .= 'Request not saved. ';
}
if (!$notification_sent) {
$error_message .= 'Notification not sent. ';
}
return [
'success' => false,
'error' => ErrorCodes::SERVER_ERROR,
'error_message' => $error_message
];
}
private function saveRequest($data) {
$db = new Database();
$client = trim($db->escapeString($data['client']));
$phone = trim($db->escapeString($data['phone']));
$country = trim($db->escapeString($data['country']));
$peoples = (int) $data['peoples'];
$start_date = DateTime::createFromFormat('d.m.Y', $data['start']) ? $data['start'] : null;
$end_date = DateTime::createFromFormat('d.m.Y', $data['end']) ? $data['end'] : null;
$days = (int) $data['days'];
$program = trim($db->escapeString($data['program']));
// $amount = (int) str_replace(',', '', $data['amount']);
$amount = $data['amount'];
$query = "INSERT INTO applications (client_name, client_phone, country, peoples, start_date, end_date, days, program, amount, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";
$stmt = $db->prepare($query);
$stmt->bind_param("sssissisi", $client, $phone, $country, $peoples, $start_date, $end_date, $days, $program, $amount);
if ($stmt->execute()) {
$insertedId = $stmt->insert_id;
$stmt->close();
$db->close();
return $insertedId;
}
$stmt->close();
$db->close();
return false;
}
public function sendTGNotification($data) {
try {
$chatId = $_ENV['TG_CHAT_ID'];
$botToken = $_ENV['TG_BOT_TOKEN'];
$client = trim($data['client']);
$phone = trim($data['phone']);
$country = trim($data['country']);
$peoples = (int) $data['peoples'];
$start_date = trim($data['start']);
$end_date = trim($data['end']);
$days = (int) $data['days'];
$program = trim($data['program']);
$amount = (int) $data['amount'];
$message = "Новая заявка от " . $client . "\n" .
"Телефон: " . $phone . "\n" .
"Страна: " . $country . "\n" .
"Количество человек: " . $peoples . "\n" .
"Дата начала: " . $start_date . "\n" .
"Дата окончания: " . $end_date . "\n" .
"Количество дней: " . $days . "\n" .
"Программа: " . $program . "\n" .
"Сумма: " . $amount . " сум.";
$tg_data = [
'chat_id' => $chatId,
'text' => $message,
];
$url = "https://api.telegram.org/bot" . $botToken . "/sendMessage";
$response = HttpClient::post($url, $tg_data);
$http_code = $response['http_code'];
$response = json_decode($response['response'], true);
if ($http_code === 200 && $response['ok'] === true) {
return true;
} else {
return false;
}
} catch (Exception $e) {
return false;
}
return false;
}
}