File: /var/www/quadcode/app/Console/Commands/CalendlyCreateWebhook.php
<?php
namespace App\Console\Commands;
use App\Helpers\CalendlyHelper;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Symfony\Component\Console\Command\Command as CommandAlias;
use Throwable;
class CalendlyCreateWebhook extends Command
{
protected $signature = 'calendly:create-webhook';
protected $description = 'Create calendly webhook';
private CalendlyHelper $calendlyHelper;
public function __construct(CalendlyHelper $calendlyHelper)
{
parent::__construct();
$this->calendlyHelper = $calendlyHelper;
}
public function handle(): int
{
if (!$this->calendlyHelper->check()) {
$this->error('CALENDLY_URL or CALENDLY_TOKEN need to set');
return CommandAlias::FAILURE;
}
$this->info('Create calendly webhook');
try {
$this->line('Get organization ID');
$response = $this->calendlyHelper->me();
if (!isset($response['resource'])) {
$this->error($response['message'] ?? 'Unexpected error');
return CommandAlias::FAILURE;
}
$currentOrganization = explode('/', $response['resource']['current_organization']);
$organizationId = $currentOrganization[count($currentOrganization) - 1];
$this->line('Organization ID for user "' . $response['resource']['name'] . '" is ' . $organizationId);
$url = in_array(App::environment(), ['local', 'dev']) ? 'http://quadcode.foach.site' : 'https://quadcode.com';
$url .= '/api/calendly/webhook';
$this->line('Set webhook address ' . $url);
$response = $this->calendlyHelper->createWebhookSubscriptions($organizationId, $url);
if (!isset($response['resource'])) {
$this->error($response['message'] ?? 'Unexpected error');
return CommandAlias::FAILURE;
}
} catch (Throwable $exception) {
$this->error($exception->getMessage());
return CommandAlias::FAILURE;
}
$this->info(PHP_EOL . 'Done');
return CommandAlias::SUCCESS;
}
}