What this workflow does
This workflow listens to messages on Telegram and answers them using AI inside n8n.
It helps users by giving smart replies that remember what was said before.
Users can also ask for pictures, and the bot will make and send AI images back automatically on Telegram.
The bot talks using the OpenAI GPT-4o model and uses LangChain to keep memory and choose between chatting or making images.
If something goes wrong sending a Telegram message, the workflow keeps running without stopping.
Who should use this workflow
Content creators or small business owners with many Telegram followers can use this.
The workflow saves time by automating answers and images to Telegram messages.
It is for people who want to improve user chats without answering every question manually.
Tools and services used
- Telegram Bot API: Receives user messages.
- OpenAI GPT-4o Chat Model: Generates smart text replies.
- Dall-E 3 API: Produces AI-generated images.
- LangChain AI Agent Node: Controls if the bot chats or makes images.
- LangChain Window Buffer Memory: Stores recent chat context.
- n8n Workflow Engine: Runs nodes and connects the steps.
Inputs, Processing Steps, and Outputs
Inputs
- Telegram messages from any user or chat.
- User names and chat IDs from Telegram messages.
- Commands or text that ask questions or request images.
Processing Steps
- Telegram Trigger Node: Listens for all new Telegram messages.
- AI Agent Node: Receives message text and user first name; decides if reply or image generation needed.
- OpenAI Chat Model Node: Generates text answers based on GPT-4o with adjustable creativity and memory penalties.
- Window Buffer Memory Node: Saves recent messages (up to 10) per Telegram chat for context in replies.
- Dall-E Image Generation Node: Called by AI Agent to create images when requested by the user.
- Telegram Tool Node: Sends generated images back as documents via Telegram.
- Telegram Final Reply Node: Sends text answers to users, continues running even on sending errors.
Outputs
- Personalized text replies sent back to Telegram users, including their first name.
- AI-generated images sent automatically in Telegram chats when asked.
- Continuous workflow operation despite errors in sending messages.
Beginner step-by-step: How to use this workflow in n8n
Import and setup
- Download the workflow file using the Download button on this page.
- Open the n8n editor and choose “Import from File”, then select the downloaded file.
- After import, add all required credentials: Telegram Bot API Token and OpenAI API Key with GPT-4o and Dall-E access.
- If needed, update the chat ID or other IDs in the Telegram nodes.
- Check the AI Agent node’s system message to keep or customize the greeting:
You are a helpful assistant. You are communicating with a user named {{ $json.message.from.first_name }}. - Test the workflow by sending messages in the linked Telegram chat and see the replies.
- Activate the workflow by enabling the “Active” switch in n8n for live running.
- Make sure your n8n instance has HTTPS if you want Telegram webhooks to work properly—for example, with self-host n8n.
Handling errors and edge cases
- If Telegram messages fail to send, enable “Continue On Error” in the final Telegram node to keep the workflow running.
- Check the chat ID used in Telegram nodes matches the user or chat messaging the bot.
- Update OpenAI API keys if any “authentication failed” errors appear.
- Ensure the Window Buffer Memory session keys match the Telegram chat ID format exactly to save conversation context properly.
Customization ideas
- Change the AI Agent node’s greeting in the system message to add new user data or languages.
- Adjust the Window Buffer Memory node to keep more or fewer past messages to change chat memory length.
- Tweak temperature and frequency penalty in the OpenAI Chat Model node to make responses more creative or strict.
- Add more parameters to the Dall-E image generation prompt for different visual styles or content control.
- Use multi-language instructions within the AI Agent to answer in languages based on user messages.
Summary of benefits and outcomes
✓ Saves time by automating Telegram message replies and image creation.
✓ Sends personalized answers that remember past conversation context.
✓ Automatically creates and sends AI images on demand.
✓ Keeps running even when message sending errors happen.
→ Gives users faster, smarter replies with less manual work.
→ Helps manage social media chats with advanced AI tools inside n8n.
