An AI-powered Telegram bot built with the n8n workflow automation platform. It features role-based access control, persistent conversation memory, and integrated DALL-E 3 image generation capabilities.
- Intelligent Conversations: Powered by OpenAI GPT-4 with conversation memory.
- Role-Based Access: Different AI behavior for administrators and regular users.
- Image Generation: Integrated DALL-E 3 for creating images on demand.
- HTML Formatting: Rich message formatting with Telegram-supported HTML tags.
- Conversation Memory: Maintains context across multiple messages using buffer window memory.
- n8n instance (self-hosted or cloud).
- OpenAI API key (with access to GPT-4 and DALL-E 3).
- Telegram Bot Token (obtain from @BotFather).
- Download the
Telegram_AI_bot_CLEANED.jsonfile from this repository. - Open your n8n instance.
- Go to Workflows → Import from File.
- Select the downloaded JSON file.
- Choose "Import as new workflow".
After import, you'll need to set up the following credentials:
OpenAI API:
- Go to Credentials in n8n.
- Create new OpenAI credential.
- Enter your OpenAI API key.
Telegram Bot:
- Create a bot via @BotFather.
- Copy the bot token.
- In n8n, create new Telegram credential.
- Paste your bot token.
To enable admin privileges:
- Open the workflow in n8n.
- Find the Edit Fields node (named "Define Leaders" in some versions).
- Locate the
leader_idsparameter. - Replace
[0]with your actual Telegram user ID.- To find your ID, message @userinfobot.
{
"name": "leader_ids",
"type": "json",
"value": [YOUR_TELEGRAM_ID]
}- Click the Active toggle in the top-right corner.
- The bot is now live and ready to receive messages!
- Trigger Node: Listen for incoming events via Telegram webhook listener.
- Authentication Flow:
- Edit Fields: Defines administrator IDs.
- Check Leader: Validates user permissions via JavaScript.
- If Node: Routes requests to the appropriate AI agent.
- AI Agents:
- AI Agent User: Standard conversational AI for regular users.
- AI Agent Admin: Priority AI agent for administrators with elevated context.
- Language Model: OpenAI Chat Model (GPT-4-1106-preview) with configurable temperature (0.7) and frequency penalty (0.2).
- Memory: Window Buffer Memory - Maintains the last 10 messages per chat session.
- Tools: Dall-E 3 Tool - Enables image generation via natural language requests.
- Response Handlers:
- Telegram Node: Sends AI responses back to users.
- Correct Errors: Fallback handler for HTML formatting issues.
- AI agent detects image request intent.
- Triggers "Execute Workflow Trigger".
- Generate image in Dall-E 3 (API call to OpenAI).
- Delivers generated image back to the user via Telegram.
Simply message your bot in Telegram. The AI will respond contextually based on your conversation history.
Ask the bot to draw or create images:
- "Draw a sunset over mountains"
- "Create an image of a futuristic city"
- "Generate a picture of a cat wearing a hat"
Users with admin privileges receive priority handling and specialized system prompts defined in the "AI Agent Admin" node.
Edit these in the OpenAI Chat Model node parameters:
- Temperature (0.0 - 2.0):
- Lower = more focused and deterministic.
- Higher = more creative and random.
- Default: 0.7
- Frequency Penalty (0.0 - 2.0): Reduces repetition in responses. Default: 0.2
- Context Window: Number of previous messages to remember. Default: 10 messages
Modify the AI's personality by editing system messages in:
AI Agent Usernode.AI Agent Adminnode.
- Bot doesn't respond: Check if the workflow is Active. Verify Telegram credentials. Check n8n execution logs.
- HTML formatting errors: The
Correct errorsnode handles most issues. Ensure special characters< > &are escaped in custom messages. - Image generation fails: Verify OpenAI API key has DALL-E 3 access and sufficient credits.
- Add web search capabilities using additional tools.
- Integrate with databases for persistent data.
- Create custom slash commands for specific actions.
- Add file processing capabilities.
This project is provided as-is for educational and personal use.
Built with n8n, OpenAI GPT-4, and Telegram.
---