From a832671a0bc2a42b083ebaea60ea7a4efee96045 Mon Sep 17 00:00:00 2001 From: sielver Date: Wed, 8 Aug 2018 10:29:46 +0200 Subject: [PATCH] Added hasMatchingEvent implementation (#31) * Added hasMatchingEvent implementation * Better/faster to use the strict operator for string comparison * Code style * Code style * Added json_encode return value safety check --- src/SlackDriver.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/SlackDriver.php b/src/SlackDriver.php index 1b28b7d..0524444 100644 --- a/src/SlackDriver.php +++ b/src/SlackDriver.php @@ -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; @@ -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