-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_notification.exs
75 lines (61 loc) · 1.58 KB
/
slack_notification.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Mix.install([
{:tesla, "~> 1.4"},
{:hackney, "~> 1.18"},
{:jason, "~> 1.4"}
])
defmodule Slack do
@moduledoc """
This modules provide helper functions to send notification to Slack channels
"""
use Tesla
@token "PLACE_YOUR_TOKEN_HERE"
@default_channel "#example"
@notification_payload %{
"username" => "elixir-bot",
"icon_emoji" => ":robot_face:"
}
@doc """
Send an alert with simple or advance formatting
## Examples
iex> Slack.send_alert("Hello World", "#temp")
:ok
iex> Slack.send_alert(["*Hello World*", "`Mix.install()` is just awesome. Slack <3 Markdown!"])
:ok
"""
@spec send_alert(
text_or_blocks :: String.t() | [String.t()],
channel :: String.t()
) :: :ok
def send_alert(text_or_blocks, channel \\ @default_channel)
def send_alert(text_or_blocks, channel) when is_list(text_or_blocks) do
blocks = transform_blocks(text_or_blocks)
@notification_payload
|> Map.put("blocks", blocks)
|> Map.put("channel", channel)
|> send_notification()
:ok
end
def send_alert(text, channel) do
@notification_payload
|> Map.put("text", text)
|> Map.put("channel", channel)
|> send_notification()
:ok
end
defp send_notification(payload) do
url = "https://hooks.slack.com/services/" <> @token
body = Jason.encode!(payload)
{:ok, _response} = Tesla.post(url, body)
end
defp transform_blocks(blocks) do
Enum.map(blocks, fn text ->
%{
type: "section",
text: %{
type: "mrkdwn",
text: text
}
}
end)
end
end