Integration · Slack

Slack AI agent: build a workspace bot

A Slack bot is an event handler, not a chatbot window. Acknowledge within three seconds, reply in the thread, and keep the model call off the acknowledgement path.

Short answer. Create a Slack app, enable the Events API, subscribe to app_mention, add app_mentions:read and chat:write, and run a Bolt app. Reply in the thread with thread_ts, deduplicate by event_id, and call the model asynchronously after Slack has been acknowledged.

1. Create the Slack app

  1. Create an app at api.slack.com/apps.
  2. Enable Socket Mode for local development, or the Events API with a public HTTPS request URL for production.
  3. Subscribe to app_mention (and message.channels only if you need ambient messages).
  4. Add bot scopes: app_mentions:read, chat:write. Add channels:history or groups:history only if you read thread context.
  5. Install the app and store the bot token and signing secret in a secret manager.

2. Minimal Bolt app

import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from openai import OpenAI

app = App(
    token=os.environ["SLACK_BOT_TOKEN"],
    signing_secret=os.environ["SLACK_SIGNING_SECRET"],
)

client = OpenAI(
    api_key=os.environ["PLUGSKY_API_KEY"],
    base_url="https://api.plugsky.com/v1",
)

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

    answer = ask_model(question)
    say(text=answer, thread_ts=thread_ts)

def ask_model(question: str) -> str:
    response = client.chat.completions.create(
        model="plugsky-plus",
        messages=[
            {"role": "system", "content": "Answer concisely. Say when you are unsure."},
            {"role": "user", "content": question},
        ],
        temperature=0.2,
    )
    return response.choices[0].message.content

if __name__ == "__main__":
    SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()

For HTTP mode, use the Bolt request handler for your web framework and mount it at the Events API URL. Socket Mode is for development; production usually uses HTTP.

3. Acknowledge within three seconds

Slack expects an acknowledgement quickly. Bolt acknowledges the event before your handler runs, but a slow handler still blocks the worker. For a model call, enqueue the work and reply with chat.postMessage when it finishes. If the model takes 20 seconds, the user should see a “working on it” message, not silence.

4. Thread context

Use conversations.replies with the thread's ts to fetch prior messages, then trim to the last N turns and a token budget. Do not resend the entire channel history. Strip bot messages and system noise, and keep user IDs for attribution.

5. Retrieval

context = search_knowledge_base(question, top_k=5)
prompt = [
    {"role": "system", "content": "Use only the provided context. Cite the source URL."},
    {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}"},
]

If no permitted source is found, say so and offer to escalate. Do not let the model invent a policy answer in a workspace where people will act on it.

6. Deduplicate events

Slack retries event deliveries. Store event_id (or client_msg_id) with a short TTL and ignore duplicates before calling the model. Also check whether the bot already replied in the thread; a duplicate reply is worse than a missed one.

7. Escalation

  • Low confidence or no retrieval result.
  • Requests involving billing, refunds, permissions or security.
  • Repeated follow-ups from the same user.
  • The model provider is rate-limited or unavailable.

Escalate with a thread reply that names the human queue and includes the question, sources and confidence. Do not silently stop replying.

8. Rate limits and retries

Slack returns 429 with Retry-After; the model provider does too. Queue the job, respect the delay, and cap retries. If the model is down, post a short fallback message and create a ticket instead of retrying forever.

FAQ

How do I build a Slack AI bot?

Create a Slack app, enable the Events API or Socket Mode, subscribe to app_mention, and run a Bolt app that calls your model API.

Which scopes do I need?

app_mentions:read and chat:write at minimum; add history scopes only if you read thread context.

Why does Slack say the app did not respond?

Slack expects an acknowledgement within 3 seconds. Acknowledge first, do the model call asynchronously, then reply.

How do I stop duplicate replies?

Store event_id and ignore retries; check whether the bot already replied in the thread.

How do I handle rate limits?

Respect Retry-After for Slack and the model provider, queue work, cap retries and post a fallback.

Reviewed 20 November 2026. Slack scopes and Bolt APIs vary by version; check the current Slack documentation. Keep a human escalation path for permissions, billing and security requests.