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

113 lines
2.8 KiB
PHP
Raw Normal View History

2017-01-04 22:36:30 +00:00
<?php
namespace Skobkin\Bundle\PointToolsBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
2017-01-04 22:36:30 +00:00
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2017-01-04 22:36:30 +00:00
use unreal4u\TelegramAPI\Telegram\Methods\DeleteWebhook;
use unreal4u\TelegramAPI\Telegram\Methods\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 ContainerAwareCommand
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 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;
}
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;
}
}