diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 5774516..7ce9e14 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -65,7 +65,7 @@ jobs: fi ssh -i ~/.ssh/id_rsa -p ${{ vars.SERVER_PORT }} ${{ vars.SERVER_USER }}@${{ vars.SERVER_HOST }} " cd /home/pkirillw/dockered-services/max-bot-test && \ - docker compose exec -T app bin/console webhook:subscribe" + docker compose exec -T app bin/console webhook:subscribe --prune" - name: Health check run: | diff --git a/src/Command/SubscribeWebhookCommand.php b/src/Command/SubscribeWebhookCommand.php index f461367..7127eba 100644 --- a/src/Command/SubscribeWebhookCommand.php +++ b/src/Command/SubscribeWebhookCommand.php @@ -20,6 +20,7 @@ final class SubscribeWebhookCommand extends AbstractCommand $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 @@ -39,6 +40,16 @@ final class SubscribeWebhookCommand extends AbstractCommand 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) {