Packages

An Elixir Slack client and bot.

Current section

Files

Jump to
slacks lib slack.ex
Raw

lib/slack.ex

defmodule Slack do
@moduledoc ~S"""
Documentation for Slacks.
"""
@doc ~S"""
Post a message to slack.
"""
@spec post_message(String.t(), String.t(), String.t(), Keyword.t()) :: any
def post_message(token, channel, message, opts \\ []) do
params =
[
text: message,
token: token,
channel: channel,
thread_ts: opts[:thread],
blocks: opts[:blocks]
]
|> Enum.reject(fn {_, v} -> is_nil(v) or v == [] end)
|> Map.new()
HTTPX.post(
"https://slack.com/api/chat.postMessage",
{:json, params},
headers: [{"Authorization", "Bearer #{token}"}]
)
end
@doc ~S"""
Post an ephemeral message to slack.
"""
@spec post_ephemeral(String.t(), String.t(), String.t(), String.t(), Keyword.t()) :: any
def post_ephemeral(token, channel, user, message, opts \\ []) do
params =
[
text: message,
token: token,
channel: channel,
user: user,
thread_ts: opts[:thread],
blocks: opts[:blocks]
]
|> Enum.reject(fn {_, v} -> is_nil(v) or v == [] end)
|> Map.new()
HTTPX.post(
"https://slack.com/api/chat.postEphemeral",
{:json, params},
headers: [{"Authorization", "Bearer #{token}"}]
)
end
@doc ~S"""
Post a message to slack.
"""
@spec chat_update(String.t(), String.t(), String.t(), String.t(), Keyword.t()) :: any
def chat_update(token, channel, ts, message, opts \\ []) do
params =
[
text: message,
token: token,
channel: channel,
ts: ts,
thread_ts: opts[:thread],
blocks: opts[:blocks]
]
|> Enum.reject(fn {_, v} -> is_nil(v) or v == [] end)
|> Map.new()
HTTPX.post(
"https://slack.com/api/chat.update",
{:json, params},
headers: [{"Authorization", "Bearer #{token}"}]
)
end
@doc ~S"""
Upload a file to slack.
"""
@spec upload_file(String.t(), String.t(), binary, Keyword.t()) :: any
def upload_file(token, channel, data, opts \\ []) do
params =
[
token: token,
channels: channel,
thread_ts: opts[:thread],
title: opts[:title],
filetype: opts[:filetype] || "text/plain",
initial_comment: opts[:comment]
]
|> Enum.reject(fn {_, v} -> is_nil(v) or v == [] end)
|> Map.new()
HTTPX.post(
"https://slack.com/api/files.upload",
{:multipart, %{"file" => {:binfile, data}}},
params: params
)
end
end