n8n

How to Automate Telegram and OpenAI Prompt Optimization?

Turn vague chat requests into clear, useful prompts. The flow upgrades any text prompt and sends the improved version back to Telegram. It fits teams that run AI tasks and want clean inputs fast.

A parent workflow triggers this subflow and passes in the user query and the Telegram chat ID. An AI Agent powered by an OpenAI Chat Model rewrites the prompt using a strict system message that asks for clear instructions, context, outcomes, and length. A short memory window keeps recent context so follow ups stay on track. A Code node formats the result so the message sends cleanly, then the Telegram node replies to the same chat.

Setup needs an OpenAI API key and a Telegram bot token. Map the query field and chat ID as shown so replies go back to the right person. Expect faster prompt prep, fewer clarifying pings, and better AI results in downstream tasks like briefs, support replies, or research summaries.

What are the key features?

  • Starts when executed by another workflow, so it slots into larger processes without changing your main flow.
  • Uses an OpenAI Chat Model set to gpt 4o mini for fast, low cost prompt refinement.
  • AI Agent applies a strict system message and output parser to add clarity, context, and structure.
  • Memory buffer window keeps recent context so follow up prompts stay consistent.
  • Code node formats the agent output into a clean string so Telegram messages send reliably.
  • Telegram node replies to the same chat using the passed chat ID for a smooth user experience.
  • Inputs mapped by expression such as $json.query to keep data flow simple and consistent.

What are the benefits?

  • Reduce manual prompt rewriting from 10 minutes to 30 seconds
  • Cut follow up chat messages by 60 percent with clearer prompts
  • Improve AI output quality by reducing redo attempts by 40 percent
  • Connect Telegram and OpenAI without copy and paste
  • Handle more prompt requests per hour without extra staff

How do you set it up?

  1. Import the template into n8n: Create a new workflow in n8n > Click the three dots menu > Select 'Import from File' > Choose the downloaded JSON file.
  2. You'll need accounts with Telegram and OpenAI. See the Tools Required section above for links to create accounts with these services.
  3. In Telegram, create a bot with BotFather and copy the bot token. Start a chat with your bot so it can message you.
  4. In your OpenAI account, go to the API keys page and create a new API key. Copy it and store it securely.
  5. In n8n, open Credentials. For Telegram, double click the Telegram node, choose Credential to connect with, click Create new credential, and follow the on screen steps. Paste the bot token and name the credential clearly, for example Telegram Prompt Bot.
  6. In n8n, create an OpenAI credential the same way. Choose Create new credential on the OpenAI Chat Model node, paste your API key, and name it, for example OpenAI Prompt Refiner.
  7. Open the OpenAI Chat Model node and confirm the model is set to gpt 4o mini. Select your OpenAI credential.
  8. Open the AI Agent node and confirm Text is set to {{$json.query}} and the system message is present. Keep the memory connection to Simple Memory enabled.
  9. Open the Code node named Split into chunks1 and keep the default code that converts the output to a clean string.
  10. Open the Telegram node. Select your Telegram credential. Leave the chatId expression mapped to $('When Executed by Another Workflow').item.json.chat_id so replies go to the source chat.
  11. In your parent workflow, call this workflow with Execute Workflow and pass two fields: query containing the raw user prompt and chat_id from your Telegram trigger or data source.
  12. Run a test from the parent workflow. You should receive the improved prompt in Telegram. If you see no message, check that query and chat_id are present. For OpenAI 401 errors, verify your API key. For Telegram 403 errors, make sure you have started a chat with the bot.

Tools Required

$24 / mo or $20 / mo billed annually to use n8n in the cloud. However, the local or self-hosted n8n Community Edition is free.

OpenAI

Sign up

Pay-as-you-go: GPT-5 at $1.25 per 1M input tokens and $10 per 1M output tokens

Telegram

Sign up

Free: $0, Telegram Bot API usage is free for developers

Similar Templates

Join Futurise to access 1,200+ automation templates

Get instant access to ready-made automation workflows for n8n, Make.com, AI agents, and more. Download, customise, and deploy in minutes.