Ported WebHookController.

This commit is contained in:
Alexey Skobkin 2023-04-01 19:53:42 +03:00
parent 430632c8e4
commit a83b783de4
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
2 changed files with 11 additions and 19 deletions

View file

@ -14,6 +14,7 @@ services:
# TODO: fix retrieval # TODO: fix retrieval
# Telegram Bot API # Telegram Bot API
$telegramToken: '' $telegramToken: ''
$debugEnabled: '%kernel.debug%'
# Point API # Point API
$pointDomain: 'point.im' $pointDomain: 'point.im'
$pointScheme: 'https' $pointScheme: 'https'

View file

@ -1,28 +1,20 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Controller\Telegram; namespace App\Controller\Telegram;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use src\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher; use src\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response}; use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response};
use unreal4u\TelegramAPI\Telegram\Types\Update; use unreal4u\Telegram\Types\Update;
/**
* {@inheritdoc}
*/
class WebHookController extends AbstractController class WebHookController extends AbstractController
{ {
/** @var string */ public function __construct(
private $telegramToken; private readonly string $telegramToken,
private readonly bool $debugEnabled,
/** @var bool */ ) {
private $debug;
public function __construct(string $telegramToken, bool $debug)
{
$this->telegramToken = $telegramToken;
$this->debug = $debug;
} }
public function receiveUpdateAction(Request $request, string $token, IncomingUpdateDispatcher $updateDispatcher, LoggerInterface $logger): Response public function receiveUpdateAction(Request $request, string $token, IncomingUpdateDispatcher $updateDispatcher, LoggerInterface $logger): Response
@ -31,21 +23,20 @@ class WebHookController extends AbstractController
throw $this->createNotFoundException(); throw $this->createNotFoundException();
} }
$content = json_decode($request->getContent(), true); $content = \json_decode($request->getContent(), flags: JSON_THROW_ON_ERROR);
$update = new Update( $update = new Update(
$content, $content,
$logger
); );
try { try {
$updateDispatcher->process($update); $updateDispatcher->process($update);
} catch (\Exception $e) { } catch (\Exception $e) {
if ($this->debug) { if ($this->debugEnabled) {
throw $e; throw $e;
} }
$logger->addError('Telegram bot error', [ $logger->error('Telegram bot error', [
'exception' => get_class($e), 'exception' => get_class($e),
'file' => $e->getFile(), 'file' => $e->getFile(),
'line' => $e->getLine(), 'line' => $e->getLine(),