webhook icon indicating copy to clipboard operation
webhook copied to clipboard

[Enhancement Request] Multiple webhook urls per 'webhook'

Open cjthomp opened this issue 7 years ago • 4 comments

What we need is the ability for a single notification-notifiable combo to have multiple destination webhooks.

Maybe check if routeNotificationForWebhook() returns an array and iterate through them?

cjthomp avatar Mar 28 '17 23:03 cjthomp

something like this? notifiable trait on Webhook model.

<?php
class Webhook extends Model
{
    use Notifiable;

    public function routeNotificationForWebhook()
    {
        return $this->url;
    }
}

then

$webhooks = Webhook::all();
foreach($webhooks as $wh) {
    $wh->notify(new InvoicePaid($invoice));
}

frkami123 avatar Mar 29 '17 04:03 frkami123

More that, say, $user->routeNotificationForWebhook() should be able to return an array (say the user has a slack webhook, zapier update, external server update, etc) of destinations instead of a single url.

So, say on WebhookChannel:33 you might do something like

if (! $urls = $notifiable->routeNotificationFor('Webhook')) {
    return;
}
$urls = !is_array($urls) ? [$urls] : $urls;

foreach ($urls as $url) {
    $webhookData = $notification->toWebhook($notifiable)->toArray();
    $response = $this->client->post($url, [
        'body' => json_encode(Arr::get($webhookData, 'data')),
        'verify' => false,
        'headers' => Arr::get($webhookData, 'headers'),
    ]);
    if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) {
        throw CouldNotSendNotification::serviceRespondedWithAnError($response);
    }
}

It's still not perfect, optimally you'd also be able to customize the payload and/or set custom options per endpoint, but it'd be a good step (and make it usable for us)

cjthomp avatar Mar 29 '17 14:03 cjthomp

Hi!

Sorry for the long delay.

I like the idea of this feature. Feel free to PR :)

atymic avatar Sep 16 '19 11:09 atymic

Would love to see this

HDVinnie avatar Apr 13 '21 01:04 HDVinnie