Files
max-bot-test/src/Command/SubscribeWebhookCommand.php
pkirillw bd5304ec5e
All checks were successful
Deploy Max Bot Test / deploy (push) Successful in 12s
feat(subscribe): --prune flag removes all stale webhooks first
Pipeline uses --prune so old tuna/test subscriptions don't linger after deploy.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-18 00:44:40 +03:00

71 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Command;
use App\Bootstrap;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(name: 'webhook:subscribe', description: 'POST /subscriptions — register this bot webhook with MAX.')]
final class SubscribeWebhookCommand extends AbstractCommand
{
protected function configure(): void
{
$this->addOption('url', 'u', InputOption::VALUE_OPTIONAL, 'Override WEBHOOK_URL from env');
$this->addOption('secret', 's', InputOption::VALUE_OPTIONAL, 'Override MAX_BOT_API_SECRET from env');
$this->addOption('types', 't', InputOption::VALUE_OPTIONAL, 'Comma-separated update types (default: empty = all)');
$this->addOption('prune', null, InputOption::VALUE_NONE, 'Unsubscribe every other webhook before subscribing');
}
protected function doExecute(SymfonyStyle $io, InputInterface $input, OutputInterface $output): int
{
$config = Bootstrap::envConfig();
$url = (string) ($input->getOption('url') ?: $config['webhook.url']);
$secret = (string) ($input->getOption('secret') ?: $config['bot.secret']);
$typesArg = $input->getOption('types');
$types = $typesArg === null ? [] : array_values(array_filter(explode(',', (string) $typesArg)));
if ($url === '' || str_contains($url, 'your-tunnel-host')) {
$io->error('WEBHOOK_URL is empty or still default. Run `make logs-tunnel` to read the cloudflared URL and put it in .env.');
return self::FAILURE;
}
if ($secret === '') {
$io->error('MAX_BOT_API_SECRET is empty. Generate one (e.g. `openssl rand -hex 32`) and put in .env.');
return self::FAILURE;
}
if ($input->getOption('prune')) {
$existing = $this->api->subscriptions->getSubscriptions();
foreach ($existing->subscriptions as $s) {
if ($s->url !== $url) {
$io->text("Pruning stale webhook: $s->url");
$this->api->subscriptions->unsubscribe($s->url);
}
}
}
$io->text("Subscribing $url with " . ($types === [] ? 'all update types' : 'types: ' . implode(',', $types)));
$result = $this->api->subscriptions->subscribe($url, $types, $secret);
if (!$result->success) {
$io->error('Subscribe failed: ' . ($result->message ?? 'unknown'));
return self::FAILURE;
}
$io->success('Subscribed. Verifying via getSubscriptions()…');
$subs = $this->api->subscriptions->getSubscriptions();
$rows = [];
foreach ($subs->subscriptions as $s) {
$rows[] = [$s->url, $s->version ?? '—', implode(',', $s->updateTypes) ?: '(all)'];
}
$io->table(['url', 'version', 'update_types'], $rows);
return self::SUCCESS;
}
}