Telegram webhook management command updated to use router default context instead of input argument. TelegramWebHookCommand is now a service.

This commit is contained in:
Alexey Skobkin 2017-01-14 02:15:08 +03:00
parent ca62397cbb
commit 93071b6be8
2 changed files with 62 additions and 21 deletions

View file

@ -3,21 +3,64 @@
namespace Skobkin\Bundle\PointToolsBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use unreal4u\TelegramAPI\Telegram\Methods\DeleteWebhook;
use unreal4u\TelegramAPI\Telegram\Methods\SetWebhook;
use unreal4u\TelegramAPI\TgLog;
/**
* Sets or deletes Telegram bot Web-Hook
* @see https://core.telegram.org/bots/api#setwebhook
*/
class TelegramSetWebHookCommand extends ContainerAwareCommand
class TelegramWebHookCommand extends ContainerAwareCommand
{
private const MODE_SET = 'set';
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}
*/
@ -27,7 +70,6 @@ class TelegramSetWebHookCommand extends ContainerAwareCommand
->setName('telegram:webhook')
->setDescription('Set webhook')
->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)
{
$container = $this->getContainer();
$telegramClient = $container->get('app.telegram.api_client');
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');
$token = $container->getParameter('telegram_token');
$url = sprintf(
'https://%s%s',
$input->getArgument('host'),
$router->generate('telegram_webhook', ['token' => $token])
$url = $this->router->generate(
'telegram_webhook',
['token' => $this->token],
UrlGeneratorInterface::ABSOLUTE_URL
);
$output->writeln('Setting webhook: '.$url);
$setWebHook = new SetWebhook();
$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');
} elseif (self::MODE_DELETE === strtolower($input->getArgument('mode'))) {
@ -67,11 +100,11 @@ class TelegramSetWebHookCommand extends ContainerAwareCommand
$deleteWebHook = new DeleteWebhook();
$telegramClient->performApiRequest($deleteWebHook);
$this->client->performApiRequest($deleteWebHook);
$output->writeln('Done');
} 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;

View file

@ -47,6 +47,7 @@ services:
# Console commands
# Subsribers update
app.point.update_subscribers_command:
class: Skobkin\Bundle\PointToolsBundle\Command\UpdateSubscriptionsCommand
calls:
@ -56,8 +57,15 @@ services:
- [setApiClient, ['@app.point.api_user']]
- [setApiDelay, ['%point_api_delay%']]
- [setSubscriptionManager, ['@app.point.subscriptions_manager']]
tags:
- { name: console.command }
tags: [{ 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