point-tools/src/Skobkin/Bundle/PointToolsBundle/Command/TelegramWebHookCommand.php

93 lines
2.6 KiB
PHP
Raw Normal View History

2017-01-04 22:36:30 +00:00
<?php
namespace Skobkin\Bundle\PointToolsBundle\Command;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\{InputArgument, InputInterface};
2017-01-04 22:36:30 +00:00
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use unreal4u\TelegramAPI\Telegram\Methods\{DeleteWebhook, SetWebhook};
use unreal4u\TelegramAPI\TgLog;
2017-01-04 22:36:30 +00:00
/**
* Sets or deletes Telegram bot Web-Hook
* @see https://core.telegram.org/bots/api#setwebhook
*/
class TelegramWebHookCommand extends Command
2017-01-04 22:36:30 +00:00
{
2017-01-12 23:02:25 +00:00
private const MODE_SET = 'set';
private const MODE_DELETE = 'delete';
2017-01-04 22:36:30 +00:00
/** @var TgLog */
private $client;
/** @var Router */
private $router;
/** @var string */
private $token;
/** @var int */
private $maxConnections;
public function __construct(TgLog $client, Router $router, string $telegramToken, int $telegramWebhookMaxConnections)
{
parent::__construct();
$this->client = $client;
$this->router = $router;
$this->token = $telegramToken;
$this->maxConnections = $telegramWebhookMaxConnections;
}
2017-01-04 22:36:30 +00:00
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('telegram:webhook')
->setDescription('Set webhook')
->addArgument('mode', InputArgument::REQUIRED, 'Command mode (set or delete)')
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (self::MODE_SET === strtolower($input->getArgument('mode'))) {
$url = $this->router->generate(
'telegram_webhook',
['token' => $this->token],
UrlGeneratorInterface::ABSOLUTE_URL
2017-01-04 22:36:30 +00:00
);
$output->writeln('Setting webhook: '.$url);
$setWebHook = new SetWebhook();
$setWebHook->url = $url;
$setWebHook->max_connections = $this->maxConnections;
2017-01-04 22:36:30 +00:00
$this->client->performApiRequest($setWebHook);
2017-01-04 22:36:30 +00:00
$output->writeln('Done');
} elseif (self::MODE_DELETE === strtolower($input->getArgument('mode'))) {
$output->writeln('Deleting webhook');
$deleteWebHook = new DeleteWebhook();
$this->client->performApiRequest($deleteWebHook);
2017-01-04 22:36:30 +00:00
$output->writeln('Done');
} else {
throw new \InvalidArgumentException(sprintf('Mode must be exactly one of: %s', implode(', ', [self::MODE_SET, self::MODE_DELETE])));
2017-01-04 22:36:30 +00:00
}
return 0;
}
}