Skip to content
Learn Agentic AI11 min read0 views

Building Slack AI Agents: Slash Commands, Bot Events, and Interactive Messages

Build a production-ready Slack AI agent with slash commands, real-time bot event handling, interactive Block Kit messages, and thread-aware conversation management using the Slack Bolt SDK.

Why Build AI Agents for Slack

Slack is where teams spend their working hours. An AI agent inside Slack meets users where they already are — no context switching, no separate dashboard. The agent can answer questions, triage requests, summarize threads, and take actions across integrated systems, all within the familiar chat interface.

The Slack Bolt SDK for Python provides a clean abstraction over Slack's Events API, slash commands, interactive components, and Socket Mode, making it the ideal foundation for AI agent development.

Setting Up the Slack App

Start by creating a Slack app at api.slack.com/apps. Enable Socket Mode for development (no public URL needed), then configure these scopes under OAuth and Permissions: app_mentions:read, chat:write, commands, im:history, and im:read.

from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler

app = App(token="xoxb-your-bot-token")

# Start listening
if __name__ == "__main__":
    handler = SocketModeHandler(
        app, "xapp-your-app-level-token"
    )
    handler.start()

Handling Slash Commands

Slash commands are the most direct way users interact with your agent. Register a command in your Slack app config, then handle it in code.

from slack_bolt import Ack, Respond

@app.command("/ask-agent")
def handle_ask_command(ack: Ack, respond: Respond, command: dict):
    ack()  # Must acknowledge within 3 seconds

    user_query = command["text"]
    user_id = command["user_id"]
    channel_id = command["channel_id"]

    # Process with AI agent (keep under 30s for respond())
    result = agent.run_sync(
        prompt=user_query,
        context={"user": user_id, "channel": channel_id}
    )

    respond(
        text=result.answer,
        response_type="in_channel",  # or "ephemeral"
    )

The critical detail: you must call ack() within 3 seconds or Slack shows an error to the user. For long-running agent tasks, acknowledge immediately, then use respond() asynchronously.

See AI Voice Agents Handle Real Calls

Book a free demo or calculate how much you can save with AI voice automation.

Listening to Bot Events

Subscribe to the app_mention and message.im events so your agent can respond when mentioned in channels or messaged directly.

import threading

@app.event("app_mention")
def handle_mention(event: dict, say, client):
    thread_ts = event.get("thread_ts", event["ts"])
    user_text = event["text"]
    channel = event["channel"]

    # Fetch thread context for multi-turn conversations
    thread_messages = []
    if event.get("thread_ts"):
        result = client.conversations_replies(
            channel=channel,
            ts=event["thread_ts"],
            limit=20,
        )
        thread_messages = [
            {"role": "user" if m.get("bot_id") is None else "assistant",
             "content": m["text"]}
            for m in result["messages"]
        ]

    agent_response = agent.run_sync(
        prompt=user_text,
        history=thread_messages,
    )

    say(text=agent_response.answer, thread_ts=thread_ts)

@app.event("message")
def handle_dm(event: dict, say):
    if event.get("channel_type") == "im" and not event.get("bot_id"):
        response = agent.run_sync(prompt=event["text"])
        say(text=response.answer)

Building Interactive Messages with Block Kit

Block Kit lets your agent present structured, interactive responses instead of plain text.

@app.command("/triage")
def handle_triage(ack, respond, command):
    ack()

    analysis = agent.run_sync(
        prompt=f"Triage this issue: {command['text']}"
    )

    blocks = [
        {
            "type": "header",
            "text": {"type": "plain_text", "text": "Issue Triage Result"}
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": f"*Summary:* {analysis.summary}\n"
                        f"*Priority:* {analysis.priority}\n"
                        f"*Category:* {analysis.category}"
            }
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {"type": "plain_text", "text": "Create Ticket"},
                    "action_id": "create_ticket",
                    "value": analysis.id,
                    "style": "primary",
                },
                {
                    "type": "button",
                    "text": {"type": "plain_text", "text": "Dismiss"},
                    "action_id": "dismiss_triage",
                    "value": analysis.id,
                },
            ]
        }
    ]

    respond(blocks=blocks, text=analysis.summary)

@app.action("create_ticket")
def handle_create_ticket(ack, body, respond):
    ack()
    analysis_id = body["actions"][0]["value"]
    ticket = create_jira_ticket(analysis_id)
    respond(
        text=f"Ticket created: {ticket.key}",
        replace_original=False,
    )

Thread Management for Multi-Turn Conversations

Keep conversation context by tracking threads. Store agent state keyed by the thread timestamp.

from collections import defaultdict

thread_contexts: dict[str, list[dict]] = defaultdict(list)

@app.event("app_mention")
def handle_threaded_mention(event, say, client):
    thread_ts = event.get("thread_ts", event["ts"])

    thread_contexts[thread_ts].append({
        "role": "user",
        "content": event["text"],
    })

    response = agent.run_sync(
        prompt=event["text"],
        history=thread_contexts[thread_ts],
    )

    thread_contexts[thread_ts].append({
        "role": "assistant",
        "content": response.answer,
    })

    say(text=response.answer, thread_ts=thread_ts)

FAQ

How do I handle Slack's 3-second acknowledgment requirement for long AI tasks?

Call ack() immediately, then spawn a background task to process the request. Use respond() with the response_url from the command payload to send the result when the agent finishes. Slack allows responses via response_url for up to 30 minutes after the original command.

Should I use Socket Mode or the Events API for production?

Socket Mode is excellent for development because it requires no public URL. For production, the Events API with a public HTTPS endpoint scales better because Slack pushes events to your server and you can load-balance across multiple instances. Socket Mode maintains a WebSocket connection per instance, which adds operational complexity at scale.

How do I prevent the agent from responding to its own messages?

Check for the bot_id field in the event payload. If event.get("bot_id") is truthy, the message came from a bot (possibly your own). Skip processing for those events to avoid infinite loops.


#Slack #BotDevelopment #SlackSDK #AIAgents #ChatIntegration #AgenticAI #LearnAI #AIEngineering

Share this article
C

CallSphere Team

Expert insights on AI voice agents and customer communication automation.

Try CallSphere AI Voice Agents

See how AI voice agents work for your industry. Live demo available -- no signup required.