Skip to content
AI Agent How-To

Home ›Build an agent that does X

Build an AI Agent That Summarizes Slack Channels

Intermediate ~7 min read Last tested November 2026

Updated 2026-11-03

A channel-summary agent pulls recent messages from Slack, compresses them with an LLM, and posts or returns a digest. This guide uses the Web API with a bot token (same app setup as Connect an AI agent to Slack), not Slack MCP. For IDE search tools, use Slack MCP server instead. Last tested 2026-11-03.

Docs used:

Prerequisites

  • A Slack app with bot token xoxb-... and scopes channels:history, channels:read, and chat:write
  • The bot invited to the channel you summarize
  • Python 3.10+, pip install slack_sdk openai
  • Optional: Build an AI agent on a schedule to run the digest daily

Steps

1. Collect channel messages

# summarize_channel.py
import os
from datetime import datetime, timezone, timedelta
from slack_sdk import WebClient

client = WebClient(token=os.environ["SLACK_BOT_TOKEN"])
CHANNEL_ID = os.environ["SLACK_CHANNEL_ID"]  # e.g. C0123456789

def fetch_recent_messages(hours: int = 24, limit: int = 200) -> list[dict]:
    oldest = (datetime.now(timezone.utc) - timedelta(hours=hours)).timestamp()
    resp = client.conversations_history(
        channel=CHANNEL_ID,
        oldest=str(oldest),
        limit=limit,
    )
    messages = resp.get("messages", [])
    messages.reverse()  # chronological for the model
    return messages

Find the channel ID in Slack: open the channel → channel details → copy ID at the bottom, or use conversations.list.

2. Flatten text for the model

def format_thread(channel: str, msg: dict) -> str:
    user = msg.get("user", "unknown")
    text = msg.get("text", "").strip()
    line = f"{user}: {text}"
    if msg.get("reply_count"):
        thread = client.conversations_replies(
            channel=channel, ts=msg["ts"], limit=50
        )
        for reply in thread.get("messages", [])[1:]:
            line += f"\n  {reply.get('user')}: {reply.get('text', '').strip()}"
    return line

def build_transcript(messages: list[dict]) -> str:
    return "\n".join(format_thread(CHANNEL_ID, m) for m in messages if m.get("text"))

Skip bot messages or join messages in production if they add noise.

3. Summarize with your LLM

from openai import OpenAI

llm = OpenAI()

def summarize(transcript: str) -> str:
    if not transcript.strip():
        return "No messages in the window."
    resp = llm.chat.completions.create(
        model="gpt-4.1-mini",
        messages=[
            {
                "role": "system",
                "content": (
                    "Summarize this Slack channel for a busy teammate. "
                    "Use bullet points: decisions, blockers, links, and open questions. "
                    "Do not invent messages not in the transcript."
                ),
            },
            {"role": "user", "content": transcript[:120000]},
        ],
    )
    return resp.choices[0].message.content

Cap transcript length and tool steps with Fix: AI agent stuck in a loop if you wrap this in a multi-tool agent.

4. Post the digest back to Slack

def post_summary(text: str) -> None:
    client.chat_postMessage(
        channel=CHANNEL_ID,
        text=f"*Daily digest*\n{text}",
    )

if __name__ == "__main__":
    msgs = fetch_recent_messages(hours=24)
    digest = summarize(build_transcript(msgs))
    post_summary(digest)

Run manually first, then schedule with cron or GitHub Actions per Build an AI agent on a schedule.

5. Optional: trigger from @mention

Reuse the Bolt handler from Connect an AI agent to Slack. On @mention, call fetch_recent_messages for that channel and reply in the thread with summarize(...).

6. Deploy

Run on a small VPS or container with outbound HTTPS. Railway and similar hosts work for a daily cron wrapper around this script.

Common errors

not_in_channel. Invite the bot to the channel before conversations.history.

missing_scope. Add channels:history (public) or groups:history (private) and reinstall the app to the workspace.

Empty digest but messages exist. oldest timestamp excludes recent posts if your clock is wrong, or limit is too low for busy channels. Raise limit or narrow the time window.

Summary hallucinates decisions. Shorten the window, include thread replies, and instruct the model to quote usernames only from the transcript.

FAQ

Does this use Slack MCP?

No. This script uses the Slack Web API with a bot token. MCP is for clients like Cursor; see Slack MCP server.

Can it summarize private channels?

Yes, if the bot is a member and has groups:history (and related read scopes). Invite the bot to the private channel.

How is this different from the Bolt @mention bot?

The connect guide focuses on conversational replies. This guide adds batch history fetch and a digest prompt for standups or async updates.