receiver icon indicating copy to clipboard operation
receiver copied to clipboard

Add support for multiple events

Open hotmeteor opened this issue 2 years ago • 4 comments

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

hotmeteor avatar Oct 19 '23 12:10 hotmeteor

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 avatar Oct 19 '23 14:10 jeffreyvanhees

@jeffreyvanhees Did you grab the latest version? Maybe run composer update and try again, I made that change as well.

hotmeteor avatar Oct 19 '23 14:10 hotmeteor

@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

hotmeteor avatar Oct 19 '23 14:10 hotmeteor

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?

jeffreyvanhees avatar Oct 19 '23 18:10 jeffreyvanhees