How to Build a Telegram AI Chat and Image Bot with n8n

Discover how this Telegram AI-bot workflow in n8n automates chat responses and image creation based on user commands, saving you hours of manual effort and enhancing user engagement through intelligent interactions.
telegramTrigger
openAi
telegram
+4
Learn how to Build this Workflow with AI:
Workflow Identifier: 2332
NODES in Use: telegramTrigger, switch, stickyNote, set, openAi, telegram, merge

Press CTRL+F5 if the workflow didn't load.

Visit through Desktop for Best experience

Opening Problem Statement

Meet Sarah, a community manager for a popular Telegram channel. Every day, she spends hours responding to user messages—answering questions, welcoming new members, and even creating images on demand to keep the chat engaging. This manual process is not only time-consuming but also prone to delays and inconsistent communication quality. Sarah knows there has to be a better way to provide instant, intelligent responses and automated image generation without losing the personal touch.

Before discovering this automated Telegram AI-bot workflow built with n8n, Sarah was losing valuable time—up to 3 hours daily—manually handling chats and requests. Sometimes, answer delays frustrated users, causing engagement drops. Plus, generating images involved switching between multiple tools and manually sending files to the chat. This inefficiency was costing her community’s growth potential and her own sanity.

What This Automation Does

This powerful Telegram AI-bot workflow in n8n transforms how Sarah interacts with her Telegram audience by automating chat responses and image creation. Here’s what happens when this workflow runs:

  • Smart Command Detection: It listens for specific commands like /start to welcome users, or /image to generate images.
  • Intelligent Chat Replies: The bot replies in the user’s language using OpenAI GPT-4, maintaining a friendly tone and including relevant emojis to make interactions lively.
  • Image Generation: Upon receiving the /image command, it creates AI-generated images based on user descriptions using OpenAI’s image generation API.
  • Typing Indicator: Shows appropriate “typing” or “upload photo” actions in the chat to improve UX and set user expectations.
  • Error Handling: Gracefully responds with helpful messages when unsupported commands are used, guiding users on how to proceed.
  • Multi-Language Support: Automatically detects and replies in the user’s system language, personalizing communication for a global audience.

By automating these tasks, Sarah saves several hours every day, reduces manual errors, and boosts user satisfaction significantly — without writing one line of code outside n8n’s visual workflow editor.

Prerequisites ⚙️

  • n8n Account (for building and running workflows)
  • Telegram Bot Token (created via Telegram BotFather, connected in n8n’s credentials)
  • OpenAI Account (with access to GPT-4 and DALL·E image generation APIs)
  • Configured Telegram API credentials in n8n
  • Configured OpenAI API credentials in n8n

Step-by-Step Guide

1. Set Up Telegram Trigger Node

Navigate to NodesAdd Node → Search and select Telegram Trigger. Configure it by selecting your Telegram API credentials (your bot token). Allow it to listen to all update types (*) to capture every message.

You should see a webhook URL generated by n8n, which Telegram uses to forward messages to your workflow.

Expected Outcome: Workflow starts on any Telegram message sent to your bot.

Common Mistake: Forgetting to set webhook URL in Telegram BotFather settings will prevent triggering.

2. Add Preprocessing Node to Extract Message Text

Add a Set node named PreProcessing. Configure with dot notation enabled and set message.text to {{ $json?.message?.text || "" }}. This prepares incoming message text for later nodes.

This standardizes data access across the workflow.

Expected Outcome: Messages have consistent text field for processing.

3. Configure Settings Node for Model Parameters

Add another Set node called Settings. Define key variables such as:

  • model_temperature: 0.8 (controls creativity of AI response)
  • token_length: 500 (limits response size)
  • system_command: A prompt string instructing the AI to respond friendly, detect user language, and reply with emojis.
  • bot_typing: Dynamic expression switching between typing and upload_photo based on user commands (for typing indicator behavior).

Expected Outcome: AI nodes receive correct parameters for personalized responses.

4. Use Telegram Send Typing Action Node

Add a Telegram node; set Operation to sendChatAction. Use an expression to decide between typing or upload_photo depending on user input ({{ $json.bot_typing }}). Connect from Settings.

This improves user experience by indicating bot activity.

5. Add Switch Node to Check Commands

Insert a Switch node named CheckCommand. Configure multiple rules to branch based on the messages:

  • If text starts with /start, send welcome message
  • If text starts with /image , trigger image generation
  • If text does not start with / (normal chat), proceed to normal chat response
  • Fallback branch for unsupported commands

Expected Outcome: Workflow decides correct response path based on user input.

6. Add OpenAI Chat Node for Chatbot Mode

Add OpenAI node named Chat_mode. Configure with GPT-4 model. Use the system_command from Settings as system prompt and incoming user text as user message. Set temperature and max tokens dynamically from Settings.

Expected Outcome: Intelligent chatbot responses tailored to user input and language detected.

7. Add OpenAI Chat Node for Greeting New Users

Add OpenAI node named Greeting for the /start command. Prompt includes welcome message in user’s language. Similar dynamic parameter use as Chat_mode.

Expected Outcome: Personalized welcome message generated for new users in their language.

8. Add OpenAI Node for Image Generation

Insert OpenAI node named Create an image. Configure for image generation with User’s command (text after /image ) as prompt. Use DALL·E endpoint with size 512×512.

Expected Outcome: AI-generated image URL returned in response.

9. Add Telegram Node to Send Chat Replies

Add Telegram node named Text reply to send text responses back to users. Reference chat ID from initial message to send directly to user. Use Markdown parsing for formatting.

Connect this node to both Chat_mode and Greeting outputs.

10. Add Telegram Node to Send Generated Images

Add Telegram node named Send image configured to sendPhoto operation. Use the image URL returned by OpenAI image generation node as the file source.

11. Add Fallback Node for Unsupported Commands

Add Telegram node named Send error message connected from fallback branch of CheckCommand. This node sends a helpful message explaining valid commands and usage examples.

This ensures users know how to interact with the bot properly.

Customizations ✏️

  • Change AI Model Temperature: In the Settings node, adjust model_temperature value (e.g., 0.5 for less creative or 1 for highly creative responses). This directly influences chatbot reply style.
  • Expand Supported Commands: Modify CheckCommand switch node to recognize new prefixes and add matching response or action nodes, like weather info or news.
  • Enhance Welcome Message: Edit the Greeting OpenAI prompt to include more personalized info or links based on your channel’s content.
  • Customize Typing Indicator: Change logic in Settings node for bot_typing expression to add more action types like “record_video” or include delay nodes.
  • Modify Image Size or Quantity: In the Create an image node, adjust parameters for output image size or number of images generated per request.

Troubleshooting 🔧

  • Problem: “Webhook not responding” error in Telegram Trigger node.
    Cause: Telegram webhook URL not set in BotFather or n8n URL not publicly accessible.
    Solution: Double-check webhook URL configuration in Telegram BotFather, ensure your n8n instance is publicly reachable or use services like ngrok.
  • Problem: AI node returns unexpected language replies.
    Cause: System prompt in Settings not correctly detecting language or user language field missing.
    Solution: Verify system command expression uses the correct user language code and defaults when missing.
  • Problem: Image generation fails or returns no URL.
    Cause: Invalid input text after /image command or OpenAI API key lacks image generation privileges.
    Solution: Ensure prompt text after command is valid, check OpenAI subscription, and review API usage logs.
  • Problem: Messages not delivered.
    Cause: Incorrect chat ID reference or Telegram API token issues.
    Solution: Confirm chatId is correctly passed from message context and your bot token is valid and active.

Pre-Production Checklist ✅

  • Verify Telegram bot token and webhook URL setup.
  • Test OpenAI API credentials by running test calls with GPT-4 and image generation.
  • Send test messages including /start, normal text, and /image commands to check all workflow branches.
  • Check that typing action shows correctly in Telegram during responses.
  • Confirm all nodes pass data as expected using n8n execution preview.

Deployment Guide

Activate your workflow in n8n by clicking the Activate toggle. Make sure webhooks are properly set and your n8n instance is accessible.

Monitor the workflow via n8n’s execution history to troubleshoot issues or optimize performance. Since this workflow connects Telegram and OpenAI, keep an eye on API usage limits to avoid unexpected costs.

FAQs

  • Q: Can I use a different AI model than GPT-4?
    A: Yes, you can switch to GPT-3.5 or other models supported by OpenAI by updating the model field in the OpenAI nodes.
  • Q: Does this workflow consume a lot of API credits?
    A: Image generation uses more credits than chat responses. Plan accordingly based on volume.
  • Q: Is user data secure?
    A: Data flows through your n8n setup and APIs. Use secure hosting and limit access for best security.

Conclusion

With this Telegram AI-bot workflow in n8n, you’ve automated user greetings, smart chat replies, and creative image generation — all personalized to user language and commands. Sarah, our community manager, reclaimed hours every day, drastically improving chat responsiveness and user satisfaction.

You can now extend this bot to include more smart commands, integrate other AI services, or enhance multilingual support further. This workflow stands as a robust example of how n8n empowers scalable, intelligent Telegram automation without complex coding.

Related Workflows

Automate Viral UGC Video Creation Using n8n + Degaus (Beginner-Friendly Guide)

Learn how to automate viral UGC video creation using n8n, AI prompts, and Degaus. This beginner-friendly guide shows how to import, configure, and run the workflow without technical complexity.
Form Trigger
Google Sheets
Gmail
+37
Free

AI SEO Blog Writer Automation in n8n (Beginner Guide)

A complete beginner guide to building an AI-powered SEO blog writer automation using n8n.
AI Agent
Google Sheets
httpRequest
+5
Free

Automate CrowdStrike Alerts with VirusTotal, Jira & Slack

This workflow automates processing of CrowdStrike detections by enriching threat data via VirusTotal, creating Jira tickets for incident tracking, and notifying teams on Slack for quick response. Save hours daily by transforming complex threat data into actionable alerts effortlessly.
scheduleTrigger
httpRequest
jira
+5
Free

Automate Telegram Invoices to Notion with AI Summaries & Reports

Save hours on financial tracking by automating invoice extraction from Telegram photos to Notion using Google Gemini AI. This workflow extracts data, records transactions, and generates detailed spending reports with charts sent on schedule via Telegram.
lmChatGoogleGemini
telegramTrigger
notion
+9
Free

Automate Email Replies with n8n and AI-Powered Summarization

Save hours managing your inbox with this n8n workflow that uses IMAP email triggers, AI summarization, and vector search to draft concise replies requiring minimal review. Automate business email processing efficiently with AI guidance and Gmail integration.
emailReadImap
vectorStoreQdrant
emailSend
+12
Free

Automate Email Campaigns Using n8n with Gmail & Google Sheets

This n8n workflow automates personalized email outreach campaigns by integrating Gmail and Google Sheets, saving hours of manual follow-up work and reducing errors in email sequences. It ensures timely follow-ups based on previous email interactions, optimizing communication efficiency.
googleSheets
gmail
code
+5
Free