How to Build an AI-Powered Slack Bot with n8n
By Abhishek Patoliya · 2026-08-12
A Slack bot that only replies with generic text is a novelty. One that can look up an order status, check a policy document, or file a request on someone's behalf is an actual internal tool. The difference comes down to giving n8n's AI Agent node real tools to work with, and wiring it correctly into Slack's message events — this guide walks through both.
Setting Up the Slack App
Before any n8n configuration, create a Slack app through Slack's developer portal (api.slack.com/apps) with these scopes at minimum: app_mentions:read, chat:write, and im:history if you want it to handle direct messages. Enable Event Subscriptions and point them at n8n's webhook URL for your Slack Trigger node. Install the app to your workspace and grab the Bot User OAuth Token — that's what n8n's Slack credential needs.
The Trigger: Responding Only When Addressed
Use n8n's Slack Trigger node configured for app_mention events (for channel mentions) and message.im events (for DMs), rather than subscribing to every message in a channel. This keeps the bot from responding to unrelated conversation and keeps your event volume — and therefore your LLM API costs — proportional to actual requests directed at it.
The Core: AI Agent Node With Tools
Connect the incoming message text to an AI Agent node with a system prompt defining its scope — what it should help with, and explicitly what it shouldn't attempt. Give it tools that map to real actions people would otherwise ask a teammate to do manually:
- An HTTP Request tool to look up a customer or order in your CRM/order system.
- A Vector Store tool if you want it to answer questions from an internal knowledge base (policies, documentation) via RAG.
- A tool that creates a ticket or task in whatever system your team already tracks work in.
Attach a Window Buffer Memory node keyed to the Slack thread ID, so a follow-up question in the same thread retains context from earlier in the conversation.
Replying Back to Slack
Take the AI Agent's final output and post it back with a Slack node using chat.postMessage, replying in-thread (using the original message's thread timestamp) rather than as a new top-level message — this keeps the bot's conversation contained and readable, especially in busy channels.
A Concrete Example: An IT Helpdesk Bot
A common first build: an internal IT bot that employees @mention to ask things like "how do I reset my VPN access" or "is the deploy pipeline down right now." Tools might include a lookup against an internal status page API and a Vector Store tool over the IT documentation. Routine questions get answered directly from the docs; anything the agent can't confidently resolve gets a reply pointing to a specific human or channel to escalate to, rather than a guessed answer.
Keeping It Safe for a Shared Channel
- Scope tools narrowly. A bot with access to only the specific systems it needs to help with is much harder to misuse or confuse than one with broad access "just in case."
- Set a clear escalation path. The system prompt should explicitly tell the model when to say "I'm not sure, here's who to ask" instead of guessing.
- Log every interaction. Store the incoming message, the tool calls made, and the final response somewhere outside Slack's own history, so you can review how the bot is actually being used and where it's going wrong.
- Set a max iteration limit on the AI Agent node, the same as any other agent-based workflow, so a confusing request can't send it into an unproductive loop.
Handling Threaded Conversations and Follow-Up Questions
A bot that treats every message as a fresh, context-free question breaks down the moment someone asks a follow-up in the same thread — "what about last quarter instead?" makes no sense without the question that came before it. Slack's thread timestamp (thread_ts) is the key you need: when a message arrives with a thread_ts, pull the prior messages in that thread and include them as conversation history in the next AI Agent call, rather than treating the follow-up as an isolated request.
// n8n Code node — after the Slack trigger, before the AI Agent node
const threadTs = $json.thread_ts || $json.ts; // fall back to ts if this starts a new thread
const isFollowUp = Boolean($json.thread_ts);
if (isFollowUp) {
// Fetch prior messages in this thread via Slack's conversations.replies API
const history = await this.helpers.httpRequest({
url: 'https://slack.com/api/conversations.replies',
qs: { channel: $json.channel, ts: threadTs },
headers: { Authorization: `Bearer ${$env.SLACK_BOT_TOKEN}` },
});
return [{ json: { ...$json, priorMessages: history.messages.slice(0, -1) } }];
}
return [{ json: { ...$json, priorMessages: [] } }];
Keep the history window bounded — the last 5-10 messages in the thread is usually enough context without pushing token cost up on every single reply. Beyond that window, summarize rather than including full raw history, the same context-window management pattern covered in more depth elsewhere on this blog for long-running agent loops.
One more detail worth getting right: cap how far back the thread history goes in terms of raw token count, not just message count, since a thread with a few very long messages can blow past a reasonable context budget even with a small message-count cap. Truncate or summarize older messages in a long thread rather than passing the full raw text of everything, and make sure whatever summarization step you use preserves the specific facts a follow-up question is likely to reference — a name, a number, a decision — rather than a vague gist that loses exactly the detail someone's follow-up needs.
Test this specifically with a thread that's deliberately long enough to exceed your chosen window, not just short happy-path threads — that's the only way to confirm the summarization step is actually preserving what matters before real users hit it in production.
This is a five-minute check that catches a real, common failure mode before it reaches anyone actually using the bot day to day.
Revisit it again after any change to the underlying model, since a different model can summarize the same thread history differently enough to be worth re-checking.
Getting Started
Start with a bot that only answers questions from a knowledge base via RAG — no action-taking tools yet — get that reliable, then add one action-oriented tool at a time. Each new tool is one more thing the agent has to correctly decide when to use, and testing that decision-making with a smaller toolset first makes it much easier to catch mistakes before they show up in a shared channel.
For the underlying Slack node mechanics used here, see n8n Slack Node: Notifications and Workflows. Building the same kind of bot on Telegram instead? See the n8n Telegram bot guide.
Written by Abhishek Patoliya, Senior n8n Developer & Automation Architect, Surat, India. Verified n8n Creator.