Getting Started
View SourceThis guide walks you through creating a Slack bot from scratch—configuring a Slack App, obtaining tokens, and running your first handler.
Prerequisites
- Elixir 1.17 or later
- A Slack workspace where you have permission to install apps
- Access to api.slack.com
1. Create a Slack App
- Go to api.slack.com/apps and click Create New App.
- Choose From scratch, give it a name (e.g., "MyBot"), and select your workspace.
- You'll land on the app's Basic Information page.
2. Enable Socket Mode
Socket Mode lets your bot receive events over a WebSocket instead of exposing a public HTTP endpoint.
- In the left sidebar, click Socket Mode.
- Toggle Enable Socket Mode on.
- You'll be prompted to generate an App-Level Token. Give it a name like
socket-tokenand add theconnections:writescope. - Copy the token (it starts with
xapp-). This is yourSLACK_APP_TOKEN.
3. Add Bot Scopes
- In the sidebar, go to OAuth & Permissions.
- Scroll to Scopes → Bot Token Scopes and add the scopes your bot needs. At minimum:
chat:write— send messagescommands— receive slash commands (if you plan to use them)channels:read— read channel metadata (for the cache sync)
- If you want your bot to respond to messages or mentions, add
app_mentions:readand/orchannels:history.
4. Install the App
- Still on OAuth & Permissions, scroll up and click Install to Workspace.
- Authorize the app.
- Copy the Bot User OAuth Token (starts with
xoxb-). This is yourSLACK_BOT_TOKEN.
5. Subscribe to Events
If your bot needs to react to messages, mentions, or other events:
- Go to Event Subscriptions in the sidebar.
- Toggle Enable Events on. (Socket Mode handles delivery, so you won't need a Request URL.)
- Under Subscribe to bot events, add events like:
message.channels— messages in public channels the bot is inapp_mention— when someone @mentions your bot
- Save changes.
6. Create a Slash Command (optional)
- Go to Slash Commands in the sidebar.
- Click Create New Command.
- Fill in the command (e.g.,
/demo), a short description, and usage hint. - Save. Slack will deliver slash-command payloads over the Socket Mode connection.
7. Add SlackBot to Your Project
In your mix.exs:
def deps do
[
{:slack_bot_ws, "~> 0.1.0"}
]
endRun:
mix deps.get
8. Scaffold with Igniter (optional)
If you have Igniter in your project:
mix slack_bot_ws.install
This creates a bot module, config stub, and supervision wiring. Skip to step 11 if you use this.
9. Define Your Bot Module
Create lib/my_app/slack_bot.ex:
defmodule MyApp.SlackBot do
use SlackBot, otp_app: :my_app
# Respond to @mentions
handle_event "app_mention", event, _ctx do
MyApp.SlackBot.push({"chat.postMessage", %{
"channel" => event["channel"],
"text" => "Hi <@#{event["user"]}>! I heard you."
}})
end
endThe use SlackBot, otp_app: :my_app macro:
- Injects the DSL (
handle_event,slash,middleware) - Tells SlackBot to read configuration from
:my_appapplication env
10. Configure Tokens
In config/config.exs:
config :my_app, MyApp.SlackBot,
app_token: System.fetch_env!("SLACK_APP_TOKEN"),
bot_token: System.fetch_env!("SLACK_BOT_TOKEN")Or in config/runtime.exs if you prefer runtime configuration.
11. Supervise the Bot
In your application supervisor (lib/my_app/application.ex):
def start(_type, _args) do
children = [
MyApp.SlackBot
]
Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)
end12. Run
Set your environment variables and start:
export SLACK_APP_TOKEN="xapp-..."
export SLACK_BOT_TOKEN="xoxb-..."
iex -S mix
Invite your bot to a channel (/invite @MyBot) and mention it. You should see a reply.
Adding a Slash Command Handler
If you created a /demo command in step 6, add a handler:
defmodule MyApp.SlackBot do
use SlackBot, otp_app: :my_app
slash "/demo" do
value :action
handle payload, _ctx do
action = payload["parsed"][:action] || "nothing"
MyApp.SlackBot.push({"chat.postMessage", %{
"channel" => payload["channel_id"],
"text" => "You asked me to: #{action}"
}})
end
end
handle_event "app_mention", event, _ctx do
MyApp.SlackBot.push({"chat.postMessage", %{
"channel" => event["channel"],
"text" => "Hi <@#{event["user"]}>!"
}})
end
endTry /demo deploy in Slack. The handler receives %{action: "deploy"} in payload["parsed"].
What's Running Under the Hood
When your supervisor starts MyApp.SlackBot, SlackBot:
- Reads configuration from
:my_appapp env and validates tokens - Starts an HTTP pool for Web API requests
- Starts the ETS-backed cache and event buffer
- Calls
apps.connections.opento get a WebSocket URL - Opens the Socket Mode connection
- Spawns supervised tasks for each incoming event
If the connection drops, SlackBot reconnects with exponential backoff. If Slack returns rate-limit headers, the rate limiter pauses outbound requests until the window passes.
Next Steps
- Slash Grammar Guide — build complex command parsers
- Rate Limiting Guide — understand how tier-aware limiting works
- Diagnostics Guide — capture and replay events
- Telemetry Guide — integrate with LiveDashboard
The examples/basic_bot/ directory contains a full working bot demonstrating middleware, advanced grammars, and diagnostics replay.