2017-01-04 22:36:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Command;
|
|
|
|
|
2017-11-05 01:42:08 +00:00
|
|
|
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;
|
2017-01-13 23:15:08 +00:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2017-11-05 01:42:08 +00:00
|
|
|
use unreal4u\TelegramAPI\Telegram\Methods\{DeleteWebhook, SetWebhook};
|
2017-01-13 23:15:08 +00:00
|
|
|
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
|
|
|
|
*/
|
2017-11-05 01:42:08 +00:00
|
|
|
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
|
|
|
|
2017-11-05 01:42:08 +00:00
|
|
|
/** @var TgLog */
|
2017-01-13 23:15:08 +00:00
|
|
|
private $client;
|
|
|
|
|
2017-11-05 02:39:52 +00:00
|
|
|
/** @var UrlGeneratorInterface */
|
2017-01-13 23:15:08 +00:00
|
|
|
private $router;
|
|
|
|
|
2017-11-05 01:42:08 +00:00
|
|
|
/** @var string */
|
2017-01-13 23:15:08 +00:00
|
|
|
private $token;
|
|
|
|
|
2017-11-05 01:42:08 +00:00
|
|
|
/** @var int */
|
2017-01-13 23:15:08 +00:00
|
|
|
private $maxConnections;
|
|
|
|
|
2017-11-05 02:39:52 +00:00
|
|
|
public function __construct(TgLog $client, UrlGeneratorInterface $router, string $telegramToken, int $telegramWebhookMaxConnections)
|
2017-01-13 23:15:08 +00:00
|
|
|
{
|
2017-11-05 01:42:08 +00:00
|
|
|
parent::__construct();
|
2017-01-13 23:15:08 +00:00
|
|
|
|
2017-11-05 01:42:08 +00:00
|
|
|
$this->client = $client;
|
2017-01-13 23:15:08 +00:00
|
|
|
$this->router = $router;
|
2017-11-05 01:42:08 +00:00
|
|
|
$this->token = $telegramToken;
|
|
|
|
$this->maxConnections = $telegramWebhookMaxConnections;
|
2017-01-13 23:15:08 +00:00
|
|
|
}
|
|
|
|
|
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'))) {
|
|
|
|
|
2017-01-13 23:15:08 +00:00
|
|
|
$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;
|
2017-01-13 23:15:08 +00:00
|
|
|
$setWebHook->max_connections = $this->maxConnections;
|
2017-01-04 22:36:30 +00:00
|
|
|
|
2017-01-13 23:15:08 +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();
|
|
|
|
|
2017-01-13 23:15:08 +00:00
|
|
|
$this->client->performApiRequest($deleteWebHook);
|
2017-01-04 22:36:30 +00:00
|
|
|
|
|
|
|
$output->writeln('Done');
|
|
|
|
} else {
|
2017-01-13 23:15:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|