Create a Session-Based Telegram Chatbot with GPT-4o-mini and Google Sheets
How It Works
This workflow creates an AI-powered Telegram chatbot with session management, allowing users to:
Start new conversations** (/new).
Check current sessions** (/current).
Resume past sessions** (/resume).
Get summaries** (/summary).
Ask questions** (/question).
Key Components:
Session Management**:
Uses Google Sheets to track active/expired sessions (storing SESSION IDs and STATE).
/new creates a session; /resume reactivates past ones.
AI Processing**:
OpenAI GPT-4 generates responses with contextual memory (via Simple Memory node).
Summarization: Condenses past conversations when requested.
Data Logging**:
All interactions (prompts/responses) are saved to Google Sheets for audit and retrieval.
User Interaction**:
Telegram commands trigger specific actions (e.g., /question [query] fetches answers from session history).
Main Advantages
-
Multi-session Handling
Each user can create, manage, and switch between multiple sessions independently, perfect for organizing different conversations without confusion. -
Persistent Memory
Conversations are stored in Google Sheets, ensuring that chat history and session states are preserved even if the server or n8n instance restarts. -
Commands for Full Control
With intuitive commands like /new, /current, /resume, /summary, and /question, users can manage sessions easily without needing a web interface. -
Smart Summarization and Q&A
Thanks to OpenAI models, the workflow can summarize entire conversations or answer specific questions about past discussions, saving time and improving the chatbot’s usability. -
Easy Setup and Scalability
By using Google Sheets instead of a database, the workflow is easy to clone, modify, and deploy — ideal for quick prototyping or lightweight production uses. -
Modular and Extensible
Each logical block (new session, get current session, resume, summarize, ask question) is modular, making it easy to extend the workflow with additional features like analytics, personalized greetings, or integrations with CRM systems.
Setup Steps
Prerequisites:
Telegram Bot Token**: Create via BotFather.
Google Sheets**:
Duplicate this template.
Two sheets: Session (active/inactive sessions) and Database (chat logs).
OpenAI API Key**: For GPT-4 responses.
Configuration:
Telegram Integration:
Add your bot token to the Telegram Trigger and Telegram Send nodes.
Google Sheets Setup:
Authenticate the Google Sheets nodes with OAuth.
Ensure sheet names (Session, Database) and column mappings match the template.
OpenAI & Memory:
Add your API key to the OpenAI Chat Model nodes.
Adjust contextWindowLength in the Simple Memory node for conversation history length.
Testing:
Use Telegram commands to test:
/new: Starts a session.
/question [query]: Tests AI responses.
/summary: Checks summarization.
Deployment:
Activate the workflow; the bot will respond to Telegram messages in real-time.
Need help customizing?
Contact me for consulting and support or add me on Linkedin.
Related Templates
Track OpenAI Token Usage and AI Agent Metrics with Google Sheets Dashboard
What it does Captures token usage and cost from your AI Agent/LLM. Logs model, tokens, cost, tool use, and conversation ...
Lookup IP Geolocation Details with IP-API.com via Webhook
This n8n template enables you to instantly retrieve detailed geolocation information for any given IP address by simply ...
Send Hourly Crypto Market Analysis from Binance to Telegram
📈 Daily Crypto Market Summary Bot (Binance to Telegram) This workflow fetches 24h price change data from Binance for s...
🔒 Please log in to import templates to n8n and favorite templates
Workflow Visualization
Loading...
Preparing workflow renderer
Comments (0)
Login to post comments