Build an AI Chatbot with Long-Term Memory and Telegram using n8n

Discover how to create a sophisticated AI chatbot using n8n that retrieves and stores long-term memories and notes in Google Docs, responds via Telegram, and maintains engaging conversations with users. This workflow automates personal memory management and note-taking for smarter interactions.
chatTrigger
agent
googleDocsTool
+8
Workflow Identifier: 1322
NODES in Use: chatTrigger, stickyNote, lmChatOpenAi, set, memoryBufferWindow, googleDocsTool, googleDocs, telegram, agent, aggregate, merge
Automate chatbot memory with n8n and Google Docs

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

What This Automation Does

This workflow helps you turn Telegram chats into smart conversations that remember important info and notes over time.
It solves the problem of forgetting past details and wasting time on manual tracking.
The result is a Telegram AI assistant that recalls memories, manages notes, and replies with personal context automatically.

The bot uses Google Docs as storage for long-term memories and notes.
It also keeps recent chats in a sliding window for smooth conversations.
By combining input from Telegram with AI powered by OpenAI, it gives useful, human-like responses.


Who Should Use This Workflow

This workflow is good for entrepreneurs, small team leads, or anyone needing smart chat assistants.
It helps users who want to save time remembering client details and managing notes inside chat.
Users who want an automatic way to track useful info shared across chat sessions will find it helpful.


Tools and Services Used

  • Telegram Bot API: Receives user messages.
  • OpenAI API: Provides AI language models for chat replies.
  • Google Docs API: Stores and retrieves long-term memories and notes.
  • n8n Automation Platform: Runs the workflow and integrates all services.
  • LangChain Nodes in n8n: Manage chat triggers, agent logic, and memory buffers.


How This Workflow Works: Input → Process → Output

Inputs

  • Messages sent by Telegram users to the bot.
  • Stored long-term memories and notes from Google Docs.
  • Recent chat context from the sliding window buffer memory.

Processing Steps

  • LangChain Chat Trigger listens for incoming chat messages.
  • Google Docs nodes fetch memories and notes separately.
  • Merge combines these two data sets.
  • Aggregate groups merged data for AI use.
  • Memory Buffer Window keeps last 50 chat messages per session.
  • LangChain Agent runs AI tools using OpenAI models, input data, and buffer memory to generate replies.
  • New memories or notes derived from the AI agent’s processing are saved back to Google Docs.

Outputs

  • The generated AI reply is sent back to Telegram users.
  • New or updated memories and notes are added to Google Docs.


Beginner Step-by-Step: How to Use This Workflow in Production

Step 1: Download and Import

  1. Click the Download button on this page to save the workflow file.
  2. Open the n8n editor.
  3. Select “Import from File” and choose the downloaded file.

Step 2: Configure Credentials

  1. Add OpenAI API Key under credentials.
  2. Set Telegram Bot Token with your bot’s API token.
  3. Authenticate Google Docs with proper OAuth credentials.

Step 3: Update Identifiers

  1. Check Google Docs nodes and update the document URLs or IDs for both memories and notes.
  2. Update Telegram chat ID if using a fixed chat target.
  3. Verify prompt or system message code in AI Tools Agent matches your desired instructions.

Step 4: Test the Workflow

  1. Send a message to the Telegram bot.
  2. Watch the workflow triggers and confirm the AI reply in Telegram.

Step 5: Activate for Production

  1. Switch the workflow toggle to Active.
  2. Monitor logs for errors.
  3. Adjust settings if needed and retest.

Optionally, consider self-host n8n for more control and stability.


Customizations

  • Change the AI Tools Agent system message to give the AI your preferred personality or rules.
  • Replace Telegram node with Slack or Discord nodes for using this workflow on other chat platforms.
  • Swap Google Docs nodes for Google Sheets nodes to organize memories and notes as tables.
  • Increase the Memory Buffer Window size to keep longer recent chat history.
  • Make memory session keys user-specific to keep separate chat histories per user.


Troubleshooting

Webhook Not Triggering on Telegram Message

The LangChain Chat Trigger webhook might be inactive or set up wrong.
Check the webhook URL is active in n8n.
Test the webhook by sending messages to your Telegram bot.
Also verify the Telegram webhook is registered properly with Botfather.

Google Docs Node Shows Authentication Error

Google Docs OAuth token may be expired or lacking permissions.
Re-authenticate the Google Docs credentials in n8n.
Confirm Docs API is enabled in Google Cloud Console.

AI Agent Fails to Respond or Returns Error

OpenAI API key may be missing or invalid.
Check the API key validity and usage limits.
Renew or upgrade subscription if needed.


Pre-Production Checklist

  • Confirm LangChain Chat Trigger webhook is activated and connected correctly.
  • Test connections for Google Docs nodes both retrieving and saving memories and notes.
  • Verify OpenAI API keys are correct and have enough quota.
  • Send test messages to the Telegram bot to ensure full response cycle.
  • Watch workflow logs for any errors or dropped requests prior to live usage.


Deployment Guide

Turn on the workflow by switching activation toggle to Active in n8n.
Check the execution logs regularly to confirm no problems.
Set up alerts or notifications in n8n or external tools to flag any errors.
Keep stable internet connection since APIs need to respond properly.


Summary

✓ Automates Telegram chats with AI that remembers long-term info.
✓ Saves time by managing notes and memories automatically.
✓ Produces replies based on stored knowledge and recent chat context.
✓ Uses Google Docs to keep data safe and organized.
✓ Easy to deploy inside n8n with clear input, processing, and output steps.


Automate chatbot memory with n8n and Google Docs

Visit through Desktop to Interact with the Workflow.

Author
Written By
Vikash Kumar
Building AI agents, n8n workflows and end-to-end automation for 30+ Brands across India, the US, Europe, Dubai & Australia. 7+ years of Experience saving founders real hours every week - no code required.
Author
Written By
Vikash Kumar
Building AI agents, n8n workflows and end-to-end automation for 30+ Brands across India, the US, Europe, Dubai & Australia. 7+ years of Experience saving founders real hours every week - no code required.

Frequently Asked Questions

Yes, replacing the Telegram node with Slack nodes will allow the workflow to work with Slack messaging.
The usage depends on how many messages are sent and which OpenAI models are chosen.
Yes, Google Docs stores data securely but credentials must be kept private and doc sharing should be limited to authorized users.
It uses a Memory Buffer Window node to keep the last 50 messages of chat history per session for context.

Related Workflows

Automate Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.