{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "339179e6-1f09-435f-9b5e-c85dc3d9ed98",
      "name": "\ud83d\udce9 Receive Telegram Message",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -208,
        0
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "38840cb5-dd2d-4e2c-87c0-e57b6064294c",
      "name": "\ud83d\udcca Fetch Usage Logs",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        16,
        0
      ],
      "parameters": {
        "options": {
          "returnFirstMatch": false
        },
        "filtersUI": {
          "values": [
            {
              "lookupValue": "={{ $json.message.from.id }}",
              "lookupColumn": "user_id"
            },
            {
              "lookupValue": "={{ new Date().toISOString().slice(0,10) }}",
              "lookupColumn": "date"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 2060422727,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RHgZNdT0whop0JY3vrj83yLtmVluG73NIfLBBa8ckPg/edit#gid=2060422727",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1RHgZNdT0whop0JY3vrj83yLtmVluG73NIfLBBa8ckPg",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RHgZNdT0whop0JY3vrj83yLtmVluG73NIfLBBa8ckPg/edit?usp=drivesdk",
          "cachedResultName": "Image bot usage statistic"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "31ef4475-8a58-4dd8-850c-cfe2bbfc3ad7",
      "name": "\ud83d\udcc8 Count Today\u2019s Requests",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        240,
        0
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "67f85ddc-9bcf-4c35-81a1-5a0e1f5d30a6",
      "name": "\ud83d\udd22 Set Daily Limit",
      "type": "n8n-nodes-base.set",
      "position": [
        464,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "1514c5a1-1bd4-4ba2-8bb9-30c679ae4f8e",
              "name": "daily_limit",
              "type": "number",
              "value": 5
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "19a8f732-f6e0-4134-84fc-dfd7e14c7812",
      "name": "\ud83d\udea6 Check Limit Exceeded?",
      "type": "n8n-nodes-base.if",
      "position": [
        688,
        0
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "08e8aac4-9f6a-4fc0-b1ad-7d8493ff74bf",
              "operator": {
                "type": "number",
                "operation": "lt"
              },
              "leftValue": "={{$('\ud83d\udcc8 Count Today\u2019s Requests').item.json.data.length }}",
              "rightValue": "={{ $json.daily_limit }}"
            }
          ]
        }
      },
      "executeOnce": false,
      "typeVersion": 2.2
    },
    {
      "id": "47ff6a8c-8a13-41f1-8847-7bb50c63014e",
      "name": "\ud83d\udeab Notify: Limit Exceeded",
      "type": "n8n-nodes-base.telegram",
      "position": [
        912,
        112
      ],
      "parameters": {
        "text": "=Sorry! Your *daily limit of {{ $('\ud83d\udd22 Set Daily Limit').item.json.daily_limit }} generations* is exceeded!",
        "chatId": "={{ $('\ud83d\udce9 Receive Telegram Message').item.json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "900536f7-d366-45a9-bafb-2fae6c1c0065",
      "name": "\ud83d\udcdd Log Successful Generation",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1808,
        -96
      ],
      "parameters": {
        "columns": {
          "value": {
            "date": "={{ new Date().toISOString().slice(0,10) }}",
            "query": "={{ $('\ud83d\udce9 Receive Telegram Message').item.json.message.text }}",
            "user_id": "={{ $('\ud83d\udce9 Receive Telegram Message').item.json.message.from.id }}",
            "result_url": "={{ $('\ud83c\udfa8 Generate Image (AI/ML API | flux-pro)').item.json.images[0].url }}"
          },
          "schema": [
            {
              "id": "user_id",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "user_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "query",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "query",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "result_url",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "result_url",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "user_id"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 2060422727,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RHgZNdT0whop0JY3vrj83yLtmVluG73NIfLBBa8ckPg/edit#gid=2060422727",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1RHgZNdT0whop0JY3vrj83yLtmVluG73NIfLBBa8ckPg",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1RHgZNdT0whop0JY3vrj83yLtmVluG73NIfLBBa8ckPg/edit?usp=drivesdk",
          "cachedResultName": "Image bot usage statistic"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "70630f26-3817-41f6-94ee-4f479ad15afe",
      "name": "\ud83c\udfa8 Generate Image (AI/ML API | flux-pro)",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1136,
        -96
      ],
      "parameters": {
        "url": "https://api.aimlapi.com/v1/images/generations",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "flux-pro"
            },
            {
              "name": "prompt",
              "value": "={{ $json.content }}"
            },
            {
              "name": "n",
              "value": "1"
            },
            {
              "name": "size",
              "value": "1024x1024"
            },
            {
              "name": "response_formats",
              "value": "=1"
            }
          ]
        },
        "nodeCredentialType": "aimlApi"
      },
      "credentials": {
        "aimlApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "a47d6a9b-9ca6-4716-9083-d43b666da2e0",
      "name": "\ud83e\udde0 Enhance Prompt (AI/ML API | GPT-4o)",
      "type": "n8n-nodes-aimlapi.aimlApi",
      "position": [
        912,
        -96
      ],
      "parameters": {
        "model": "openai/gpt-4o",
        "prompt": "=You are a prompt enhancer for AI image generation. Your task is to take the user's input and return only an improved version of the prompt. Do not include any explanations, introductions, or formatting. Output only the final prompt, nothing else.\n\n\ud83e\udde0 The user\u2019s message is taken as the foundation for visual content generation. This input is then carefully analyzed, enriched, and transformed into a more expressive, structured, and detailed prompt. The purpose of this enhancement is to guide the AI model more effectively, resulting in an image that better matches the user's intent and imagination.\n\n\u270f\ufe0f Original user input:\n{{ $('\ud83d\udce9 Receive Telegram Message').item.json.message.text }}",
        "options": {},
        "requestOptions": {}
      },
      "credentials": {
        "aimlApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "dcffa91d-a4c7-4851-9bdb-85552f4420e5",
      "name": "\ud83d\udd8b Describe Image (AI/ML API | GPT-4o)",
      "type": "n8n-nodes-aimlapi.aimlApi",
      "position": [
        1360,
        -96
      ],
      "parameters": {
        "model": "openai/gpt-4o",
        "prompt": "=Write a vivid and visually rich description for an AI-generated image, starting with the phrase: \"Here\u2019s your image:\". The description should reflect the result of the prompt below, as if the image already exists.\n\nPrompt:\n\"{{ $json.prompt }}\"\n\nAvoid repeating the prompt verbatim. Use expressive language and sensory detail. Keep it to 1\u20132 elegant sentences.\n",
        "options": {},
        "requestOptions": {}
      },
      "credentials": {
        "aimlApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "560a50a5-a96a-48b2-ab1e-1f6f0bcf2a15",
      "name": "\ud83d\udce4 Send Image to User",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1584,
        -96
      ],
      "parameters": {
        "file": "={{ $('\ud83c\udfa8 Generate Image (AI/ML API | flux-pro)').item.json.images[0].url }}",
        "chatId": "={{ $('\ud83d\udce9 Receive Telegram Message').item.json.message.chat.id }}",
        "operation": "sendPhoto",
        "additionalFields": {
          "caption": "={{ $json.content }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "1fed89b3-457f-457e-ba5d-13f209a63cd4",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -272,
        -160
      ],
      "parameters": {
        "color": 4,
        "width": 224,
        "height": 336,
        "content": "## Incoming Message\n### Handle incoming user messages from Telegram."
      },
      "typeVersion": 1
    },
    {
      "id": "3623d8a3-1933-4809-ab5b-ed54f039f116",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -32,
        -160
      ],
      "parameters": {
        "width": 864,
        "height": 336,
        "content": "##  Usage Limit Check\n### Check how many times the user has generated today and enforce the daily limit.\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "a34ed607-47b0-4966-a11f-7298c41b4223",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        848,
        -256
      ],
      "parameters": {
        "color": 6,
        "width": 208,
        "height": 336,
        "content": "##  Prompt Enhancement\n### Use LLM to rewrite the user's input into a rich and detailed prompt.\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "02a9f22d-a4d4-4deb-8ed1-4818a9ddf198",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1072,
        -256
      ],
      "parameters": {
        "color": 6,
        "width": 208,
        "height": 336,
        "content": "##  Image Generation\n### Generate an image using the enhanced prompt via AIMLAPI.\n\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "31f52e39-80dc-45d5-9ef8-7c96d55b81a0",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1296,
        -256
      ],
      "parameters": {
        "color": 6,
        "width": 208,
        "height": 336,
        "content": "##  Image Description\n### Create a vivid description of the generated image using LLM.\n\n\n\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "d923875c-b2d0-4804-8050-5b8daa2aaad8",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1520,
        -256
      ],
      "parameters": {
        "color": 4,
        "width": 416,
        "height": 336,
        "content": "## Delivery & Logging\n### Send the final image and caption to the user, and log the result to Google Sheets.\n\n\n\n\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "46afbb51-ec45-49b2-b6b1-c3333328edf1",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        848,
        96
      ],
      "parameters": {
        "color": 3,
        "width": 256,
        "height": 352,
        "content": "\n\n---\n\n\n---\n\n\n---\n\n\n---\n\n\n---\n\n\n##  Notify: Limit Exceeded\n### Notify the user that the daily generation limit has been reached\n\n\n \n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "53ac8095-8518-465d-bc4c-965a7110bd78",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -336,
        -1904
      ],
      "parameters": {
        "color": 5,
        "width": 560,
        "height": 608,
        "content": "# \ud83e\udde0 AI Image Generator Bot \u2014 Telegram + AIMLAPI (via n8n)\n\nThis workflow lets users generate AI images by messaging a Telegram bot. Each user has a **daily limit**, tracked using **Google Sheets**. Prompts are enhanced and images are generated using **AIMLAPI**.\n\n---\n\n## \ud83d\ude80 Features\n\n* Text-to-image generation via `flux-pro` model\n* Prompt enhancement with GPT-4o\n* Descriptive captions for each image\n* Telegram delivery\n* Daily usage tracking and limiting\n* Logging all activity in Google Sheets"
      },
      "typeVersion": 1
    },
    {
      "id": "56bb2e61-b024-4e87-9471-7f379e9ee6b9",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -336,
        -1280
      ],
      "parameters": {
        "color": 5,
        "width": 560,
        "height": 736,
        "content": "## \u2699\ufe0f Customization\n\n* `Set Daily Limit` node controls the number of free generations\n* You can add:\n\n  * NSFW filtering\n  * Dynamic model selection\n  * `/help` or `/history` commands\n\n---\n\n## \ud83d\udca1 Example User Flow\n\n1. User sends:\n\n   ```\n   a dragon flying above New York at night\n   ```\n\n2. Bot replies:\n\n   > **Here\u2019s your image:**\n   > A crimson dragon soars through moonlit clouds over Manhattan, its wings glowing against the skyline.\n\n> 3. Image is generated and sent\n"
      },
      "typeVersion": 1
    },
    {
      "id": "352ebd4e-5f9e-44b7-8802-2fcf323ca0ad",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        240,
        -1808
      ],
      "parameters": {
        "color": 5,
        "width": 560,
        "height": 912,
        "content": "\n# \ud83d\udee0 Setup Guide\n\n### 1. \ud83d\udcf2 Create a Telegram Bot\n\n* Go to [@BotFather](https://t.me/BotFather)\n* Use `/newbot`, set a name and username\n* Save the **API token**\n\n---\n\n### 2. \ud83d\udd10 Connect Telegram API in n8n\n\n* Go to `Credentials > Telegram API`\n* Create new credentials using the token from BotFather\n\n---\n\n### 3. \ud83d\udcd7 Create a Google Sheet\n\n* Add a sheet named `Sheet1`\n\n* Use these columns as headers:\n\n  ```\n  user_id | date | query | result_url\n  ```\n\n* Share it with your Google service account or OAuth user\n  (depends on how you authenticate Google Sheets in n8n)\n\n---\n\n### 4. \ud83d\udd0c Set Up Credentials in n8n\n\n* **Google Sheets:** OAuth2 or service account\n* **AIMLAPI:** Use your API key in `AI/ML API` credentials\n\n  * Base URL: `https://api.aimlapi.com/v1`\n  * Docs: [docs.aimlapi.com](https://docs.aimlapi.com)\n"
      },
      "typeVersion": 1
    },
    {
      "id": "63074211-fe01-4947-9ffd-f45baab48fbd",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        240,
        -880
      ],
      "parameters": {
        "color": 5,
        "width": 560,
        "height": 336,
        "content": "## \ud83d\udcc1 Data Logged\n\nEach generation is saved to Google Sheets:\n\n` | user_id | date | query | result_url | `\n` | ------- | ---- | ----- | ---------- | `\n\n\n## \ud83e\uddea Testing Tips\n\n* Use a test Telegram chat\n* Add `Console` or `Set` nodes to inspect payloads\n* Test the workflow using `/execute` from Telegram, **not \"Execute Node\"**, if you're using static data\n"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "\ud83d\udd22 Set Daily Limit": {
      "main": [
        [
          {
            "node": "\ud83d\udea6 Check Limit Exceeded?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udcca Fetch Usage Logs": {
      "main": [
        [
          {
            "node": "\ud83d\udcc8 Count Today\u2019s Requests",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udce4 Send Image to User": {
      "main": [
        [
          {
            "node": "\ud83d\udcdd Log Successful Generation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udea6 Check Limit Exceeded?": {
      "main": [
        [
          {
            "node": "\ud83e\udde0 Enhance Prompt (AI/ML API | GPT-4o)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "\ud83d\udeab Notify: Limit Exceeded",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udcc8 Count Today\u2019s Requests": {
      "main": [
        [
          {
            "node": "\ud83d\udd22 Set Daily Limit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udce9 Receive Telegram Message": {
      "main": [
        [
          {
            "node": "\ud83d\udcca Fetch Usage Logs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udcdd Log Successful Generation": {
      "main": [
        []
      ]
    },
    "\ud83d\udd8b Describe Image (AI/ML API | GPT-4o)": {
      "main": [
        [
          {
            "node": "\ud83d\udce4 Send Image to User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83e\udde0 Enhance Prompt (AI/ML API | GPT-4o)": {
      "main": [
        [
          {
            "node": "\ud83c\udfa8 Generate Image (AI/ML API | flux-pro)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83c\udfa8 Generate Image (AI/ML API | flux-pro)": {
      "main": [
        [
          {
            "node": "\ud83d\udd8b Describe Image (AI/ML API | GPT-4o)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}