{
 "name": "RecipeAgents: Telegram AI agent with memory in n8n",
 "nodes": [
  {
   "parameters": {
    "updates": [
     "message"
    ],
    "additionalFields": {}
   },
   "id": "95a5e12f-6a2f-4ef2-83f4-354dca44015b",
   "name": "Telegram Trigger",
   "type": "n8n-nodes-base.telegramTrigger",
   "typeVersion": 1.5,
   "position": [
    0,
    300
   ],
   "webhookId": "c9e348ba-1699-4b0d-8d1a-a1cdb03c1f66"
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "strict",
      "version": 2
     },
     "conditions": [
      {
       "id": "84efd569-3df0-49c9-90be-34467a625884",
       "leftValue": "={{ $json.message.text }}",
       "rightValue": "",
       "operator": {
        "type": "string",
        "operation": "exists",
        "singleValue": true
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "id": "f440856a-1af9-4f3b-aaf7-832e78f85c45",
   "name": "Has text?",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.2,
   "position": [
    220,
    300
   ]
  },
  {
   "parameters": {
    "promptType": "define",
    "text": "={{ $json.message.text }}",
    "hasOutputParser": true,
    "options": {
     "systemMessage": "You are the Telegram support assistant for YOUR BUSINESS. Answer customer questions using ONLY what the read_faq tool returns: call it before you answer any question about products, prices, delivery, opening hours or policies. Keep replies under 80 words, friendly, plain text with no Markdown. Use the earlier messages in this chat for context. If the FAQ doesn't cover the question, or the customer asks for a refund, is upset, or wants a person, set needs_human to true and tell them a person will reply soon. Never invent prices, dates or policies."
    }
   },
   "id": "8d35f437-c6c4-4d94-ae4f-758e496de0b1",
   "name": "AI Agent",
   "type": "@n8n/n8n-nodes-langchain.agent",
   "typeVersion": 3.1,
   "position": [
    480,
    200
   ]
  },
  {
   "parameters": {
    "model": {
     "__rl": true,
     "mode": "id",
     "value": "gpt-6-luna"
    },
    "options": {}
   },
   "id": "b54a75df-5cf8-47c4-80e6-c04daefa4ecb",
   "name": "OpenAI Chat Model",
   "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
   "typeVersion": 1.3,
   "position": [
    320,
    460
   ]
  },
  {
   "parameters": {
    "sessionIdType": "customKey",
    "sessionKey": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
    "contextWindowLength": 10
   },
   "id": "1c9f1652-45db-4268-94dd-45ad7c792d95",
   "name": "Simple Memory",
   "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
   "typeVersion": 1.4,
   "position": [
    480,
    460
   ]
  },
  {
   "parameters": {
    "toolDescription": "read_faq: returns the text of the business FAQ page. Call it before answering any question about products, prices, delivery, opening hours or policies.",
    "url": "https://example.com/faq",
    "options": {},
    "optimizeResponse": true,
    "responseType": "html",
    "cssSelector": "body",
    "onlyContent": true,
    "truncateResponse": true,
    "maxLength": 8000
   },
   "id": "da704621-d6fa-4882-bec3-920418ce4bd2",
   "name": "Read FAQ page",
   "type": "n8n-nodes-base.httpRequestTool",
   "typeVersion": 4.5,
   "position": [
    640,
    460
   ]
  },
  {
   "parameters": {
    "schemaType": "fromJson",
    "jsonSchemaExample": "{\n  \"reply\": \"Yes, we ship to Germany. Delivery takes 3-5 working days.\",\n  \"needs_human\": false\n}"
   },
   "id": "ce5b2db2-d2b7-4bf0-b0f9-763292602c25",
   "name": "Structured Output Parser",
   "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
   "typeVersion": 1.3,
   "position": [
    800,
    460
   ]
  },
  {
   "parameters": {
    "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
    "text": "={{ $json.output.reply.replaceAll('&','&amp;').replaceAll('<','&lt;').replaceAll('>','&gt;') }}",
    "additionalFields": {
     "appendAttribution": false,
     "parse_mode": "HTML"
    }
   },
   "id": "99382b83-8cd3-4bb4-a6ec-4c84daf18ab2",
   "name": "Send reply",
   "type": "n8n-nodes-base.telegram",
   "typeVersion": 1.2,
   "position": [
    880,
    200
   ],
   "webhookId": "f9c7af20-9839-40a6-a596-352f3ac1449a"
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "strict",
      "version": 2
     },
     "conditions": [
      {
       "id": "ee034678-05db-4524-9809-5be7cb3e2cf8",
       "leftValue": "={{ $('AI Agent').item.json.output.needs_human }}",
       "rightValue": "",
       "operator": {
        "type": "boolean",
        "operation": "true",
        "singleValue": true
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "id": "2fa5948c-5d45-41eb-a3ac-9a13e28971a2",
   "name": "Needs a human?",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.2,
   "position": [
    1100,
    200
   ]
  },
  {
   "parameters": {
    "chatId": "YOUR_OWN_CHAT_ID",
    "text": "=Needs a human: {{ String($('Telegram Trigger').item.json.message.from.first_name || '').replaceAll('&','&amp;').replaceAll('<','&lt;').replaceAll('>','&gt;') }} (chat {{ $('Telegram Trigger').item.json.message.chat.id }}) asked: {{ String($('Telegram Trigger').item.json.message.text).replaceAll('&','&amp;').replaceAll('<','&lt;').replaceAll('>','&gt;') }}",
    "additionalFields": {
     "appendAttribution": false,
     "parse_mode": "HTML"
    }
   },
   "id": "6de26871-cf4f-430e-b1d3-b42e6b3eb83a",
   "name": "Alert owner",
   "type": "n8n-nodes-base.telegram",
   "typeVersion": 1.2,
   "position": [
    1320,
    120
   ],
   "webhookId": "fc430386-f5eb-49b9-998e-89eeb033c477"
  },
  {
   "parameters": {
    "chatId": "={{ $json.message.chat.id }}",
    "text": "I can only read text messages for now. Please type your question.",
    "additionalFields": {
     "appendAttribution": false,
     "parse_mode": "HTML"
    }
   },
   "id": "ce7b1cd8-9022-4c45-a66f-0cb88c2850f7",
   "name": "Text only, please",
   "type": "n8n-nodes-base.telegram",
   "typeVersion": 1.2,
   "position": [
    480,
    0
   ],
   "webhookId": "ea510622-b509-486e-bc12-680c9411e217"
  },
  {
   "parameters": {
    "content": "## Telegram AI agent with memory in n8n\nFrom recipeagents.usestarbase.com/recipes/telegram-ai-agent-memory\n\n1. Connect credentials: Telegram (bot token from @BotFather) on the trigger and the three Telegram nodes, OpenAI on the chat model.\n2. Read FAQ page: put your own FAQ URL. Alert owner: put your own chat ID.\n3. Telegram allows one webhook per bot: test with a second bot, or unpublish while you test.\n4. Simple Memory is lost on restart and doesn't work in queue mode; swap in Postgres Chat Memory for production.\nKeep a human check on refunds and anything sensitive.",
    "height": 330,
    "width": 460
   },
   "id": "a4b9e9a0-e1e5-47a4-a224-3e541d00ae02",
   "name": "Read me",
   "type": "n8n-nodes-base.stickyNote",
   "typeVersion": 1,
   "position": [
    -520,
    40
   ]
  }
 ],
 "connections": {
  "Telegram Trigger": {
   "main": [
    [
     {
      "node": "Has text?",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Has text?": {
   "main": [
    [
     {
      "node": "AI Agent",
      "type": "main",
      "index": 0
     }
    ],
    [
     {
      "node": "Text only, please",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "AI Agent": {
   "main": [
    [
     {
      "node": "Send reply",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Send reply": {
   "main": [
    [
     {
      "node": "Needs a human?",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Needs a human?": {
   "main": [
    [
     {
      "node": "Alert owner",
      "type": "main",
      "index": 0
     }
    ],
    []
   ]
  },
  "OpenAI Chat Model": {
   "ai_languageModel": [
    [
     {
      "node": "AI Agent",
      "type": "ai_languageModel",
      "index": 0
     }
    ]
   ]
  },
  "Simple Memory": {
   "ai_memory": [
    [
     {
      "node": "AI Agent",
      "type": "ai_memory",
      "index": 0
     }
    ]
   ]
  },
  "Read FAQ page": {
   "ai_tool": [
    [
     {
      "node": "AI Agent",
      "type": "ai_tool",
      "index": 0
     }
    ]
   ]
  },
  "Structured Output Parser": {
   "ai_outputParser": [
    [
     {
      "node": "AI Agent",
      "type": "ai_outputParser",
      "index": 0
     }
    ]
   ]
  }
 },
 "pinData": {},
 "active": false,
 "settings": {
  "executionOrder": "v1"
 },
 "meta": {
  "templateCredsSetupCompleted": false
 },
 "tags": []
}