|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace BushlanovDev\MaxMessengerBot\Laravel\Commands; |
| 6 | + |
| 7 | +use BushlanovDev\MaxMessengerBot\Api; |
| 8 | +use BushlanovDev\MaxMessengerBot\Enums\UpdateType; |
| 9 | +use Illuminate\Console\Command; |
| 10 | +use Illuminate\Contracts\Config\Repository as Config; |
| 11 | +use Illuminate\Support\Facades\Log; |
| 12 | +use Throwable; |
| 13 | + |
| 14 | +/** |
| 15 | + * Artisan command for subscribing to webhook updates. |
| 16 | + */ |
| 17 | +class WebhookSubscribeCommand extends Command |
| 18 | +{ |
| 19 | + /** |
| 20 | + * The name and signature of the console command. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $signature = 'maxbot:webhook:subscribe |
| 25 | + {url : The webhook URL to subscribe to} |
| 26 | + {--secret= : Secret key for webhook verification (optional)} |
| 27 | + {--types=* : Update types to subscribe to (optional)}'; |
| 28 | + |
| 29 | + /** |
| 30 | + * The console command description. |
| 31 | + * |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $description = 'Subscribe bot to webhook updates'; |
| 35 | + |
| 36 | + /** |
| 37 | + * Execute the console command. |
| 38 | + */ |
| 39 | + public function handle(Api $api, Config $config): int |
| 40 | + { |
| 41 | + $url = (string)$this->argument('url'); // @phpstan-ignore-line |
| 42 | + $secret = $this->option('secret') ?? $config->get('maxbot.webhook_secret'); |
| 43 | + $types = $this->option('types'); |
| 44 | + |
| 45 | + if (!filter_var($url, FILTER_VALIDATE_URL)) { |
| 46 | + $this->error('Invalid URL provided.'); |
| 47 | + |
| 48 | + return self::FAILURE; |
| 49 | + } |
| 50 | + |
| 51 | + $updateTypes = null; |
| 52 | + if (is_array($types) && !empty($types)) { |
| 53 | + $updateTypes = []; |
| 54 | + foreach ($types as $type) { |
| 55 | + try { |
| 56 | + $updateTypes[] = UpdateType::from($type); |
| 57 | + } catch (\ValueError $e) { |
| 58 | + $this->error("Invalid update type: $type"); |
| 59 | + |
| 60 | + return self::FAILURE; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + $this->info('Subscribing to webhook...'); |
| 66 | + $this->line("URL: $url"); |
| 67 | + if ($secret) { |
| 68 | + $this->line("Secret: " . str_repeat('*', strlen($secret))); |
| 69 | + } |
| 70 | + if ($updateTypes) { |
| 71 | + $this->line("Update types: " . implode(', ', array_map(fn($type) => $type->value, $updateTypes))); |
| 72 | + } else { |
| 73 | + $this->line("Update types: All (default)"); |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + $result = $api->subscribe($url, $secret, $updateTypes); |
| 78 | + |
| 79 | + if ($result->success) { |
| 80 | + $this->info('✅ Successfully subscribed to webhook!'); |
| 81 | + |
| 82 | + return self::SUCCESS; |
| 83 | + } else { |
| 84 | + $this->error('❌ Failed to subscribe to webhook.'); |
| 85 | + $this->line("Response: $result->message"); |
| 86 | + |
| 87 | + return self::FAILURE; |
| 88 | + } |
| 89 | + } catch (Throwable $e) { |
| 90 | + Log::error("Webhook subscription error: {$e->getMessage()}", [ |
| 91 | + 'exception' => $e, |
| 92 | + ]); |
| 93 | + $this->error("❌ Webhook subscription error: {$e->getMessage()}"); |
| 94 | + |
| 95 | + return self::FAILURE; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments