Send Slack Message Through Elixir (The Easy Way)

Alvin Rapada
3 min readFeb 13, 2020

Prerequisite:

  1. HTTPoison: http client for elixir
  2. A Slack App to Integrate with elixir.

Creating Slack app

Go to Your apps home page and click Create New App .

Then, name your app and select or create a workspace you want your app to integrate with.

After creating the app, you’ll be redirected to this page, click Incoming Webhooks .

Then, Active Incoming Webhooks.

After activation you will see the Webhook URls for Your Workspace , we’re gonna need to add new webhook to workspace.

Here you need to select a channel inside your workspace to post to as an app.

And finally, you were able to generate a webhook url that we will need in our elixir configuration.

To test if your webhook is working, got to your terminal and run the sample curl request .

Now let’s proceed to the elixir configuration.

Elixir Configuration

If the sample curl request works, we just need to do that using HTTPoison in our Elixir Project.

  1. Add HTTPoison to your mix.exs dependencies:
def deps do
[
{:httpoison, "~> 1.6"}
]
end

and run $ mix deps.get. Add :httpoison to your applications list.

def application do
[applications: [:httpoison]]
end

2. Define a module for your slack method/s, for this example we’re just gonna send a message to your selected slack channel.

defmodule YourElixixApp.Slack do  def send(message) do
HTTPoison.post(
"<Your WebHook URL>",
'{"text": "#{message}"}',
[{"Content-Type", "application/json"}]
)
end
end

3. And finally, in your Controller Module or any module, make sure to add send(message) to your pipeline.

alias YourElixirApp.Slackdef create(conn, %{"params" => params}) do
with {:ok, your_return} <- YourContext.yourmethod(params),
_ <- Slack.send("Hello, World!") do
...
end
end

And that's it!!! you should be able to receive a message in your slack workspace. like this.

If you find this helpful, I’d appreciate a coffee 😊

Happy Coding!!

--

--