oliverbusk
2 months ago

Integrate different sources of notifications

Posted 2 months ago by oliverbusk

Notification Integrations

I am struggling a bit with how to design a flexible/dynamic notification system in my Laravel 5.8 web application. Consider that I have something called streams. Each streams can have many fields - and these fields ultimately contains a string.

Now, I want to be able from within my web application to dispatch all the fields on a stream to various 3rd party sources that my users can set up integrations to, such as:

  • Simple webhooks
  • Zapier
  • Microsoft Flow
  • Emails

I am unsure on how to design the database to allow for this, as well as the code.

My initial thoughts was just to create a table for each type of integration, and then check if a Stream have any integrations defined.

webhook_integrations

id | stream_id | url                | fields
1  | 3         | www.external.com   | 1, 3, 5, 10
2  | 5         | www.other.com      | 2, 4, 5, 10

I then imagine I can use it like this:

$stream = Stream::Find(3);

//If the stream have any webhooks defined, fire them off.
if($stream->webhooks)
{
    foreach($stream->webhooks as $webhook)
    {
        //Fire off each webhook here!
        $FireWebhook = new Webhook();
        return $FireWebhook->url($webhook->url)->payload($webhook->fields);
    }
}

Please note the above code is simply dummy code, and is just how I imagined firing off webhooks for a specific resource can be carried out.

Now imagine if I wants to introduce another integration into my web application. For example Zapier that uses a Oauth 2.0 authenticaion layer.

A database design for that could for example be:

zapier_integrations

id | stream_id | client_id | client_secret | auth_url | token_url | refresh_url | fields

But now, I have to check if my code if I need to send a notification to my Zapier integration:

$stream = Stream::Find(3);

//If the stream have any webhooks defined, fire them off.
if($stream->webhooks)
{
    foreach($stream->webhooks as $webhook)
    {
        //Fire off each webhook here!
        $FireWebhook = new Webhook();
        return $FireWebhook->url($webhook->url)->payload($webhook->fields);
    }
}

if($stream->zapierWebhooks)
{
    foreach($stream->zapierWebhooks as $zapierWebhook)
    {
        //Fire off each webhook here!
        $zapierWebhook = new zapierWebhook();
        return $zapierWebhook->payload($webhook->fields);
    }
}

(And so on.. if I example decide to add another notification integration - for example: sms, email, slack).

I am very unsure on how to go about designing such a setup. How can I allow for different type of notification endpoints (webhook, Zapier, SMS, Email and so on), and check against all these notification types for a specific resource - and if any exist, dispatch it?

Please sign in or create an account to participate in this conversation.