Work_Flow_Of_Ali¶
A multilingual Telegram customer service bot that provides intelligent support in both English and Luganda, using AI-powered language detection, knowledge base search, and conversation memory to deliver personalized customer assistance.
Purpose¶
No business context provided yet — add a context.md to enrich this documentation.
How It Works¶
- Message Reception: The workflow activates when a user sends a message to the connected Telegram bot
- User Profile Lookup: Retrieves the user's existing profile from the PostgreSQL database using their Telegram ID
- Language Detection: Analyzes the incoming message text to detect whether it's written in English or Luganda using keyword matching
- AI-Powered Response: An OpenAI-powered agent processes the message, searches the knowledge base for relevant information, and generates a response in the same language as the user's message
- Profile Update: Updates the user's language preference in the database based on the detected language
- Response Delivery: Sends the AI-generated response back to the user via Telegram
The system maintains conversation history and personalizes responses based on previous interactions, ensuring continuity across multiple conversations.
Mermaid Diagram¶
graph TD
A[Telegram Trigger] --> B[Get User Profile]
B --> C[Detect Language]
C --> D[Customer Service Agent]
E[OpenAI Chat Model] --> D
F[Postgres Chat Memory] --> D
G[Knowledge Base Vector Store] --> D
H[HuggingFace Embeddings] --> G
D --> I[Update User Profile]
I --> J[Send Telegram Response]
Trigger¶
Telegram Trigger: Webhook-based trigger that activates when users send messages to the configured Telegram bot. Listens for message updates and captures the full message context including sender information.
Nodes Used¶
| Node Type | Purpose |
|---|---|
| Telegram Trigger | Receives incoming messages from Telegram users |
| Postgres (Get User Profile) | Retrieves existing user data from the database |
| Code (Detect Language) | Analyzes message text to identify English vs Luganda language |
| OpenAI Chat Model | Provides the AI language model for generating responses |
| Postgres Chat Memory | Stores and retrieves conversation history for context |
| Knowledge Base Vector Store | Searches the knowledge base for relevant information |
| HuggingFace Embeddings | Converts text to vector embeddings for semantic search |
| Customer Service Agent | Orchestrates the AI response generation with tools and memory |
| Postgres (Update User Profile) | Updates user language preferences in the database |
| Telegram (Send Response) | Delivers the AI-generated response back to the user |
External Services & Credentials Required¶
- Telegram Bot API: Bot token for receiving and sending messages
- OpenAI API: API key for GPT-4.1-mini language model access
- HuggingFace API: API token for text embedding services
- PostgreSQL Database: Connection credentials for user profiles, chat history, and knowledge base storage
Environment Variables¶
The workflow requires the following credentials to be configured in n8n: - Telegram bot credentials (bot token) - OpenAI API credentials - HuggingFace API credentials - PostgreSQL database connection details
Data Flow¶
Input: Telegram messages containing user text in English or Luganda
Processing: - User identification and profile retrieval - Language detection using keyword analysis - AI-powered response generation with knowledge base search - Conversation history integration - User profile updates
Output: - Contextual responses in the user's preferred language - Updated user profiles with language preferences - Stored conversation history for future interactions
Error Handling¶
The workflow does not include explicit error handling nodes. Consider adding error handling for: - Database connection failures - API rate limits or timeouts - Invalid Telegram message formats - Knowledge base search failures
Known Limitations¶
No specific limitations documented in the provided context.
Related Workflows¶
No related workflows mentioned in the provided context.
Setup Instructions¶
-
Import Workflow: Import the JSON into your n8n instance
-
Configure Credentials:
- Set up Telegram bot credentials with your bot token
- Configure OpenAI API credentials with your API key
- Add HuggingFace API credentials
- Set up PostgreSQL database connection
-
Database Setup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
-- Create users table CREATE TABLE users ( telegram_id BIGINT PRIMARY KEY, language VARCHAR(10), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- Create chat_history table for conversation memory CREATE TABLE chat_history ( session_id VARCHAR(255), message_id SERIAL, message JSONB, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -
Knowledge Base Setup:
- Populate the vector store with your knowledge base content
- Ensure the HuggingFace embeddings are properly configured
-
Telegram Bot Configuration:
- Create a bot via @BotFather on Telegram
- Set the webhook URL to your n8n instance
- Configure the bot's privacy settings as needed
-
Test the Workflow:
- Activate the workflow in n8n
- Send test messages in both English and Luganda to verify language detection
- Confirm responses are generated in the correct language
- Verify user profiles are being created and updated
-
Monitor and Optimize:
- Review conversation logs for accuracy
- Update the knowledge base as needed
- Adjust language detection keywords for better accuracy