Slack Chatbot with Persistent Memory and OpenRouter - n8n Workflow

Build a powerful, context-aware Slack AI chatbot using this advanced n8n workflow. Integrate OpenRouter, persistent Postgres memory, and a 'thinking' UI for superior user experience. Get this complete n8n template now.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Developers and Integrators: Seeking advanced n8n templates for custom Slack apps.
Team Managers: Who need a powerful, private AI assistant integrated directly into their collaboration platform.
Automation Specialists: Interested in utilizing LangChain n8n node functionality for complex AI agents.
Anyone: Looking for an example of a full conversational loop in an n8n workflow using external memory.

Overview

This robust n8n workflow template solves the challenge of creating a truly conversational AI assistant within Slack. Unlike simple notification bots, this architecture utilizes a dedicated Postgres database for persistent chat memory, ensuring the AI remembers previous statements within a specific Slack thread. The automation starts with an n8n trigger listening for messages. Before processing, it sends an immediate HTTP request to Slack to display a "thinking..." status, significantly enhancing the user experience. This advanced n8n workflow leverages the LangChain Agent n8n node, powered by OpenRouter, providing excellent flexibility in LLM choice and reliable performance. This is a crucial n8n workflow for teams needing reliable, stateful AI integration using OpenRouter and Postgres to manage context.

How it Works

The entire process is initiated by the "On Message Received" n8n trigger, which listens for incoming Slack messages. The flow immediately moves to the "Check If User" n8n node, which filters out messages from bots, ensuring the text field exists, and confirming the event type is a message, effectively preventing feedback loops.

If the message is valid, the n8n workflow executes two paths concurrently:


  1. Thinking UI: An HTTP Request n8n node ("Set Thinking Status") instantly sends a request to the Slack API to activate the "is thinking..." visual indicator in the thread, providing immediate feedback to the user.

  2. AI Processing: The message is passed to the "AI Agent" n8n node. This n8n node utilizes the "OpenRouter Chat Model" for LLM inference and, critically, the "Postgres Chat Memory" n8n node. The Postgres n8n node uses the Slack message's thread_ts (thread timestamp) as the session key, guaranteeing that the AI only uses the context of the current thread for its response.

Once the AI Agent calculates the optimal output, the n8n workflow concludes by running the "Send Reply" Slack n8n node, which posts the full, markdown-formatted AI response back into the original Slack thread.

Installation Guide


  1. Import the n8n Workflow: Copy the provided JSON and import it into your n8n instance via the 'Workflows' menu.

  2. Slack Credentials: You need a Slack API credential configured for your application. This must be applied to both the "On Message Received" n8n trigger and the final "Send Reply" n8n node.

  3. Postgres Setup: Configure a Postgres credential pointing to your database. Ensure the database user has permissions to create tables, as the 'Postgres Chat Memory' n8n node will create the chat_histories table if it doesn't exist.

  4. OpenRouter Setup: Provide your OpenRouter API key to the "OpenRouter Chat Model" n8n node.

  5. Bearer Token: The "Set Thinking Status" HTTP Request n8n node requires a custom HTTP Bearer Token credential. This token must be a Slack User OAuth Token with chat:write and chat:write.customize scopes, needed to interact with the special assistant.threads.setStatus API endpoint.

  6. Channel ID: Update the "On Message Received" n8n trigger to include the specific Channel ID where your Slack app is configured to listen for messages.

Node Details

On Message Received (Slack Trigger): This is the starting n8n trigger. It listens for message events in a specified channel ID. This crucial n8n trigger ensures the workflow only runs when relevant activity occurs.
Check If User (If): A core logic n8n node that filters the incoming data. It validates that the message did not come from a bot (botid is null) and is a standard message type, protecting the n8n workflow from recursive calls.
Set Thinking Status (HTTP Request): This n8n node makes a POST request to slack.com/api/assistant.threads.setStatus. It provides immediate visual feedback to the user by setting the thread status to 'is thinking...'.
AI Agent (LangChain Agent n8n node): The brain of the operation. It receives the input text and is configured with a system message emphasizing helpful, markdown-formatted responses. This n8n node orchestrates the LLM and memory components.
OpenRouter Chat Model (LangChain LM n8n node): Provides the language model capabilities to the Agent. Using OpenRouter allows access to various state-of-the-art models.
Postgres Chat Memory (LangChain Memory n8n node): Handles statefulness. It uses Postgres to store conversation history, keyed by the Slack message's thread
ts. This ensures context is retained between messages within the same thread, a key feature of this n8n template.


  • Send Reply (Slack): The final action n8n node. It sends the AI's generated markdown response ($json.output) back to the originating channel, specifying the thread timestamp to keep the conversation structured.

Related n8n Workflows

Free

Nodes: 9 Nodes
Updated: December 26 2025
View all
Created by

👋 Hi, I'm James. I run an AI automation agency focused on building small, focused workflows that have a direct and measurable impact on business growth or optimization.

Featured*