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 =
[
token: token,
channel: channel,
thread_ts: opts[:thread]
]
|> Enum.reject(fn {_, v} -> is_nil(v) end)
|> Map.new()
HTTPX.post(
"https://slack.com/api/chat.postMessage",
{:urlencoded, %{"text" => message}},
params: params
)
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) end)
|> Map.new()
HTTPX.post(
"https://slack.com/api/files.upload",
{:multipart, %{"file" => {:binfile, data}}},
params: params
)
end
end