generated from yiisoft/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetTelegramWebhookCommand.php
More file actions
134 lines (111 loc) · 4.75 KB
/
SetTelegramWebhookCommand.php
File metadata and controls
134 lines (111 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
namespace Botasis\Runtime\Console;
use Botasis\Client\Telegram\Client\ClientInterface;
use Botasis\Client\Telegram\Request\TelegramRequest;
use InvalidArgumentException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use function str_contains;
use function str_starts_with;
#[AsCommand(name: 'botasis:telegram:set-webhook', description: 'Set TG webhook address')]
final class SetTelegramWebhookCommand extends Command
{
public function __construct(
private readonly ClientInterface $client,
private readonly QuestionHelper $questionHelper,
?string $name = null
) {
parent::__construct($name);
}
protected function configure(): void
{
$this->addOption(
name: 'url',
mode: InputOption::VALUE_OPTIONAL,
description: 'HTTPS URL to send updates to. Use an empty string to remove webhook integration.',
default: ''
);
$this->addOption(
name: 'ip_address',
mode: InputOption::VALUE_OPTIONAL,
description: 'The fixed IP address which will be used to send webhook requests instead of the IP address resolved through DNS',
);
$this->addOption(
name: 'max_connections',
mode: InputOption::VALUE_OPTIONAL,
description: 'The maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Use lower values to limit the load on your bot\'s server, and higher values to increase your bot\'s throughput.',
);
$this->addOption(
name: 'allowed_updates',
mode: InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
description: 'A list of the update types you want your bot to receive. Complete list is at https://core.telegram.org/bots/api#update.',
default: [],
);
$this->addOption(
name: 'drop_pending_updates',
mode: InputOption::VALUE_NONE,
description: 'Pass this option to drop all pending updates',
);
$this->addOption(
name: 'secret_token',
mode: InputOption::VALUE_OPTIONAL,
description: 'A secret token to be sent in a header “X-Telegram-Bot-Api-Secret-Token” in every webhook request, 1-256 characters. Only characters A-Z, a-z, 0-9, _ and - are allowed. The header is useful to ensure that the request comes from a webhook set by you.',
);
}
public function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string $url */
$url = $input->getOption('url');
if ($url === '') {
/** @var bool $answer */
$answer = $this->questionHelper->ask(
$input,
$output,
new ConfirmationQuestion(
'You are about to remove webhook because --url option is not set. ' .
"Your application will no longer receive any message through Telegram webhooks.\n" .
'Are you sure? [y|N]: ',
false
),
);
if ($answer === false) {
$output->writeln('Operation canceled');
return 0;
}
}
if (str_contains($url, '://') && !str_starts_with($url, 'https://')) {
throw new InvalidArgumentException('url must not contain protocol or must start with https://');
}
if ($url !== '' && !str_starts_with($url, 'https://')) {
$url = "https://$url";
}
$fields = [
'url' => $url,
'allowed_updates' => $input->getOption('allowed_updates'),
'drop_pending_updates' => $input->getOption('drop_pending_updates'),
];
/** @var string|null $ip */
$ip = $input->getOption('ip_address');
if ($ip !== null && $ip !== '') {
$fields['ip_address'] = $ip;
}
/** @var int|null $connections */
$connections = $input->getOption('max_connections');
if ($connections !== null && $connections > 0) {
$fields['max_connections'] = $connections;
}
/** @var string|null $token */
$token = $input->getOption('secret_token');
if ($token !== null && $token !== '') {
$fields['secret_token'] = $token;
}
$this->client->send(new TelegramRequest('setWebhook', $fields));
return 0;
}
}