2017-01-04 22:36:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Command;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
2017-01-13 23:15:08 +00:00
|
|
|
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;
|
2017-01-13 23:15:08 +00:00
|
|
|
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;
|
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-01-13 23:15:08 +00:00
|
|
|
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
|
|
|
|
2017-01-13 23:15:08 +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'))) {
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|