Opening Problem Statement
Meet Sarah, a Telegram group admin and small business owner who wants to provide personalized AI assistance to her community. Every day, she spends hours answering repetitive questions, remembering preferences, and trying to keep context alive in chat. Worse, she struggles to manage different types of inputs like voice notes and images through Telegram, and she worries about losing past chat insights because her bot just resets memory after each session.
This wastes Sarah about 3 hours daily that could be better spent on her business, leads to frustrated users because the bot feels impersonal, and risks loss of valuable user-specific data over time.
If only there was a way to automate meaningful AI conversations on Telegram that remembered past interactions, understood user context, and handled multiple input types seamlessly.
What This Automation Does
This n8n workflow combines Telegram, DeepSeek AI models, and Google Docs to create a powerful personalized chatbot with long-term memory capabilities.
- Automatically receives Telegram messages through a webhook for real-time updates.
- Validates the sender’s Telegram user by matching first name, last name, and ID.
- Routes messages based on content type (text, voice, image) to appropriate processing.
- Uses the DeepSeek AI Agent node to process text messages conversationally, with personalized interaction based on user context.
- Retrieves and saves long-term memories in Google Docs, allowing the bot to remember past conversations and preferences over time.
- Sends AI-generated responses back to the Telegram chat, maintaining a natural and engaging user experience.
This saves hours of manual responding, reduces repetitive answers, and ensures a context-aware, personalized AI assistant on Telegram.
Prerequisites ⚙️
- n8n account with workflow activation permissions.
- Telegram Bot API access and credentials for your bot.
- Google Docs account and OAuth2 credentials set up in n8n.
- DeepSeek API key credentials integrated in n8n for AI processing.
- HTTPS-accessible endpoint or self-hosted n8n instance for webhook reception.
(Self-hosting option recommended for reliability, see self-hosting with Hostinger).
Step-by-Step Guide
1. Set Up the Telegram Webhook Listener
Navigate to n8n and create a new workflow. Add the Webhook node and configure it:
- Click Add Node → Search and select Webhook (
n8n-nodes-base.webhook). - Set HTTP Method to POST and Path to
wbot(or your preferred webhook path). - Note the webhook URL generated by n8n; you will register this URL with Telegram bot’s setWebhook method.
Verify your webhook with Telegram by visiting:
https://api.telegram.org/botYOUR_BOT_TOKEN/setWebhook?url=YOUR_WEBHOOK_URL
You should see a JSON confirming success:
{
"ok": true,
"result": true
}Common Mistake: Forgetting to use HTTPS URL or incorrect bot token will cause webhook registration to fail.
2. Validate Telegram User Data
Add the Set node (n8n-nodes-base.set) named “Validation” and enter your expected user’s first name, last name, and ID exactly as known.
Then add an If node (n8n-nodes-base.if) called “Check User & Chat ID” that compares incoming Telegram message sender’s first name, last name, and ID with your stored values.
This ensures only authorized or known users proceed, sending unauthorized users an error message.
Common Mistake: Ensure case sensitivity matches actual Telegram usernames; mismatches block valid users.
3. Route Messages by Type
Use a Switch node (n8n-nodes-base.switch) named “Message Router” to filter messages by type: text, voice notes, images, or extras.
Each output path handles a different type. For this example, the workflow mainly processes text messages with an AI agent, while other types can be extended with custom nodes.
Common Mistake: Not accounting for all possible message types may cause some inputs to be ignored.
4. Process Text Messages with DeepSeek AI Agent
Add the LangChain AI Agent node (@n8n/n8n-nodes-langchain.agent) and connect it to the text output of the Message Router.
Configure it with:
- Prompt including system messages directing the agent to provide friendly, context-aware responses.
- Dynamic input text from the Telegram message body.
- Tool usage enabled for saving long-term memories.
This node uses DeepSeek models (deepseek-chat and deepseek-reasoner) for advanced reasoning and personalized responses.
5. Retrieve Long-Term Memories from Google Docs
Use a Google Docs node (n8n-nodes-base.googleDocs) configured to read a specific document URL where long-term user memories are stored.
Merge retrieved memories back into the flow so the AI Agent can refer to past conversations and context.
Common Mistake: Using an incorrect or inaccessible document URL will block memory retrieval causing context loss.
6. Save New Long-Term Memories
Connect the AI Agent node output to a Google Docs Tool node (n8n-nodes-base.googleDocsTool) configured to append summarized user information or context updates to the same Google Docs document.
This enables the chatbot to gradually learn and remember important details over time, preserving continuity.
7. Send AI Response Back to Telegram
Finally, use the Telegram node (n8n-nodes-base.telegram) to send the AI-generated response back to the user in the original chat ID.
Enable HTML formatting for rich messages, disable appended attribution, and test with real Telegram conversations.
8. Handle Unauthorized and Error Messages
Add additional Telegram nodes configured for error responses if unknown users send messages or in case the AI agent fails.
These nodes send friendly error messages without exposing internal details.
Customizations ✏️
- Customize User Validation: In the “Validation” set node, modify the stored user details to handle multiple users by expanding the logic in the “Check User & Chat ID” node.
- Add Voice Note Processing: Extend the “Message Router” node to connect voice messages to a speech-to-text node for transcription, then send text to the AI Agent.
- Change Memory Storage: Swap Google Docs nodes with a database node like Airtable or MongoDB for scalable long-term memory management.
- Personalized AI Prompt: Adjust the system message in the AI Agent node to include more personalized instructions or company-specific jargon.
- Enable Image Processing: Add an image recognition API node to handle photo messages routed by the “Message Router” node.
Troubleshooting 🔧
Problem: “Webhook not receiving Telegram messages”
Cause: Webhook URL not properly registered or HTTPS issues.
Solution: Double-check the HTTPS webhook URL in Telegram Bot setup; ensure your n8n instance is publicly accessible over HTTPS.
Problem: “User validation failing consistently”
Cause: Case mismatches or incorrect user data in validation node.
Solution: Verify exact Telegram sender.first_name, last_name, and ID, and ensure case sensitivity matches in the “Check User & Chat ID” node conditions.
Problem: “Memory not retrieved or saved correctly”
Cause: Incorrect Google Docs document URL or insufficient permissions.
Solution: Confirm document URL is correct and the OAuth2 credentials have write/read access to the document.
Pre-Production Checklist ✅
- Verify Telegram bot webhook registration successfully returns JSON status OK.
- Test message flow with valid and invalid user inputs through the “Check User & Chat ID” node.
- Confirm the “Message Router” routes text messages correctly to the AI Agent.
- Perform read and write tests on the Google Docs used for memories.
- Simulate AI Agent handling and Telegram responses end-to-end before publishing.
Deployment Guide
Activate your workflow in n8n by toggling the active status. Monitor the execution via n8n’s built-in execution tracking for errors or drop-offs.
Ensure your Telegram bot’s webhook URL remains valid and reachable to avoid missed messages.
Consider setting automated alerts in n8n for failures or add additional logging nodes for production monitoring.
FAQs
- Can I use other AI models instead of DeepSeek?
Yes, the workflow structure supports other OpenAI-compatible models by adjusting credentials and model names. - Does this consume API credits rapidly?
The usage depends on message volume and AI response length; monitor your DeepSeek API quota accordingly. - Is my data safe in Google Docs?
Google Docs is secured with OAuth2 authentication and encrypted storage; protect access with strong credentials. - Can this handle large Telegram groups?
Yes, but ensure your n8n and API quotas scale accordingly.
Conclusion
You’ve just built a personalized AI Telegram bot using DeepSeek with long-term memory via Google Docs, powered by n8n automation. You saved hours of repetitive chat management and improved user engagement with contextual AI responses.
Next, explore adding voice-to-text transcription, richer multimedia handling, or integrating more complex workflows with CRM systems for complete automation.
Keep experimenting and making AI work smarter for you!