<?php
namespace App\EventListener;
use App\Event\ApplyWithWhatsappEvent;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class ApplyWithWhatsappEventListener
{
/**
* @var Client
*/
private $client;
/**
* @var ParameterBagInterface
*/
private $parameterBag;
public function __construct(ParameterBagInterface $parameterBag)
{
$this->parameterBag = $parameterBag;
$this->client = new Client([
'base_uri' => $this->parameterBag->get('site_apply_with_whatsapp_url'),
]);
}
public function onAppliedWithWhatsapp(ApplyWithWhatsappEvent $event)
{
if (!$this->checkRequiredParameters()) {
return;
}
try {
$this->client->post('whatsapp', [
RequestOptions::JSON => [
'chatbot' => $this->parameterBag->get('site_apply_with_whatsapp_chatbot'),
'token' => 'Token '.$this->parameterBag->get('site_apply_with_whatsapp_token'),
'flow' => $this->parameterBag->get('site_apply_with_whatsapp_flow_id'),
'phone_number' => $event->getApplyWithWhatsapp()->getPhone(),
'template_name' => $this->parameterBag->get('site_apply_with_whatsapp_template_name'),
'template_params' => ['1' => []],
],
]);
} catch (Exception $exception) {
}
}
private function checkRequiredParameters(): bool
{
if (empty($this->parameterBag->get('site_apply_with_whatsapp_url'))) {
return false;
}
if (empty($this->parameterBag->get('site_apply_with_whatsapp_chatbot'))) {
return false;
}
if (empty($this->parameterBag->get('site_apply_with_whatsapp_token'))) {
return false;
}
if (empty($this->parameterBag->get('site_apply_with_whatsapp_flow_id'))) {
return false;
}
if (empty($this->parameterBag->get('site_apply_with_whatsapp_template_name'))) {
return false;
}
return true;
}
}