webhook
webhook copied to clipboard
[Enhancement Request] Multiple webhook urls per 'webhook'
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?
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));
}
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)
Hi!
Sorry for the long delay.
I like the idea of this feature. Feel free to PR :)
Would love to see this