Telegram webhook management command updated to use router default context instead of input argument. TelegramWebHookCommand is now a service.
This commit is contained in:
parent
ca62397cbb
commit
93071b6be8
|
@ -3,21 +3,64 @@
|
||||||
namespace Skobkin\Bundle\PointToolsBundle\Command;
|
namespace Skobkin\Bundle\PointToolsBundle\Command;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Routing\Router;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||||
use unreal4u\TelegramAPI\Telegram\Methods\DeleteWebhook;
|
use unreal4u\TelegramAPI\Telegram\Methods\DeleteWebhook;
|
||||||
use unreal4u\TelegramAPI\Telegram\Methods\SetWebhook;
|
use unreal4u\TelegramAPI\Telegram\Methods\SetWebhook;
|
||||||
|
use unreal4u\TelegramAPI\TgLog;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets or deletes Telegram bot Web-Hook
|
* Sets or deletes Telegram bot Web-Hook
|
||||||
* @see https://core.telegram.org/bots/api#setwebhook
|
* @see https://core.telegram.org/bots/api#setwebhook
|
||||||
*/
|
*/
|
||||||
class TelegramSetWebHookCommand extends ContainerAwareCommand
|
class TelegramWebHookCommand extends ContainerAwareCommand
|
||||||
{
|
{
|
||||||
private const MODE_SET = 'set';
|
private const MODE_SET = 'set';
|
||||||
private const MODE_DELETE = 'delete';
|
private const MODE_DELETE = 'delete';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var TgLog
|
||||||
|
*/
|
||||||
|
private $client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Router
|
||||||
|
*/
|
||||||
|
private $router;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $token;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $maxConnections;
|
||||||
|
|
||||||
|
public function setClient(TgLog $client): void
|
||||||
|
{
|
||||||
|
$this->client = $client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setRouter(Router $router): void
|
||||||
|
{
|
||||||
|
$this->router = $router;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setToken(string $token): void
|
||||||
|
{
|
||||||
|
$this->token = $token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setMaxConnections(int $maxConnections)
|
||||||
|
{
|
||||||
|
$this->maxConnections = $maxConnections;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -27,7 +70,6 @@ class TelegramSetWebHookCommand extends ContainerAwareCommand
|
||||||
->setName('telegram:webhook')
|
->setName('telegram:webhook')
|
||||||
->setDescription('Set webhook')
|
->setDescription('Set webhook')
|
||||||
->addArgument('mode', InputArgument::REQUIRED, 'Command mode (set or delete)')
|
->addArgument('mode', InputArgument::REQUIRED, 'Command mode (set or delete)')
|
||||||
->addArgument('host', InputArgument::OPTIONAL, 'Host of telegram hook')
|
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,30 +78,21 @@ class TelegramSetWebHookCommand extends ContainerAwareCommand
|
||||||
*/
|
*/
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$container = $this->getContainer();
|
|
||||||
$telegramClient = $container->get('app.telegram.api_client');
|
|
||||||
|
|
||||||
if (self::MODE_SET === strtolower($input->getArgument('mode'))) {
|
if (self::MODE_SET === strtolower($input->getArgument('mode'))) {
|
||||||
if (!$input->hasArgument('host')) {
|
|
||||||
throw new \InvalidArgumentException('Host must be specified when using --set flag');
|
|
||||||
}
|
|
||||||
|
|
||||||
$router = $container->get('router');
|
$url = $this->router->generate(
|
||||||
$token = $container->getParameter('telegram_token');
|
'telegram_webhook',
|
||||||
|
['token' => $this->token],
|
||||||
$url = sprintf(
|
UrlGeneratorInterface::ABSOLUTE_URL
|
||||||
'https://%s%s',
|
|
||||||
$input->getArgument('host'),
|
|
||||||
$router->generate('telegram_webhook', ['token' => $token])
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$output->writeln('Setting webhook: '.$url);
|
$output->writeln('Setting webhook: '.$url);
|
||||||
|
|
||||||
$setWebHook = new SetWebhook();
|
$setWebHook = new SetWebhook();
|
||||||
$setWebHook->url = $url;
|
$setWebHook->url = $url;
|
||||||
$setWebHook->max_connections = (int) $container->getParameter('telegram_max_connections');
|
$setWebHook->max_connections = $this->maxConnections;
|
||||||
|
|
||||||
$telegramClient->performApiRequest($setWebHook);
|
$this->client->performApiRequest($setWebHook);
|
||||||
|
|
||||||
$output->writeln('Done');
|
$output->writeln('Done');
|
||||||
} elseif (self::MODE_DELETE === strtolower($input->getArgument('mode'))) {
|
} elseif (self::MODE_DELETE === strtolower($input->getArgument('mode'))) {
|
||||||
|
@ -67,11 +100,11 @@ class TelegramSetWebHookCommand extends ContainerAwareCommand
|
||||||
|
|
||||||
$deleteWebHook = new DeleteWebhook();
|
$deleteWebHook = new DeleteWebhook();
|
||||||
|
|
||||||
$telegramClient->performApiRequest($deleteWebHook);
|
$this->client->performApiRequest($deleteWebHook);
|
||||||
|
|
||||||
$output->writeln('Done');
|
$output->writeln('Done');
|
||||||
} else {
|
} else {
|
||||||
throw new \InvalidArgumentException(sprintf('Mode must be exaclty one of: %s', implode(', ', [self::MODE_SET, self::MODE_DELETE])));
|
throw new \InvalidArgumentException(sprintf('Mode must be exactly one of: %s', implode(', ', [self::MODE_SET, self::MODE_DELETE])));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
|
@ -47,6 +47,7 @@ services:
|
||||||
|
|
||||||
|
|
||||||
# Console commands
|
# Console commands
|
||||||
|
# Subsribers update
|
||||||
app.point.update_subscribers_command:
|
app.point.update_subscribers_command:
|
||||||
class: Skobkin\Bundle\PointToolsBundle\Command\UpdateSubscriptionsCommand
|
class: Skobkin\Bundle\PointToolsBundle\Command\UpdateSubscriptionsCommand
|
||||||
calls:
|
calls:
|
||||||
|
@ -56,8 +57,15 @@ services:
|
||||||
- [setApiClient, ['@app.point.api_user']]
|
- [setApiClient, ['@app.point.api_user']]
|
||||||
- [setApiDelay, ['%point_api_delay%']]
|
- [setApiDelay, ['%point_api_delay%']]
|
||||||
- [setSubscriptionManager, ['@app.point.subscriptions_manager']]
|
- [setSubscriptionManager, ['@app.point.subscriptions_manager']]
|
||||||
tags:
|
tags: [{ name: console.command }]
|
||||||
- { name: console.command }
|
# Webhook management
|
||||||
|
app.telegram.webhook_command:
|
||||||
|
class: Skobkin\Bundle\PointToolsBundle\Command\TelegramWebHookCommand
|
||||||
|
calls:
|
||||||
|
- [setClient, ['@app.telegram.bot_client']]
|
||||||
|
- [setRouter, ['@router']]
|
||||||
|
- [setToken, ['%telegram_token%']]
|
||||||
|
tags: [{ name: console.command }]
|
||||||
|
|
||||||
|
|
||||||
# Entity repositories as services
|
# Entity repositories as services
|
||||||
|
|
Loading…
Reference in a new issue