Skip to content

Commit

Permalink
add webhook event
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed May 15, 2020
1 parent 7b1f74c commit e296faf
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
22 changes: 22 additions & 0 deletions src/Command/WebHook/HandleWebHook/EventDispatcherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Sokil\Viber\Notifier\Command\WebHook\HandleWebHook;

interface EventDispatcherInterface
{
/**
* @param $eventName
* @param array $payload
*
* @return void
*/
public function dispatch($eventName, array $payload);

/**
* @param $eventName
* @param $callable
*
* @return void
*/
public function addListener($eventName, $callable);
}
38 changes: 27 additions & 11 deletions src/Command/WebHook/HandleWebHook/HandleWebHookCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@
use Sokil\Viber\Notifier\Repository\SubscribersRepositoryInterface;
use Sokil\Viber\Notifier\Command\CommandHandlerInterface;

/**
* @link https://developers.viber.com/docs/api/rest-bot-api/#callbacks
*/
class HandleWebHookCommandHandler implements CommandHandlerInterface
{
/**
* @var SubscribersRepositoryInterface
*/
private $subscriberRepository;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* @param SubscribersRepositoryInterface $subscriberRepository
*/
public function __construct(SubscribersRepositoryInterface $subscriberRepository)
{
public function __construct(
SubscribersRepositoryInterface $subscriberRepository,
EventDispatcherInterface $eventDispatcher = null
) {
$this->subscriberRepository = $subscriberRepository;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand All @@ -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':
Expand All @@ -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));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Message/RichMediaMessage/RichMediaMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(array $buttons)
{
$this->buttons = $buttons;
}

function getMinimalApiVersion()
{
return 2;
Expand Down
25 changes: 13 additions & 12 deletions src/Service/ViberClient/ViberClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/Service/ViberClient/ViberClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e296faf

Please sign in to comment.