1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
11
|
|
|
use unreal4u\TelegramAPI\Telegram\Methods\DeleteWebhook; |
12
|
|
|
use unreal4u\TelegramAPI\Telegram\Methods\SetWebhook; |
13
|
|
|
use unreal4u\TelegramAPI\TgLog; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Sets or deletes Telegram bot Web-Hook |
17
|
|
|
* @see https://core.telegram.org/bots/api#setwebhook |
18
|
|
|
*/ |
19
|
|
|
class TelegramWebHookCommand extends ContainerAwareCommand |
20
|
|
|
{ |
21
|
|
|
private const MODE_SET = 'set'; |
22
|
|
|
private const MODE_DELETE = 'delete'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var TgLog |
26
|
|
|
*/ |
27
|
|
|
private $client; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Router |
31
|
|
|
*/ |
32
|
|
|
private $router; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $token; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
private $maxConnections; |
43
|
|
|
|
44
|
|
|
public function setClient(TgLog $client): void |
45
|
|
|
{ |
46
|
|
|
$this->client = $client; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setRouter(Router $router): void |
50
|
|
|
{ |
51
|
|
|
$this->router = $router; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setToken(string $token): void |
55
|
|
|
{ |
56
|
|
|
$this->token = $token; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setMaxConnections(int $maxConnections) |
60
|
|
|
{ |
61
|
|
|
$this->maxConnections = $maxConnections; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
protected function configure() |
68
|
|
|
{ |
69
|
|
|
$this |
70
|
|
|
->setName('telegram:webhook') |
71
|
|
|
->setDescription('Set webhook') |
72
|
|
|
->addArgument('mode', InputArgument::REQUIRED, 'Command mode (set or delete)') |
73
|
|
|
; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
80
|
|
|
{ |
81
|
|
|
if (self::MODE_SET === strtolower($input->getArgument('mode'))) { |
82
|
|
|
|
83
|
|
|
$url = $this->router->generate( |
84
|
|
|
'telegram_webhook', |
85
|
|
|
['token' => $this->token], |
86
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$output->writeln('Setting webhook: '.$url); |
90
|
|
|
|
91
|
|
|
$setWebHook = new SetWebhook(); |
92
|
|
|
$setWebHook->url = $url; |
93
|
|
|
$setWebHook->max_connections = $this->maxConnections; |
94
|
|
|
|
95
|
|
|
$this->client->performApiRequest($setWebHook); |
96
|
|
|
|
97
|
|
|
$output->writeln('Done'); |
98
|
|
|
} elseif (self::MODE_DELETE === strtolower($input->getArgument('mode'))) { |
99
|
|
|
$output->writeln('Deleting webhook'); |
100
|
|
|
|
101
|
|
|
$deleteWebHook = new DeleteWebhook(); |
102
|
|
|
|
103
|
|
|
$this->client->performApiRequest($deleteWebHook); |
104
|
|
|
|
105
|
|
|
$output->writeln('Done'); |
106
|
|
|
} else { |
107
|
|
|
throw new \InvalidArgumentException(sprintf('Mode must be exactly one of: %s', implode(', ', [self::MODE_SET, self::MODE_DELETE]))); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return 0; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|