KI-Agenten in n8n: Aktionen per Telegram freigeben statt blind ausführen
Ein neuer Ansatz für n8n-Agenten löst das Dilemma zwischen read-only-Agenten und riskanten Vollzugriffen: geplante Aktionen landen in einer Warteschlange und werden erst nach Freigabe über Telegram ausgeführt.

Wenn man in n8n einen KI-Agenten baut, landet man meist bei einem von zwei Extremen. Entweder ist der Agent nur lesend (er fasst E-Mails zusammen und beantwortet Fragen, was sicher, aber begrenzt ist), oder man hängt Gmail- und Google-Calendar-Tools direkt an den AI-Agent-Knoten und hofft, dass ein schlechter Prompt oder ein missverstandener Thread nie die falsche E-Mail versendet oder ein Meeting löscht.
Es gibt einen einfachen Mittelweg: Man gibt einem KI-Agenten niemals ein Tool, das direkt E-Mails versendet oder einen Kalender verändert. Stattdessen erhält er ein einziges Tool, das lediglich eine vorgeschlagene Aktion in eine Warteschlange stellt. Ein separater Workflow-Pfad validiert die Felder, speichert den Vorschlag, fragt über Telegram nach und führt die Aktion erst aus, nachdem man auf Genehmigen getippt hat. Lehnt man ab oder läuft die Genehmigungsanfrage ab, wird nichts ausgeführt.
Im Folgenden wird beschrieben, wie man dieses Muster in n8n 2.x in fünf Schritten umsetzt.
Hänge ein Call-n8n-Workflow-Tool ( toolWorkflow ) an deinen AI-Agent-Knoten und benenne es propose_action. Attache keine Gmail- oder Google-Calendar-Schreib-Tools direkt an den Agenten.
Definiere die Eingabeargumente des Tools um ein kind-Feld plus die aktionsspezifischen Felder herum:
Stelle im System-Prompt des AI Agents klar, dass propose_action lediglich eine Anfrage zur Genehmigung durch den Eigentümer in die Warteschlange stellt. Wenn das Tool eine Bestätigung der Einreihung zurückgibt, muss der Agent dem Nutzer mitteilen, dass die Aktion auf Genehmigung wartet, niemals, dass die E-Mail gesendet oder das Ereignis erstellt wurde.
Vertraue im vom propose_action-Tool ausgelösten Sub-Workflow nicht der Ausgabe des Modells. Führe einen Code-Knoten (Modus: Run Once for All Items) aus, der jedes Feld anhand deiner Regeln und deines Config-Set-Knotens (der Werte wie die Zeitzone enthält) validiert.
Hier ist eine gekürzte Version, die send_email und create_event abdeckt:
const a = $( 'Tool request' ). first ( ). json. args || { }; const c = $( 'Config' ). first ( ). json; const kind = String ( a. kind || '' ). trim ( ); const email = ( s ) => /^ [ ^ \ s @ ] + @ [ ^ \ s @ ] + \. [ ^ \ s @ ] + $ /. test ( String ( s || '' ). trim ( ) ); const iso = ( s ) => { const d = DateTime. fromISO ( String ( s || '' ), { zone: c. timezone } ); return d. isValid? d. toISO ( ): ''; }; let p = { }, err = ''; if ( kind === 'send_email' ) { p = { to: String ( a. to || '' ). trim ( ), subject: String ( a. subject || '' ). trim ( ), body: String ( a. body || '' ). trim ( ), }; if (! p. to. split ( ',' ). every ( email ) ) { err = 'to must be one or more valid email addresses'; } else if (! p. subject ||! p. body ) { err = 'subject and body are required'; } } else if ( kind === 'create_event' ) { const start = iso ( a. start ); const end = iso ( a. end ); p = { title: String ( a. title || '' ). trim ( ), start, end }; if (! p. title ||! start ||! end ) { err = 'title, start, and end are required'; } else if ( DateTime. fromISO ( end ) <= DateTime. fromISO ( start ) ) { err = 'end must be after start'; } } else { err = 'unknown kind'; } return [ { json: { ok:! err, error: err, kind, payload: p } } ]; Verzweige anhand von ok:
Auf dem Genehmigungspfad formatiere eine einfache Zusammenfassung der eingereihten Zeile (an wen die E-Mail geht, Betreff und Text, oder Titel und Zeitraum des Ereignisses) und sende sie mit dem Telegram-Knoten und der Operation Send and Wait for Response ( sendAndWait ).
Die Begrenzung der Wartezeit verhindert, dass alte Anfragen ewig offen bleiben. Wenn man eine Aufforderung über Nacht ignoriert, setzt der Knoten sauber als Timeout fort, statt hängen zu bleiben.
Wenn der Telegram-Knoten fortgesetzt wird (weil man auf einen Button getippt hat oder der Warte-Timer abgelaufen ist), normalisiere das Ergebnis in einem Code-Knoten:
const d = ( $input. first ( ). json || { } ). data || $input. first ( ). json || { }; const decision = d. approved === true? 'approve': d. approved === false? 'decline': 'timeout'; return [ { json: { decision } } ]; Leite decision in einen Switch-Knoten mit drei Ausgängen:
Beachte den Default: Nur ein explizites d.approved === true erreicht den Ausführungszweig. Alles andere, einschließlich eines Timeouts oder einer unerwarteten Payload, bewirkt nichts.
Selbst mit expliziter Genehmigung kann eine Aktion wegen Wiederholungen oder manueller erneuter Ausführungen doppelt laufen. Setze eine Idempotenz-Prüfung vor Gmail und Google Calendar, indem du eine zweite n8n Data Table mit dem Namen ea_locks verwendest.
Da die Lock-Zeile vor dem externen API-Aufruf geschrieben wird, sieht eine spätere doppelte Ausführung den vorhandenen Schlüssel und beendet sich, bevor eine zweite E-Mail gesendet oder ein doppeltes Ereignis erstellt wird. (Die Prüfung und das Einfügen sind zwei getrennte Schritte, sodass dies vor Wiederholungen und erneuten Ausführungen schützt, nicht vor zwei Ausführungen, die in derselben Millisekunde starten. Für einen persönlichen Assistenten mit einem Eigentümer ist das in der Regel ausreichend.)
AI Agent | v propose_action(kind, fields) Code: Validate args |-- invalid --> return error to AI Agent (agent retries) | v valid Data Table: ea_actions (insert status = queued, ref) |-- return "queued, waiting for owner approval" to AI Agent | v Telegram: Send and Wait for Response (Approve / Decline + Limit Wait Time) | v Code: Read decision --> Switch |-- decline --> ea_actions: status = declined (stop) |-- timeout --> ea_actions: status = expired (stop) | v approve Data Table: ea_locks (get key = 'action-' + ref) |-- exists --> stop (already executed) | v new Data Table: ea_locks (insert { key, at }) | v Gmail / Google Calendar (execute) | v Data Table: ea_actions (status = finished) Häufige Fehler: Jeden Telegram-Chat den Bot triggern zu lassen. Prüfe die eingehende Telegram-Chat-ID ganz am Anfang des Workflows gegen die Chat-ID des Eigentümers in deinem Config-Set-Knoten. Jeder andere, der deinen Bot-Benutzernamen findet, sollte ignoriert werden, bevor die Nachricht den AI Agent erreicht.
Rohtext von E-Mails als vertrauenswürdige Anweisungen an das LLM weiterzugeben. Eingehende E-Mails sind nicht vertrauenswürdige Eingaben. Verpacke E-Mail-Texte in klare Trennzeichen, kennzeichne sie im Prompt als nicht vertrauenswürdige Daten und verdrahte niemals einen E-Mail-Trigger direkt in einen aktionsausführenden Zweig ohne menschliche Genehmigung.
Ein tägliches Budget für KI-Aufrufe auszulassen. Ein lauter Posteingang oder eine sich wiederholende Tool-Aufrufschleife kann Modellguthaben schnell verbrauchen. Zähle KI-Aufrufe pro Tag und rufe das Modell nicht mehr auf, sobald das Limit erreicht ist.
Den Agenten behaupten zu lassen, die Aktion sei bereits geschehen. Wenn dein Tool ein "ok" ohne Kontext zurückgibt, antwortet der Agent oft "Ich habe die E-Mail gesendet." Gib vom Tool "queued, waiting for owner approval" zurück und wiederhole diese Formulierung im System-Prompt, damit der Nutzer weiß, dass noch ein Tipp in Telegram erforderlich ist. Fazit: Die Trennung von Vorschlag und Ausführung macht einen n8n-KI-Agenten zu etwas, dem man E-Mail- und Kalenderzugriff anvertrauen kann.
Die Validierung fängt fehlerhafte Argumente ab, bevor man sie überhaupt zu Gesicht bekommt, Telegram sendAndWait bietet einen klaren Genehmigen/Ablehnen-Schritt mit sicherem Timeout, und ea_locks verhindert, dass Wiederholungen Duplikate senden.
Wenn du das nicht selbst verdrahten möchtest, ist die Vorlage AI Executive Assistant (153 Knoten, Gmail-Sortierung plus Kalender plus Aufgaben in Telegram, jeder Sendevorgang wartet auf deinen Tipp, getestet mit 42 Szenarien auf n8n 2.47.7) hier erhältlich: https://rasareka.gumroad.com/l/ai-executive-assistant-n8n?utm_source=devto&utm_campaign=dev-weekly. Kleinere eigenständige Beispiele findest du auch im kostenlosen n8n-Workflow-Repository: https://github.com/rasareka/n8n-free-workflows.
Automatisch übersetzt. Das Original auf Englisch ist unten aufklappbar.
Original (Englisch) anzeigen
When you build an AI agent in n8n, you usually end up at one of two extremes. Either the agent is read-only (it summarizes emails and answers questions, which is safe but limited), or you attach Gmail and Google Calendar tools directly to the AI Agent node and hope a bad prompt or a misread thread never sends the wrong email or deletes a meeting.
There is a straightforward middle ground: never give an AI agent a tool that sends email or modifies a calendar directly. Instead, give it a single tool that only queues a proposed action. A separate workflow path validates the fields, stores the proposal, asks you in Telegram, and runs the action only after you tap Approve. If you decline, or if the approval request times out, nothing runs.
Below is how to build this pattern in n8n 2.x in five steps.
Attach one Call n8n Workflow Tool ( toolWorkflow ) to your AI Agent node and name it propose_action. Do not attach Gmail or Google Calendar write tools directly to the agent.
Define the tool's input arguments around a kind field plus the action-specific fields:
In your AI Agent system prompt, state clearly that propose_action only queues a request for owner approval. When the tool returns a queued confirmation, the agent must tell the user that the action is waiting for approval, never that the email was sent or the event was created.
Inside the sub-workflow triggered by propose_action, do not trust the model's output. Run a Code node (mode: Run Once for All Items ) to validate every field against your rules and your Config Set node (which holds values like timezone ).
Here is a shortened version covering send_email and create_event:
const a = $( 'Tool request' ). first ( ). json. args || { }; const c = $( 'Config' ). first ( ). json; const kind = String ( a. kind || '' ). trim ( ); const email = ( s ) => /^ [ ^ \ s @ ] + @ [ ^ \ s @ ] + \. [ ^ \ s @ ] + $ /. test ( String ( s || '' ). trim ( ) ); const iso = ( s ) => { const d = DateTime. fromISO ( String ( s || '' ), { zone: c. timezone } ); return d. isValid? d. toISO ( ): ''; }; let p = { }, err = ''; if ( kind === 'send_email' ) { p = { to: String ( a. to || '' ). trim ( ), subject: String ( a. subject || '' ). trim ( ), body: String ( a. body || '' ). trim ( ), }; if (! p. to. split ( ',' ). every ( email ) ) { err = 'to must be one or more valid email addresses'; } else if (! p. subject ||! p. body ) { err = 'subject and body are required'; } } else if ( kind === 'create_event' ) { const start = iso ( a. start ); const end = iso ( a. end ); p = { title: String ( a. title || '' ). trim ( ), start, end }; if (! p. title ||! start ||! end ) { err = 'title, start, and end are required'; } else if ( DateTime. fromISO ( end ) <= DateTime. fromISO ( start ) ) { err = 'end must be after start'; } } else { err = 'unknown kind'; } return [ { json: { ok:! err, error: err, kind, payload: p } } ]; Enter fullscreen mode Exit fullscreen mode Branch on ok:
On the approval path, format a plain summary of the queued row (who the email goes to, the subject and body, or the event title and time range) and send it using the Telegram node with the Send and Wait for Response ( sendAndWait ) operation.
Limiting the wait time prevents old requests from sitting open forever. If you ignore a prompt overnight, the node resumes cleanly as a timeout instead of hanging.
When the Telegram node resumes (because you tapped a button or because the wait timer expired), normalize the result in a Code node:
const d = ( $input. first ( ). json || { } ). data || $input. first ( ). json || { }; const decision = d. approved === true? 'approve': d. approved === false? 'decline': 'timeout'; return [ { json: { decision } } ]; Enter fullscreen mode Exit fullscreen mode Feed decision into a Switch node with three outputs:
Notice the default: only an explicit d.approved === true reaches the execution branch. Anything else, including a timeout or an unexpected payload, does nothing.
Even with explicit approval, an action can run twice because of retries or manual re-executions. Put an idempotency check in front of Gmail and Google Calendar using a second n8n Data Table named ea_locks.
Because the lock row is written before the external API call, a later duplicate execution sees the existing key and exits before sending a second email or creating a duplicate event. (The check and the insert are two separate steps, so this protects against retries and re-runs, not two executions starting in the same millisecond. For a personal assistant with one owner, that is usually enough.)
AI Agent | v propose_action(kind, fields) Code: Validate args |-- invalid --> return error to AI Agent (agent retries) | v valid Data Table: ea_actions (insert status = queued, ref) |-- return "queued, waiting for owner approval" to AI Agent | v Telegram: Send and Wait for Response (Approve / Decline + Limit Wait Time) | v Code: Read decision --> Switch |-- decline --> ea_actions: status = declined (stop) |-- timeout --> ea_actions: status = expired (stop) | v approve Data Table: ea_locks (get key = 'action-' + ref) |-- exists --> stop (already executed) | v new Data Table: ea_locks (insert { key, at }) | v Gmail / Google Calendar (execute) | v Data Table: ea_actions (status = finished) Enter fullscreen mode Exit fullscreen mode Common mistakes Letting any Telegram chat trigger the bot. Check the incoming Telegram chat ID against the owner's chat ID in your Config Set node at the very start of the workflow. Anyone else who finds your bot username should be ignored before the message reaches the AI Agent.
Passing raw email text into the LLM as trusted instructions. Incoming emails are untrusted input. Wrap email bodies in clear delimiters, label them as untrusted data in the prompt, and never wire an email trigger straight into an action-executing branch without human approval.
Skipping a daily AI-call budget. A noisy inbox or a looping tool call can burn through model credits quickly. Count AI calls per day and stop calling the model once the cap is reached.
Letting the agent claim the action already happened. If your tool returns "ok" without context, the agent will often reply "I've sent the email." Return "queued, waiting for owner approval" from the tool and repeat that wording in the system prompt, so the user knows a tap in Telegram is still required. Wrap-up Separating proposal from execution turns an n8n AI agent into something you can trust with email and calendar access.
Validation catches malformed arguments before you ever see them, Telegram sendAndWait gives you a clear approve/decline step with a safe timeout, and ea_locks keeps retries from sending duplicates.
If you'd rather not wire this yourself, the AI Executive Assistant template (153 nodes, Gmail triage + calendar + tasks in Telegram, every send waits for your tap, tested with 42 scenarios on n8n 2.47.7) is here: https://rasareka.gumroad.com/l/ai-executive-assistant-n8n?utm_source=devto&utm_campaign=dev-weekly. You can also browse smaller standalone examples in the free n8n workflows repository: https://github.com/rasareka/n8n-free-workflows.