Telegram AI Bot with DALL-E Image Generation using LangChain

Overview

This workflow creates an intelligent Telegram bot that leverages OpenAI's GPT-4o and DALL-E 3 models through n8n's LangChain integration. The bot can hold context-aware conversations (remembering the last 10 messages per user) and generate images on demand—all via simple Telegram messages. It's ideal for building personal assistants, creative bots, or customer support agents that need visual output.

Node-by-Node Walkthrough

Here's every node in the workflow, along with its authentication method and key configuration:

  • Listen for incoming events (Telegram Trigger — No auth; bot token set in node credential) – Listens for all types of Telegram updates (messages, commands, etc.). No authentication parameters are required directly on the node; the Telegram bot token is configured via the node's credential settings.
  • AI Agent (LangChain Agent — No auth; relies on connected sub‑nodes) – The core of the workflow. It receives the incoming message text and uses the attached language model (OpenAI Chat Model), memory (Window Buffer Memory), and tools (Send back an image / Generate image in Dalle) to decide what action to take.
  • OpenAI Chat Model (LangChain Chat Model — API Key auth via openAiApi credential) – Uses gpt-4o with a temperature of 0.7 and frequency penalty of 0.2. This model powers the agent's text responses.
  • Window Buffer Memory (LangChain Memory — No auth) – Stores the last 10 messages per user (keyed by Telegram chat ID) to maintain conversational context.
  • Generate image in Dalle (LangChain HTTP Request Tool — API Key auth via openAiApi credential) – A custom tool that sends a POST request to https://api.openai.com/v1/images/generations with model dall-e-3 and a prompt (provided by the AI agent). The tool description tells the agent to call it when the user asks to draw something.
  • Send back an image (Telegram Tool — API Key auth via Telegram credential) – Sends a file (image URL from the DALL-E response) to the user's chat ID using the sendDocument operation. This is exposed as a tool the AI agent can call.
  • Send final reply (Telegram Node — API Key auth via Telegram credential) – Sends the AI agent's text output (from $json.output) back to the user. Note: On error, the workflow continues to the error output (useful for debugging).

Detailed Flow

  1. The Telegram Trigger catches any incoming message from your bot.
  2. The message text is passed to the AI Agent.
  3. The agent uses the OpenAI Chat Model to understand the request and generate a response. It also checks the Window Buffer Memory for recent conversation history.
  4. If the user asks for an image, the agent calls the Generate image in Dalle tool, which returns an image URL.
  5. The agent then uses the Send back an image tool to send that image to the user.
  6. For all other requests, the agent produces a text response, which is sent via the Send final reply node.

Setup Instructions

  1. Telegram Bot – Create a bot via @BotFather and get the API token. In n8n, create a Telegram credential and paste the token.
  2. OpenAI Account – Obtain an API key from OpenAI's platform. Create an openAiApi credential in n8n and paste the key. This credential is used by both the Chat Model and the DALL-E tool.
  3. Workflow Activation – After configuring the credentials, activate the workflow. n8n will automatically set up the webhook for the Telegram trigger (no manual URL needed—n8n handles it internally).
  4. Test – Send a message to your bot on Telegram. Try asking a question or say “Draw a cat wearing a hat” to trigger image generation.

Use Cases & Variations

  • Personal Assistant – Extend with additional tools (web search, calendar lookup, weather API) to make a full‑fledged chatbot.
  • Customer Support – Replace the OpenAI model with a fine‑tuned model or add a fallback to a human agent using n8n's error handling.
  • Image Editor – Modify the DALL-E tool to accept style parameters or multiple images.
  • Multi‑Language – Add a translation node before the AI agent to support multiple languages.
  • Logging – Insert a database node (e.g., Google Sheets, PostgreSQL) to store conversation logs for analysis.
8 nodesmanual triggerAI
Lm Chat Open AIMemory Buffer WindowSticky NoteTelegramTelegram ToolTool HTTP RequestAgent

Workflow JSON

{
  "id": "U8EOTtZvmZPMYc6m",
  "meta": {
    "instanceId": "fb924c73af8f703905bc09c9ee8076f48c17b596ed05b18c0ff86915ef8a7c4a",
    "templateCredsSetupCompleted": true
  },
  "name": "Agentic Telegram AI bot with LangChain nodes and new tools",
  "tags": [],
  "nodes": [
    {
      "id": "13b3488e-af72-4d89-bef4-e9b895e3bf76",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1640,
        580
      ],
      "parameters": {
        "model": "gpt-4o",
        "options": {
          "temperature": 0.7,
          "frequencyPenalty": 0.2
        }
      },
      "credentials": {
        "openAiApi": {
          "id": "rveqdSfp7pCRON1T",
          "name": "Ted's Tech Talks OpenAi"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "864937a1-43f6-4055-bdea-61ab07db9903",
      "name": "Window Buffer Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        1760,
        580
      ],
      "parameters": {
        "sessionKey": "=chat_with_{{ $('Listen for incoming events').first().json.message.chat.id }}",
        "contextWindowLength": 10
      },
      "typeVersion": 1
    },
    {
      "id": "4ef838d4-feaa-4bd3-b2c7-ccd938be4373",
      "name": "Listen for incoming events",
      "type": "n8n-nodes-base.telegramTrigger",
// ... truncated (copy to see full JSON)

How to Import This Workflow

  1. 1Copy the workflow JSON above using the Copy Workflow JSON button.
  2. 2Open your n8n instance and go to Workflows.
  3. 3Click Import from JSON and paste the copied workflow.

Don't have an n8n instance? Start your free trial at n8nautomation.cloud

Ready to automate with n8n?

Get affordable managed n8n hosting with 24/7 support.