<?php
namespace App\Controller\Cronjob;
use App\Entity\VPN\Service\Service;
use App\Repository\Telegram\AgentPublicBot\BotRepository;
use App\Repository\VPN\Service\ServiceRepository;
use App\Service\Util\NotificationService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class NotificationController extends AbstractController
{
#[Route('/cronjob/notification/site-notification', name: 'app_cronjob_notification_size')]
public function app_cronjob_notification_size(ServiceRepository $serviceRepository,NotificationService $notificationService, BotRepository $botRepository, EntityManagerInterface $entityManager): JsonResponse
{
$mainBot = $botRepository->findOneBy(['isBotForWebsite' => 1]);
$limitedServices = $serviceRepository->getLessThanServicesWithoutNotifications(1073741824);
foreach ($limitedServices as $service) {
try {
/**
* @var Service $service
*/
$keyboard = json_encode([
"inline_keyboard" => [
[
[
"text" => "تمیدید مستقیم سرویس {$service->getClientId()} - {$service->getUsername()}",
"url" => "https://tehrannet.org//panel/user/vpn/service/charge/{$service->getId()}"
]
]
]
]);
$message = "#اطلاعیه" . PHP_EOL;
$message = $message . "#حجم" . PHP_EOL;
$message = $message . "سرویس کمتر از یک گیگ حجم دارد برای جلو گیری از قطعی سرویس را تمدید فرمایید" . PHP_EOL;
$message = $message . "شناسه سرویس : " . $service->getClientId() . PHP_EOL;
$message = $message . "نام کاربری سرویس : " . $service->getUsername() . PHP_EOL;
$service->setSizeNotif(1);
$this->sendMessage($mainBot->getToken(), $service->getOwner()->getConnectedTelegramUserId()['userId'], $message, $keyboard);
try {
$creatorBot = $service->getCreatorBot();
$botUser = $service->getBotUser();
if ($creatorBot && $botUser){
$message = "#اطلاعیه" . PHP_EOL;
$message = $message . "#حجم" . PHP_EOL;
$message = $message . "سرویس کمتر از یک گیگ حجم دارد برای جلو گیری از قطعی سرویس را تمدید فرمایید" . PHP_EOL;
$message = $message . "شناسه سرویس : " . $service->getClientId() . PHP_EOL;
$message = $message . "نام کاربری سرویس : " . $service->getUsername() . PHP_EOL;
$userBot = $service->getBotUser();
$this->sendMessage($creatorBot->getToken(),$userBot->getUserId(), $message);
}
}catch (\Exception $exception){
$notificationService->exception($exception);
}
} catch (\Exception $exception) {
$notificationService->exception($exception);
$service->getOwner()->setConnectedTelegramUserId([]);
$entityManager->flush();
}
}
$entityManager->flush();
return $this->json(['res' => true]);
}
#[Route('/cronjob/notification/time-notification', name: 'app_cronjob_notification_time')]
public function app_cronjob_notification_time(NotificationService $notificationService ,ServiceRepository $serviceRepository, BotRepository $botRepository, EntityManagerInterface $entityManager): JsonResponse
{
$mainBot = $botRepository->findOneBy(['isBotForWebsite' => 1]);
$limitedServices = $serviceRepository->getLessTimeServicesWithoutNotifications(2);
foreach ($limitedServices as $service) {
try {
/**
* @var Service $service
*/
$keyboard = json_encode([
"inline_keyboard" => [
[
[
"text" => "تمیدید مستقیم سرویس {$service->getClientId()} - {$service->getUsername()}",
"url" => "https://tehrannet.org//panel/user/vpn/service/charge/{$service->getId()}"
]
]
]
]);
$message = "#اطلاعیه" . PHP_EOL;
$message = $message . "#زمان" . PHP_EOL;
$message = $message . "سرویس کمتر از یک روز زمان دارد برای جلو گیری از قطعی سرویس را تمدید فرمایید" . PHP_EOL;
$message = $message . "شناسه سرویس : " . $service->getClientId() . PHP_EOL;
$message = $message . "نام کاربری سرویس : " . $service->getUsername() . PHP_EOL;
$service->setDayNotif(1);
$this->sendMessage($mainBot->getToken(), $service->getOwner()->getConnectedTelegramUserId()['userId'], $message, $keyboard);
try {
$creatorBot = $service->getCreatorBot();
$userBot = $service->getBotUser();
if ($creatorBot && $userBot){
$message = "#اطلاعیه" . PHP_EOL;
$message = $message . "#زمان" . PHP_EOL;
$message = $message . "سرویس کمتر از یک روز زمان دارد برای جلو گیری از قطعی سرویس را تمدید فرمایید" . PHP_EOL;
$message = $message . "شناسه سرویس : " . $service->getClientId() . PHP_EOL;
$message = $message . "نام کاربری سرویس : " . $service->getUsername() . PHP_EOL;
$this->sendMessage($creatorBot->getToken(),$userBot->getUserId(), $message);
}
}catch (\Exception $exception){
$notificationService->exception($exception);
}
} catch (\Exception $exception) {
$notificationService->exception($exception);
$service->getOwner()->setConnectedTelegramUserId([]);
$entityManager->flush();
}
}
$entityManager->flush();
return $this->json(['res' => true]);
}
private function sendMessage($token, string $chatId, string $text, $keyboard = [], $markdown = null, bool $silent = false): void
{
$url = sprintf(
'https://api.telegram.org/bot%s/sendMessage',
$token
);
$payload = [
'chat_id' => $chatId,
'text' => ($text),
'disable_notification' => $silent, // Set to true for silent messages
'reply_markup' => $keyboard
];
if ($markdown) {
$payload['parse_mode'] = $markdown;
}
$this->sendRequest($url, $payload);
}
private function sendRequest(string $url, array $payload)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
if (curl_errno($ch)) {
// Log or handle the error
throw new \Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
return $response; // Return the response for further processing
}
}