Add support for multiple events
This PR adds support for handling multiple events in a single payload.
The getEvent method has been updated to support the return of either a string or array value, and the handle method updated accordingly.
Resolves https://github.com/hotmeteor/receiver/issues/13
Good PR! The only thing I am missing is that in my case the data is also different per event. Maybe you could pass the event name in the getData-method so I can figure out how to fetch the data from that specific event?
{
"time_ms": 1697717045179,
"events": [
{
"name": "channel_occupied",
"channel": "admin",
"data": {} <---
},
{
"name": "member_added",
"channel": "admin",
"data": {} <---
}
]
}
public function getData(Request $request, string $event): array
{
return $request->collect('events')
->filter(fn($item) => $item['name'] === $event)
->map(fn($item) => $item['data'])
->toArray();
}
@jeffreyvanhees Did you grab the latest version? Maybe run composer update and try again, I made that change as well.
@jeffreyvanhees Here's the README change in the PR to show you how:
https://github.com/hotmeteor/receiver/blob/2a6893c46ca2e954e9f83579455c17838e1bb483/README.md#receiving-multiple-events-in-a-single-webhook
As far as I know I have the latest version. ("hotmeteor/receiver": "dev-multi-events"). Did you test it with the JSON from the example?
I get the error Cannot assign array to property Receiver\AbstractWebhook::$event of type ?string in https://github.com/hotmeteor/receiver/blob/2a6893c46ca2e954e9f83579455c17838e1bb483/src/AbstractWebhook.php#L79 when I post the JSON from the example.
Also, can you give an example how to use the getData()-method in this case?
Note: in the new example you'd add to the readme, you use $this->request instead of the $request-argument. Is there a reason for that?