Skip to content

Commit a832671

Browse files
sielvermpociot
authored andcommitted
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
1 parent 3da3b8b commit a832671

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/SlackDriver.php

+29
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use BotMan\BotMan\Messages\Attachments\Image;
1313
use BotMan\BotMan\Messages\Outgoing\Question;
1414
use Symfony\Component\HttpFoundation\Request;
15+
use BotMan\BotMan\Drivers\Events\GenericEvent;
1516
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\HttpFoundation\JsonResponse;
1718
use Symfony\Component\HttpFoundation\ParameterBag;
@@ -82,6 +83,34 @@ public function matchesRequest()
8283
return ! is_null($this->event->get('user')) || ! is_null($this->event->get('team_domain')) || ! is_null($this->event->get('bot_id'));
8384
}
8485

86+
/**
87+
* Determine whether a non-message event is matching the current request, and if so,
88+
* build and return a GenericEvent instance from it.
89+
*
90+
* @return GenericEvent|bool|mixed A GenericEvent instance, or the result from
91+
* the parent class' method (likely false).
92+
*/
93+
public function hasMatchingEvent()
94+
{
95+
// Retrieve the 'event' part of the payload
96+
$eventData = $this->payload->get('event');
97+
98+
// If the event type isn't 'message' (which should go through BotMan::hears),
99+
// build a GenericEvent and return it
100+
if (isset($eventData['type']) && $eventData['type'] !== 'message') {
101+
$eventPayload = json_encode($eventData);
102+
if ($eventPayload !== false) {
103+
$event = new GenericEvent($eventPayload);
104+
$event->setName($eventData['type']);
105+
106+
return $event;
107+
}
108+
}
109+
110+
// Otherwise, fall back to the parent implementation
111+
return parent::hasMatchingEvent();
112+
}
113+
85114
/**
86115
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $message
87116
* @return \BotMan\BotMan\Messages\Incoming\Answer

0 commit comments

Comments
 (0)