{
  "id": "dAwPcs1DtXANk4uu",
  "meta": {
    "builderVariant": "mcp",
    "aiBuilderAssisted": true
  },
  "name": "Answer Telegram messages with an AI agent using GPT-5, Perplexity and Wikipedia",
  "tags": [],
  "nodes": [
    {
      "id": "05348692-5051-441d-926b-5d18301d67da",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -1072,
        496
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "0f60d33c-e043-4889-b125-910a3938a9c6",
      "name": "Configuration",
      "type": "n8n-nodes-base.set",
      "position": [
        -848,
        496
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "system-prompt",
              "name": "system_prompt",
              "type": "string",
              "value": "You are Iris, a helpful and friendly personal AI assistant available on Telegram. You have access to 4 tools: a real-time web search (Perplexity), Wikipedia, a calculator, and an internal thinking tool. Use the web search tool for anything time-sensitive, current, or that requires up-to-date facts (news, prices, weather, recent events). Use Wikipedia for general knowledge, history, and background information. Use the calculator for any arithmetic instead of computing it yourself. Use the thinking tool to plan multi-step or ambiguous answers before replying. Always reply in the same language the user wrote in. Keep replies concise, warm, and conversational, formatted for a Telegram chat (short paragraphs, light use of emoji, no heavy markdown tables). If you are not fully certain about a fact and a tool could verify it, use the tool rather than guessing."
            },
            {
              "id": "openai-model",
              "name": "openai_model",
              "type": "string",
              "value": "gpt-5-mini"
            },
            {
              "id": "perplexity-model",
              "name": "perplexity_model",
              "type": "string",
              "value": "sonar-pro"
            },
            {
              "id": "memory-context-window",
              "name": "memory_context_window",
              "type": "number",
              "value": 20
            },
            {
              "id": "max-reply-length",
              "name": "max_reply_length",
              "type": "number",
              "value": 4000
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "4b6d664b-dd17-436a-8a4e-fdd69896e166",
      "name": "Has Text Message",
      "type": "n8n-nodes-base.filter",
      "position": [
        -672,
        496
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              },
              "leftValue": "={{ $json.message.text }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "3c6b8c83-8287-42d4-bef1-cc2be5014402",
      "name": "Conversational Assistant",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -400,
        432
      ],
      "parameters": {
        "text": "={{ $json.message.text }}",
        "options": {
          "maxIterations": 10,
          "systemMessage": "={{ $('Configuration').item.json.system_prompt }}"
        },
        "promptType": "define"
      },
      "typeVersion": 3.1
    },
    {
      "id": "25321161-6651-4f0b-acc4-a9226bdf9d98",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -464,
        560
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Configuration').item.json.openai_model }}"
        },
        "options": {},
        "builtInTools": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "3fc024af-c516-461d-87a2-c0c3992b1e1c",
      "name": "Simple Memory (Per Chat)",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -288,
        576
      ],
      "parameters": {
        "sessionKey": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "sessionIdType": "customKey",
        "contextWindowLength": "={{ $('Configuration').item.json.memory_context_window }}"
      },
      "typeVersion": 1.4
    },
    {
      "id": "8df0b458-ce7e-48e3-ada1-a85f1fa85d9b",
      "name": "Search the web with Perplexity",
      "type": "n8n-nodes-base.perplexityTool",
      "position": [
        -112,
        560
      ],
      "parameters": {
        "model": "={{ $('Configuration').item.json.perplexity_model }}",
        "options": {},
        "messages": {
          "message": [
            {
              "content": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('query', 'A clear, specific web search query rephrased from the user question. Use this whenever the answer could have changed recently or needs up-to-date facts.') }}"
            }
          ]
        },
        "simplify": true,
        "requestOptions": {}
      },
      "credentials": {
        "perplexityApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "4ae0535c-6118-4000-9964-76c689684009",
      "name": "Wikipedia",
      "type": "@n8n/n8n-nodes-langchain.toolWikipedia",
      "position": [
        32,
        560
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "1aa4cef8-e853-47b1-96c7-b0afa62d57e1",
      "name": "Calculator",
      "type": "@n8n/n8n-nodes-langchain.toolCalculator",
      "position": [
        144,
        560
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "6cb78417-8d1d-4241-8be1-8a526a928e9e",
      "name": "Think",
      "type": "@n8n/n8n-nodes-langchain.toolThink",
      "position": [
        240,
        560
      ],
      "parameters": {},
      "typeVersion": 1.1
    },
    {
      "id": "4ece288c-6f35-4b24-8077-0f8f01fc400a",
      "name": "Send Reply",
      "type": "n8n-nodes-base.telegram",
      "position": [
        640,
        432
      ],
      "parameters": {
        "text": "={{ $json.output.length > $('Configuration').item.json.max_reply_length ? $json.output.slice(0, $('Configuration').item.json.max_reply_length - 3) + '...' : $json.output }}",
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "additionalFields": {
          "parse_mode": "Markdown",
          "appendAttribution": false,
          "disable_web_page_preview": true
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "245fa34e-537b-4b8a-8d7f-0dab85aea6e8",
      "name": "Sticky Note - Main",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        896,
        -336
      ],
      "parameters": {
        "color": 4,
        "width": 620,
        "height": 1708,
        "content": "# Answer Telegram messages with an AI agent using GPT-5, Perplexity and Wikipedia\n# \ud83d\udce5  [Open full documentation on Notion](https://automatisation.notion.site/Course-Answer-Telegram-messages-with-an-AI-agent-using-GPT-5-Perplexity-and-Wikipedia-3a03d6550fd98169bd84c742dbdcc1e0)\n\n## How it works\nThis workflow turns a Telegram bot into a general-purpose conversational AI assistant. Every message you send is answered by an AI Agent (GPT-5) that can call on real-time web search, Wikipedia, a calculator, and an internal reasoning tool to give accurate, well-researched answers. Each Telegram chat keeps its own short-term memory, so the assistant remembers the recent conversation.\n\n## Setup\n1. Create a Telegram bot with @BotFather and add its token as a Telegram credential.\n2. Select your Telegram credential on both Telegram nodes (Trigger and Send Reply).\n3. Connect your OpenAI credential on the OpenAI Chat Model node.\n4. Connect your Perplexity credential on the Perplexity node ([Perplexity](https://www.perplexity.ai)).\n5. Open the Configuration node and adjust the system prompt, model, and memory settings.\n6. Activate the workflow and message your bot on Telegram.\n\n## Requirements\n- An OpenAI API account (GPT-5 family models)\n- A Perplexity API account for real-time web search\n- A Telegram bot token from @BotFather\n\n## Customization\n- Edit `system_prompt` in the Configuration node to change the assistant persona and tone.\n- Swap `openai_model` for a different GPT-5 family model (mini/nano/pro) to balance cost vs. quality.\n- Adjust `memory_context_window` to control how many past messages the assistant remembers per chat.\n- Add more tools (e.g. Google Sheets, HTTP Request, a sub-workflow) to the AI Agent for extra capabilities.\n\nNeed help customizing?\nContact me for consulting and support : [Linkedin](https://www.linkedin.com/in/doctor-firass/)\n\n# MY NEW YOUTUBE CHANNEL\n\ud83d\udc49 [Subscribe to my new YouTube channel](https://www.youtube.com/@DrFiras_AI). Here I'll share videos and Shorts with practical tutorials and FREE templates for n8n.\n\n[![The AI Doctor](https://www.dr-firas.com/the-ai-doctor.png)](https://www.youtube.com/@DrFiras_AI)"
      },
      "typeVersion": 1
    },
    {
      "id": "745baae7-9b52-4a21-aa51-39183f992f09",
      "name": "Sticky Note - Configuration",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -864,
        208
      ],
      "parameters": {
        "color": 7,
        "width": 300,
        "height": 248,
        "content": "## 1. Configuration\nSet your system prompt, OpenAI model, Perplexity model, and memory window here \u2014 this is the only place you need to edit."
      },
      "typeVersion": 1
    },
    {
      "id": "6172f6e9-a447-4647-9018-43ff8ae86914",
      "name": "Sticky Note - Agent",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -512,
        208
      ],
      "parameters": {
        "color": 7,
        "width": 1000,
        "height": 508,
        "content": "## 2. AI Agent\nGPT-5 agent with 4 tools (Perplexity web search, Wikipedia, Calculator, Think) and per-chat memory (last N messages)."
      },
      "typeVersion": 1
    },
    {
      "id": "10e94509-732b-4d88-8919-a622158744a2",
      "name": "Sticky Note - Reply",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        512,
        208
      ],
      "parameters": {
        "color": 7,
        "width": 300,
        "height": 200,
        "content": "## 3. Reply\nSends the assistant's answer back to the same Telegram chat it came from."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "availableInMCP": true,
    "executionOrder": "v1"
  },
  "versionId": "471215de-9b76-4e13-9fde-2ee6bc20665d",
  "nodeGroups": [],
  "connections": {
    "Think": {
      "ai_tool": [
        [
          {
            "node": "Conversational Assistant",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Wikipedia": {
      "ai_tool": [
        [
          {
            "node": "Conversational Assistant",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Calculator": {
      "ai_tool": [
        [
          {
            "node": "Conversational Assistant",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Configuration": {
      "main": [
        [
          {
            "node": "Has Text Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Text Message": {
      "main": [
        [
          {
            "node": "Conversational Assistant",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Conversational Assistant",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Conversational Assistant": {
      "main": [
        [
          {
            "node": "Send Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory (Per Chat)": {
      "ai_memory": [
        [
          {
            "node": "Conversational Assistant",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Search the web with Perplexity": {
      "ai_tool": [
        [
          {
            "node": "Conversational Assistant",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    }
  }
}