Skip to main content
Build a Slack bot with tools, memory, and slash commands.
1

Create the Slack app

  1. Go to api.slack.com/apps and click Create New App
  2. Enable Socket Mode and generate an App-Level Token (xapp-)
  3. Add bot scopes: chat:write, app_mentions:read, channels:history, im:history
  4. Subscribe to events: message.im, app_mention
  5. Install the app and copy the Bot Token (xoxb-)
2

Create the agent

slack_bot.py
import asyncio
import os
from definable.agent import Agent
from definable.agent.interface import SlackInterface
from definable.memory import Memory, SQLiteStore
from definable.tool.decorator import tool

@tool
def search_docs(query: str) -> str:
    """Search documentation for an answer."""
    return f"Found results for: {query}"

agent = Agent(
    model="gpt-4o",
    instructions="You are a helpful Slack assistant.",
    tools=[search_docs],
    memory=Memory(store=SQLiteStore("./slack_memory.db")),
)

interface = SlackInterface(
    agent=agent,
    bot_token=os.environ["SLACK_BOT_TOKEN"],
    app_token=os.environ["SLACK_APP_TOKEN"],
    slash_commands={
        "/ask": "Ask the AI assistant a question",
    },
    done_reaction="white_check_mark",
)

asyncio.run(interface.serve_forever())
3

Install dependencies

pip install 'definable[slack]'
4

Set environment variables

export SLACK_BOT_TOKEN="xoxb-..."
export SLACK_APP_TOKEN="xapp-..."
export OPENAI_API_KEY="sk-..."
5

Run

python slack_bot.py
Mention the bot in a channel or send a DM. Use /ask for slash commands.

Production Deployment

Switch to HTTP Events API mode for production:
interface = SlackInterface(
    agent=agent,
    bot_token=os.environ["SLACK_BOT_TOKEN"],
    signing_secret=os.environ["SLACK_SIGNING_SECRET"],
    mode="http",
)
See Slack reference for all configuration options including Block Kit interactions, modals, and shortcuts.