What This Automation Does
This workflow listens to Telegram messages and uses AI to answer questions or make pictures.
It saves time by replying fast with smart text or images based on user messages.
The Telegram Bot API sends the user message to n8n.
Then OpenAI GPT-4 via Langchain decides the best answer or if an image is needed.
If an image is asked, Dall-E 3 creates one using the prompt.
The bot sends text replies or photos back to Telegram chats.
Who Should Use This Workflow
This workflow fits people who manage Telegram groups and get many similar questions.
It helps non-technical users save hours by automating chat replies and improves chat activity.
Users without coding skills can use this with some setup in n8n.
Tools and Services Used
- Telegram Bot API: Receives user messages.
- n8n Automation Platform: Runs the workflow and connects the tools.
- Langchain Nodes: Link the workflow with OpenAI models and memory.
- OpenAI GPT-4 (gpt-4-1106-preview): Understands and replies to text messages.
- OpenAI Dall-E 3 API: Generates images from text prompts.
Beginner Step-by-Step: How to Use This Workflow in n8n
1. Import Workflow
- Download the workflow file using the Download button on this page.
- Open n8n editor and click “Import from File” to upload the workflow.
2. Configure Credentials
- Open each node that needs credentials.
- Provide your Telegram Bot API token and OpenAI API Key.
3. Update IDs if Needed
- Check if any chat IDs, channel names, or folder IDs in nodes require your own values.
- Replace placeholder values with your actual information.
4. Review Prompts and Expressions
- Look inside the AI Agent and OpenAI Chat Model nodes.
- Copy the prompt texts or expressions to adjust wording if needed.
5. Test the Workflow
- Send test messages to your Telegram bot account.
- Check that the workflow triggers and replies with text or images.
6. Activate Workflow
- Turn on the workflow toggle in n8n to run it live.
- Make sure your n8n instance is running stable and connected to the internet.
If hosting n8n yourself, consider a self-host n8n setup.
Inputs, Processing Steps, and Outputs
Inputs
- New text messages or commands from Telegram group chats.
Processing Steps
- Telegram Trigger catches incoming messages.
- AI Agent reads message and decides if reply or image needed.
- OpenAI Chat Model (GPT-4) generates text answer when needed.
- Window Buffer Memory keeps last 10 messages to remember conversation.
- Dall-E 3 Tool detects image requests and triggers image creation.
- Generate Image in Dall-E 3 HTTP Request calls OpenAI image API.
- Telegram node sends text or photo responses back.
- Correct errors node cleans special characters for Telegram display.
Outputs
- AI-generated text replies to user questions or chats.
- AI-created images based on user prompts delivered inside Telegram.
Common Edge Cases and Failures
- If the Telegram bot does not receive messages, verify webhook URL is set and bot privacy settings allow message access.
- If AI replies are empty or off-topic, check that session keys in Window Buffer Memory use dynamic chat IDs.
- Invalid OpenAI API keys cause message generation failures—renew or correct keys immediately.
- Image generation may fail if Dall-E API quota is reached or prompt is empty.
Customization Ideas
- Change text model parameters inside OpenAI Chat Model node to adjust creativity or response style.
- Modify Window Buffer Memory‘s number of stored messages to keep longer or shorter conversation history.
- Add tools such as Wikipedia search or weather lookup nodes to widen user support.
- Change Telegram message parsing mode from HTML to Markdown for different text formatting.
- Translate system and human prompts inside AI Agent node to support multiple languages.
Sample Prompt in AI Agent Node
The following prompt directs AI response and when to create images.
System: You are a helpful assistant. Reply to users or generate images if asked.
Human: {{$json.message.text}}
Adjust this prompt inside the AI Agent node under parameters for message processing.
Sample Expression for Window Buffer Memory session key
Use this to separate conversations per chat in memory node:
=chat_with_{{ $('Listen for incoming events').first().json.message.chat.id }}
This prevents mixing histories from different Telegram chats.
Special Character Escaping in Telegram Messages
Use this expression to fix AI reply message so Telegram shows it right in HTML mode:
{{ $('AI Agent').item.json.output.replace(/&/g, "&").replace(/>/g, ">").replace(/Summary of Results
✓ Saves 3-4 hours weekly by automating responses and image sharing.
✓ Uses AI to understand chat and provide helpful answers or pictures.
✓ Keeps chat context per user for natural conversations.
✓ Easily imported and customized inside n8n with simple steps.
