<?php
namespace App\Controller;
use App\Repository\AutorizationStatusRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class TestApiController extends AbstractController
{
private $client;
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}
#[Route('/test/api/{name}', name: 'app_test_api')]
public function index(string $name = '',AutorizationStatusRepository $autorizationStatus): Response
{
define('TOKEN', '5749519723:AAGtXtempHM2pLa1kWLCarVBuK_PPdbXcu4');
$statuses = $autorizationStatus->findOneBy(['id' => $name]);
$this->sendOneReport($statuses);
return $this->render('test_api/index.html.twig', [
'controller_name' => 'TestApiController',
]);
}
public function sendOneReport($status)
{
$login = $status->getLogin();
$password = $status->getPassword();
$cid = $status->getCid();
// echo "LOGIN $login \n";
// echo "PASSWORD $password \n";
// echo "CID $cid \n";
//
// exit();
if(!is_numeric($cid)){
return;
}
$authorization = 'Basic ' . base64_encode($login . ':' . $password);
$urlForRequest = 'https://cid' . $cid . '.core12.ru/api/telegram-bot-get-data';
$urlForMessage = 'https://cid' . $cid . '.core12.ru';
$response = $this->client->request('GET', $urlForRequest, [
'headers' => [
'authorization' => $authorization,
],
]
);
try {
$response->getStatusCode();
} catch (\Exception $e) {
return;
}
$statusCode = $response->getStatusCode();
// echo $statusCode . "\n";
if($statusCode == 200){
$preDataForSend = $response->toArray();
$revenue = number_format($preDataForSend['sumPayYesterday']['sumOfPay'],0,'',' ') . " ₽";
$totalSalary = number_format($preDataForSend['sumManagerSalaryYesterday']['sumOfSalaryManager'] + $preDataForSend['doerReward']['sumOfSalaryDoer'],0,'',' ') . " ₽";
$dataForSend = "<b>Ежедневный отчет Core12</b> \n\n" . $preDataForSend['yesterday'] .
"\n\n" . "<b>" . $preDataForSend['street'] . "</b>" .
"\n\n" .
"Выручка " . "<b>" . $revenue . "</b>" . "\n" .
"Выплаты сотрудникам " . "<b>" . $totalSalary . "</b>" . "\n" .
"Клиенты " . "<b>" . $preDataForSend['quantityClientYesterday']['quantityClient'] . "</b>" . "\n" .
"Новые клиенты " . "<b>" . $preDataForSend['newClientYesterday'] . "</b>" . "\n" .
"Подозрительные события " . "<b>" . $preDataForSend['sumSuspicious']['sumSuspicious'] . "</b>" . "\n" .
"Негативные отзывы " . "<b>" . $preDataForSend['negativeFeedback']['rating'] . "</b>" . "\n" .
"#отчетизcore12"
;
$UrlOfCid = [
'inline_keyboard' => [[[
"text" => "Перейти в личный кабинет",
"url" => $urlForMessage
]]]
];
}
else{
$dataForSend = 'Введены неверные учетные данные';
}
$idInTelegram = $status->getChatId();
$ch = curl_init();
$ch_post = [
CURLOPT_URL => 'https://api.telegram.org/bot' . TOKEN . '/sendMessage',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 10,
CURLOPT_POSTFIELDS => [
'chat_id' => $idInTelegram,
'parse_mode' => 'HTML',
'text' => $dataForSend,
'reply_markup' => json_encode($UrlOfCid)
]
];
curl_setopt_array($ch, $ch_post);
curl_exec($ch);
}
}