Skip to content

Commit

Permalink
Added hasMatchingEvent implementation (#31)
Browse files Browse the repository at this point in the history
* Added hasMatchingEvent implementation

* Better/faster to use the strict operator for string comparison

* Code style

* Code style

* Added json_encode return value safety check
  • Loading branch information
sielver authored and mpociot committed Aug 8, 2018
1 parent 3da3b8b commit a832671
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/SlackDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use BotMan\BotMan\Messages\Attachments\Image;
use BotMan\BotMan\Messages\Outgoing\Question;
use Symfony\Component\HttpFoundation\Request;
use BotMan\BotMan\Drivers\Events\GenericEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand Down Expand Up @@ -82,6 +83,34 @@ public function matchesRequest()
return ! is_null($this->event->get('user')) || ! is_null($this->event->get('team_domain')) || ! is_null($this->event->get('bot_id'));
}

/**
* Determine whether a non-message event is matching the current request, and if so,
* build and return a GenericEvent instance from it.
*
* @return GenericEvent|bool|mixed A GenericEvent instance, or the result from
* the parent class' method (likely false).
*/
public function hasMatchingEvent()
{
// Retrieve the 'event' part of the payload
$eventData = $this->payload->get('event');

// If the event type isn't 'message' (which should go through BotMan::hears),
// build a GenericEvent and return it
if (isset($eventData['type']) && $eventData['type'] !== 'message') {
$eventPayload = json_encode($eventData);
if ($eventPayload !== false) {
$event = new GenericEvent($eventPayload);
$event->setName($eventData['type']);

return $event;
}
}

// Otherwise, fall back to the parent implementation
return parent::hasMatchingEvent();
}

/**
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $message
* @return \BotMan\BotMan\Messages\Incoming\Answer
Expand Down

0 comments on commit a832671

Please sign in to comment.