diff --git a/src/Command/WebHook/HandleWebHook/EventDispatcherInterface.php b/src/Command/WebHook/HandleWebHook/EventDispatcherInterface.php new file mode 100644 index 0000000..8e478b1 --- /dev/null +++ b/src/Command/WebHook/HandleWebHook/EventDispatcherInterface.php @@ -0,0 +1,22 @@ +subscriberRepository = $subscriberRepository; + $this->eventDispatcher = $eventDispatcher; } /** @@ -35,11 +46,16 @@ public function handle($command) ); } - $request = $command->getWebHookServerRequestBody(); - $event = !empty($request['event']) ? $request['event'] : null; + $webHookServerRequestBody = $command->getWebHookServerRequestBody(); + $eventName = !empty($webHookServerRequestBody['event']) ? $webHookServerRequestBody['event'] : null; + + // dispatch event + if ($this->eventDispatcher) { + $this->eventDispatcher->dispatch($eventName, $webHookServerRequestBody); + } // route - switch ($event) { + switch ($eventName) { case 'webhook': case 'seen': case 'action': @@ -50,23 +66,23 @@ public function handle($command) case 'subscribed': case 'conversation_started': - if (isset($request['user']['id'])) { + if (isset($webHookServerRequestBody['user']['id'])) { $this->subscriberRepository->subscribe( - new SubscriberId($request['user']['id']), - $request['user']['name'] + new SubscriberId($webHookServerRequestBody['user']['id']), + $webHookServerRequestBody['user']['name'] ); } break; case 'unsubscribed': - if (isset($request['user_id'])) { - $this->subscriberRepository->unsubscribe(new SubscriberId($request['user_id'])); + if (isset($webHookServerRequestBody['user_id'])) { + $this->subscriberRepository->unsubscribe(new SubscriberId($webHookServerRequestBody['user_id'])); } break; default: - throw new \InvalidArgumentException(sprintf('Unsupported event "%s"', $event)); + throw new \InvalidArgumentException(sprintf('Unsupported event "%s"', $eventName)); } } } diff --git a/src/Message/RichMediaMessage/RichMediaMessage.php b/src/Message/RichMediaMessage/RichMediaMessage.php index a601c5c..0e529fa 100644 --- a/src/Message/RichMediaMessage/RichMediaMessage.php +++ b/src/Message/RichMediaMessage/RichMediaMessage.php @@ -33,7 +33,7 @@ public function __construct(array $buttons) { $this->buttons = $buttons; } - + function getMinimalApiVersion() { return 2; diff --git a/src/Service/ViberClient/ViberClient.php b/src/Service/ViberClient/ViberClient.php index daf3b84..f0dbd42 100644 --- a/src/Service/ViberClient/ViberClient.php +++ b/src/Service/ViberClient/ViberClient.php @@ -41,27 +41,28 @@ public function __construct(HttpClientInterface $httpClient, $authToken) /** * @param string $url - * + * @param array $eventTypes = null If null, subscribe to all events. Possible values (message, delivered, seen, failed, subscribed, unsubscribed, conversation_started) * @throws ViberApiRequestError */ - public function setWebHookUrl($url) + public function setWebHookUrl($url, array $eventTypes = null) { try { + $query = [ + "url" => (string)$url, + "send_name" => true, + "send_photo" => true, + ]; + + if (is_array($eventTypes)) { + $query['event_types'] = $eventTypes; + } + $response = $this->httpClient->request( self::BASE_URI . '/pa/set_webhook', [ 'X-Viber-Auth-Token' => $this->authToken, ], - [ - "url" => (string)$url, - "event_types" => [ - "subscribed", - "unsubscribed", - "conversation_started", - ], - "send_name" => true, - "send_photo" => true, - ] + $query ); } catch (\Exception $e) { throw new ViberApiRequestError( diff --git a/src/Service/ViberClient/ViberClientInterface.php b/src/Service/ViberClient/ViberClientInterface.php index 7f0bf10..bf2c849 100644 --- a/src/Service/ViberClient/ViberClientInterface.php +++ b/src/Service/ViberClient/ViberClientInterface.php @@ -20,10 +20,11 @@ interface ViberClientInterface { /** * @param string $url + * @param array|null $eventTypes If null, subscribe to all events. Possible values (message, delivered, seen, failed, subscribed, unsubscribed, conversation_started) * * @throws ViberApiRequestError */ - public function setWebHookUrl($url); + public function setWebHookUrl($url, array $eventTypes = null); /** * @param string $senderName