LINE Chatbot with Persistent Memory using Google Sheets and Gemini AI - n8n Workflow

Deploy a powerful LINE chatbot using this robust n8n workflow. It integrates Google Gemini for advanced conversational intelligence and utilizes Google Sheets for persistent conversation memory management.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

AI Developers: Individuals building custom, stateful chatbots requiring persistent memory across sessions.
E-commerce/Support Teams: Businesses needing an intelligent, custom LINE response system.
n8n Users: Those looking for advanced n8n templates demonstrating complex control flow, API interaction, and Google Sheets manipulation.
Automation Specialists: Experts seeking to deploy reliable, high-volume chatbot solutions using n8n.

Overview

Building a truly useful chatbot requires memory, especially on platforms like LINE where context is key. This sophisticated n8n workflow solves the problem of stateless API interactions by using Google Sheets as a low-cost, high-capacity external memory store.

When a message is received via the LINE platform, the n8n trigger instantly pulls the user's past conversation history from Google Sheets. This history is then injected into the prompt provided to the Google Gemini AI Model, allowing the chatbot, named 'ลลิตา', to respond accurately and contextually, even across long periods. After the response is generated, a critical custom JavaScript n8n node manages the history size, archiving older conversation chunks to prevent Google Sheets cell overflow before saving the updated record. This comprehensive approach makes this n8n template exceptionally powerful for conversational AI deployment.

How it Works

This powerful n8n workflow executes the following steps:


  1. Incoming Message Trigger: The automation starts with the Webhook n8n trigger, which is configured to listen for incoming messages from the LINE Official Account API.

  2. Data Extraction: The Edit Fields n8n node extracts the necessary data from the payload, including the userId, the user's message text, and the essential replyToken needed for the response.

  3. Memory Retrieval: The Get History Google Sheets n8n node uses the userId to look up and retrieve all existing conversation history and archives stored in the spreadsheet.

  4. Prompt Compilation: The Prepare Prompt n8n node dynamically generates the AI prompt. It prepends the system instruction, concatenates all retrieved history archives, and adds the newest user message, ensuring the AI has full context.

  5. AI Generation: The assembled prompt is passed to the AI Agent n8n node, which utilizes the Google Gemini Chat Model to generate a contextual, Thai-language response.

  6. History Management (The Core Logic): The Split History Code n8n node combines the old history, the user input, and the AI output. This custom script checks the length of the new combined history against a safe threshold (35,000 characters) and automatically segments and archives older parts into dedicated archive columns (HistoryArchive1 to 4) in Google Sheets to maintain optimal performance and prevent data loss.

  7. Memory Update: The Save History Google Sheets n8n node updates the user's row in the spreadsheet with the latest, properly segmented history data.

  8. Final Response: The HTTP Request n8n node sends the AI's generated text response back to the LINE platform using the replyToken, completing the lifecycle of this sophisticated n8n workflow.

Installation Guide

To import and run this n8n workflow template, follow these steps:


  1. Import: Copy the JSON data provided and paste it into your n8n instance using the 'Import Workflow' function.

  2. Webhook Setup: Open the Webhook n8n trigger node. Note the unique URL path (/guitarpa) and configure your LINE Official Account settings to send message events to the full resulting Webhook URL.

  3. Google Sheets Setup: You need a Google Sheet with columns including UserID, History, LastUpdated, and four archive columns (HistoryArchive1, etc.). Establish the Google Sheets OAuth2Api credential in n8n.

  4. Google Gemini Setup: Configure the required Google Palm/Gemini API credential and link it to the Google Gemini Chat Model n8n node.

  5. LINE API Authorization: Open the HTTP Request n8n node. Update the Authorization header parameter with your LINE Messaging API Access Token (Bearer [YOUR_TOKEN]).

  6. Activation: Ensure all credentials are set and activate the n8n workflow.

Node Details

Webhook (n8n trigger): The entry point of the n8n workflow. It is configured to listen for POST requests from the LINE service on a specific path (/guitarpa).
Google Sheets (Get History): Functions as the memory retrieval mechanism. It uses the incoming userId as the lookup value to fetch the entire conversation context (History and Archives) for that specific user. Key Configuration: Uses lookupColumn: 'UserID '.
Prepare Prompt (Set n8n node): Essential data manipulation node that compiles the system instructions, all archived history items, and the latest user message into a single, comprehensive string input for the AI.
Google Gemini Chat Model: This powerful n8n node connects to the Gemini API (models/gemini-2.0-flash-001), providing the generative intelligence for the chatbot.
AI Agent: Manages the LLM interaction. It takes the prepared prompt and the configured Gemini model to generate a response. Key Configuration: Uses a defined system message, setting the bot's name ('ลลิตา') and context.
Split History (Code n8n node): This custom JavaScript n8n node manages memory persistence. It checks if the updated history exceeds 35,000 characters and handles the archival process, moving older segments into History_Archive fields before the data is saved.
Google Sheets (Save History): Used for persistence. It updates the user's row in Google Sheets with the newly segmented history and archives, ensuring continuity for the next run of this n8n workflow.
HTTP Request: Sends the final AI response back to the LINE API (https://api.line.me/v2/bot/message/reply) using the crucial replyToken extracted earlier. This is the final action of the n8n workflow.

Related n8n Workflows

Free

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

Working as a marketer in a bilingual school in Phuket with no coding background. I am a lazy person so I constantly looking for a way to make me work less. Been learning about AI stuff for around 2 years now.

Featured*