Telegram RAG Chatbot with File Upload and Groq LLM
This workflow creates a Telegram chatbot that uses Retrieval-Augmented Generation (RAG) to answer questions based on documents you upload. Users can send PDF or CSV files to a Telegram chat, and the workflow processes them into a vector database. When a user then sends a text message, the AI agent retrieves relevant chunks from the stored documents and generates an answer using the Groq Llama 3.1 model. It's like having a personal assistant that learns from your files and responds via Telegram.
Node-by-Node Walkthrough
- Telegram Message Trigger (Telegram Trigger, no authentication required on the node itself — authentication is handled via the Telegram Bot API credentials configured in the n8n credentials store). This trigger listens for all types of updates (messages, commands, etc.) from a Telegram bot. When a new update arrives, it starts the workflow.
- Get a chat1 (Telegram node, Telegram Bot API token auth). It extracts the
chatIdfrom the incoming message and passes it to the next node, ensuring the AI agent knows which chat to reply to. - AI Agent1 (LangChain Agent, no auth). This is the core of the workflow. It uses a system message that instructs the AI to answer only from the knowledge base. The agent has access to the Groq Chat Model as its language model and the Retrieve documents tool to fetch relevant document chunks. It also uses Simple Memory to keep conversation context within a session (keyed by the Telegram chat ID).
- Groq Chat Model (Groq LLM, Groq API key auth). Uses the
llama-3.1-8b-instantmodel. This provides fast, coherent answers. - Simple Memory (Memory Buffer Window, no auth). Stores the last few turns of conversation to give the agent context. The session key is set to the Telegram chat ID from the trigger.
- Retrieve documents (Vector Store as Tool, no auth). This tool allows the agent to query the in-memory vector store. It returns the top 10 most similar document chunks.
- Embeddings HuggingFace Inference1 (Hugging Face Inference, Hugging Face API key auth). Generates embeddings for the user's query so the vector store can find relevant documents.
- Upload your file here (Form Trigger, no auth). This form trigger provides a web form where users can upload
.pdfor.csvfiles. The uploaded file(s) are passed to the Default Data Loader1. - Default Data Loader1 (Document Default Data Loader, no auth). Converts the uploaded binary file into a LangChain document object.
- Recursive Character Text Splitter (Text Splitter, no auth). Splits the document into chunks of up to 3,000 characters with 200 character overlap. This ensures the vector store indexes manageable pieces.
- Simple Vector Store (In-Memory Vector Store, no auth). Stores the document chunks along with their embeddings. The embedding model used is Embeddings HuggingFace Inference.
- Embeddings HuggingFace Inference (Hugging Face Inference, Hugging Face API key auth). Generates embeddings for each document chunk before storing them.
- Reply in Telegram1 (Telegram node, Telegram Bot API token auth). Sends the AI agent's output back to the same Telegram chat.
Setup Instructions
- Telegram Bot: Create a bot via @BotFather and get its API token. In n8n, add a Telegram credential using this token.
- Groq: Sign up at GroqCloud to get an API key. Add a Groq credential in n8n.
- Hugging Face: Create a free account at Hugging Face and generate an Inference API token. Add a Hugging Face Inference credential in n8n.
- n8n Form Trigger: This node is built-in. Ensure your n8n instance is publicly accessible (or use a tunnel like ngrok) so the form can be accessed by users.
- Deploy: Activate the workflow. Share the Telegram bot username with users. They can upload files by visiting the form URL (the one generated by the Upload your file here node) and then send text messages to the bot.
Use Cases and Variations
- Personal knowledge base: Upload company policies, technical documentation, or study materials and ask questions about them via Telegram.
- Customer support bot: Replace the file upload with an HTTP node that fetches FAQs from a database, and use the same RAG architecture to answer customer queries.
- Multi-user support: The session memory is already scoped by Telegram chat ID, so multiple users can use the bot simultaneously without interfering.
- Switch LLM: Replace Groq with OpenAI, Anthropic, or any other supported LLM provider by changing the model node.
- Persistent vector store: Swap the in-memory vector store for Pinecone, Weaviate, or Qdrant to retain documents across restarts.
15 nodesmanual triggerAI
Memory Buffer WindowVector Store In MemoryText Splitter Recursive Character Text SplitterDocument Default Data LoaderTelegramAgentLm Chat GroqEmbeddings Hugging Face Inference
Workflow JSON
{
"nodes": [
{
"parameters": {
"sessionIdType": "customKey",
"sessionKey": "={{ $('Telegram Message Trigger1').item.json.message.chat.id }}"
},
"type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
"typeVersion": 1.3,
"position": [
-2576,
-384
],
"id": "2ac4d2dd-945d-4462-bb22-d2df7c5a05fa",
"name": "Simple Memory"
},
{
"parameters": {
"mode": "retrieve-as-tool",
"toolDescription": "Use this tool to retrieve any information required.",
"memoryKey": {
"__rl": true,
"value": "vector_store_key",
"mode": "list",
"cachedResultName": "vector_store_key"
},
"topK": 10
},
"type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
"typeVersion": 1.3,
"position": [
-2432,
-352
],
"id": "759cc820-fb4e-4dfc-8c67-a7db3a47df18",
"name": "Retrieve documents"
},
{
"parameters": {
"chunkSize": 3000,
"chunkOverlap": 200,
"options": {}
},
"id": "a3ca8f55-b216-435d-a723-7d7e5da647b4",
"name": "Recursive Character Text Splitter",
"type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
"position": [
-3488,
-208
],
// ... truncated (copy to see full JSON)How to Import This Workflow
- 1Copy the workflow JSON above using the Copy Workflow JSON button.
- 2Open your n8n instance and go to Workflows.
- 3Click Import from JSON and paste the copied workflow.
Don't have an n8n instance? Start your free trial at n8nautomation.cloud