Ravenx
Hex: https://hex.pm/packages/ravenx
GitHub: https://github.com/acutario/ravenx
Ravenx is a notification dispatcher that allows to simplify the sending of notifications to external services, even if you need to send them using multiple providers.
The library has two main methods: the dispatch
that returns the final state of the process and dispatch_async
which just returns the reference of the %Task{}
without waiting the response.
Each notification is defined by three thinhs: the strategy
(or service) to use, the payload
map (which contains the data used to build the notification) and the options
map, which include configuration for the service provider, or similar.
In case you need to notify something using multiple methods (or strategies
), you can create a Notification
module that specify each notification sent.
Example of simple notification
You can just send a notification synchronously:
{:ok, response} = Ravenx.dispatch(:slack, %{title: "Hello world!", body: "Science is cool!"})
Or asynchronously:
{:ok, %Task{}} = Ravenx.dispatch_async(:slack, %{title: "Hello world!", body: "Science is cool!"})
Example of complex notification
Given this example notification module:
defmodule YourApp.Notification.NotifyUser do
use Ravenx.Notification
def get_notifications_config(user) do
# In this function you can define which strategies use for your user (or
# whatever you want to pass as argument) and return something like:
[
slack: {:slack, %{title: "Important notification!", body: "Wait..."}, %{channel: user.slack_username}},
email_user: {:email, %{subject: "Important notification!", html_body: "<h1>Wait...</h1>", to: user.email_address}},
email_company: {:email, %{subject: "Important notification about an user!", html_body: "<h1>Wait...</h1>", to: user.company.email_address}},
other_notification: {:invalid_strategy, %{text: "Important notification!"}, %{option1: value2}},
]
end
end
You can just tell Ravenx to dispatch it synchronously:
YourApp.Notification.NotifyUser.dispatch(user)
And it will spawn the different processes and return the response for each in just one call:
[
slack: {:ok, ...},
email_user: {:ok, ...},
email_company: {:ok, ...},
other_notification: {:error, {:unknown_strategy, :invalid_strategy}}
]