workflow recipe
n8n Telegram AI Bot Workflow With Guardrails
Use Telegram to receive messages, IF to filter supported commands, Set or Code to build a compact prompt, OpenAI to generate a reply, and Telegram to send the response.
Independent third-party notes. n8n is a trademark of its owner and is referenced only for compatibility and troubleshooting context.
Quick Answer
Use Telegram to receive messages, IF to filter supported commands, Set or Code to build a compact prompt, OpenAI to generate a reply, and Telegram to send the response.
Problem Pattern
Telegram AI bots get messy when they answer every message, leak private chat content into prompts, or fail to handle long, empty, or command-style messages.
Key Facts
- Input
- Telegram messages should be filtered before reaching an AI node.
- Prompt
- A compact prompt reduces noise and accidental over-sharing.
- Output
- Telegram replies should be length-controlled and safe for the chat context.
- Abuse control
- Public bots need rate, command, and permission controls.
Recommended Steps
- Receive Telegram messages or commands through the Telegram node.
- Filter unsupported commands, empty text, and unauthorized chat IDs.
- Use Set or Code to create a short prompt with only necessary context.
- Call OpenAI and format the reply.
- Send the response back through Telegram and log failures safely.
Verification
- Allowed commands receive a response.
- Unauthorized or unsupported messages are ignored.
- Long messages are handled without breaking the workflow.
- The response does not include hidden prompt or sensitive data.
Warnings
- Do not feed private chat history into AI prompts without consent.
- Public bots can be abused if they respond to every user.
- Model output should not be treated as authoritative advice.
Best For
- Internal helper bots
- FAQ assistants
- Small team automation commands
Not For
- Public high-volume bots without abuse controls
- High-stakes advice without human review
Common Mistakes
- Replying to every Telegram message.
- Forgetting chat ID allowlists.
- Passing entire chat history into prompts.
- Ignoring long message limits and API failures.
Examples
Telegram: receive message
IF: text starts with /ask and chat_id allowed
Set: clean question and system context
OpenAI: generate concise answer
Telegram: send reply FAQ
Should the bot respond in every chat?
No. Start with an allowlist or command filter so the workflow is not abused.
Can I include chat history?
Only include the minimum context needed and make sure privacy expectations are clear.