{
  "id": "RV4zArpzN5InxYND",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Conversational Telegram Bot with GPT-4o for Text and Voice Messages",
  "tags": [
    {
      "id": "hlEHEpOdSg5lhIqi",
      "name": "Template",
      "createdAt": "2025-06-05T15:35:52.689Z",
      "updatedAt": "2025-06-05T15:35:52.689Z"
    }
  ],
  "nodes": [
    {
      "id": "a33e3f45-f16e-4107-937e-0ce1d230060f",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -660,
        -180
      ],
      "parameters": {
        "updates": [
          "message",
          "*"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "e17f452c-4f9a-48f8-91a2-526260159676",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        520,
        420
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o",
          "cachedResultName": "gpt-4o"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "9b33ec46-8772-4bfb-a448-fea5a673bbaa",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        740,
        460
      ],
      "parameters": {
        "sessionKey": "={{ $json.sessionId }}",
        "sessionIdType": "customKey",
        "contextWindowLength": 10
      },
      "typeVersion": 1.3
    },
    {
      "id": "8cdd7ba8-c998-4ea7-b861-8dcaf12febfe",
      "name": "Get Voice",
      "type": "n8n-nodes-base.telegram",
      "position": [
        80,
        0
      ],
      "parameters": {
        "fileId": "={{ $json.message.voice.file_id }}",
        "resource": "file"
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "7fa34d0e-5215-44e5-b1d3-1e8abb5771ae",
      "name": "Text Or Voice",
      "type": "n8n-nodes-base.switch",
      "position": [
        -240,
        -180
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Text",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "0a236099-525e-4748-a57d-8b8e2c63c48b",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    },
                    "leftValue": "={{ $json.message.text }}",
                    "rightValue": ""
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Voice",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "24b7e8b8-6fce-4ddc-94b6-30b852891995",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    },
                    "leftValue": "={{  $json.message.voice.file_id }}",
                    "rightValue": ""
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "b90a586b-5d90-4f76-8e4e-bd44095f9aac",
      "name": "Transcribe The Voice",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        280,
        0
      ],
      "parameters": {
        "options": {},
        "resource": "audio",
        "operation": "transcribe"
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "587adf8a-2e69-4078-a603-84f0322207cb",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        720,
        80
      ],
      "parameters": {
        "text": "={{ $json.message.text }}",
        "options": {
          "systemMessage": "=# Overview\nYou are a personal assistant that helps user to fulfill their request.\n\nWhen you are asked to perform a task on the current date, please use the current time and date: {{$now}}\n\n## Output\nYou should output the result and don't include any link. Just mentioned where you added it to."
        },
        "promptType": "define"
      },
      "typeVersion": 1.9
    },
    {
      "id": "2455c7cb-f341-4891-9096-dc62116298c0",
      "name": "Typing Action",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -240,
        -360
      ],
      "parameters": {
        "chatId": "={{ $json.message.chat.id }}",
        "operation": "sendChatAction"
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8c2b6195-6101-4652-848c-505d13a3c1d2",
      "name": "Send Response To Telegram",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1340,
        80
      ],
      "parameters": {
        "text": "={{ $json.output }}",
        "chatId": "={{$('Telegram Trigger').first().json.message.chat.id}}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "c8fd047d-8575-484e-939a-46064b81d035",
      "name": "Send Context To AI Agent",
      "type": "n8n-nodes-base.set",
      "position": [
        460,
        -200
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "590b803f-833c-4854-bde7-5241d25b2b32",
              "name": "message.text",
              "type": "string",
              "value": "={{ $json.message.text }}"
            },
            {
              "id": "303e0b5d-3d37-48fc-bbd3-ce62dbe7ef78",
              "name": "sessionId",
              "type": "string",
              "value": "={{ $json.message.chat.id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "f7e90526-08a5-4a59-956f-d39e6ff472f0",
      "name": "Send Transcribe Context To AI Agent",
      "type": "n8n-nodes-base.set",
      "position": [
        460,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "2bb71aef-0ca1-4c26-95e1-c0c83bc82068",
              "name": "message.text",
              "type": "string",
              "value": "={{ $json.text }}"
            },
            {
              "id": "692a2bae-35da-41d3-be8d-32c0f5dfbbef",
              "name": "sessionId",
              "type": "string",
              "value": "={{ $('Telegram Trigger').item.json.message.chat.id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    }
  ],
  "active": false,
  "settings": {
    "timezone": "Australia/Sydney",
    "callerPolicy": "workflowsFromSameOwner",
    "executionOrder": "v1"
  },
  "versionId": "3a29c25b-e7bd-41a7-91ee-4438fa9c1c1a",
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send Response To Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Voice": {
      "main": [
        [
          {
            "node": "Transcribe The Voice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Text Or Voice": {
      "main": [
        [
          {
            "node": "Send Context To AI Agent",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get Voice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Typing Action": {
      "main": [
        []
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Text Or Voice",
            "type": "main",
            "index": 0
          },
          {
            "node": "Typing Action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Transcribe The Voice": {
      "main": [
        [
          {
            "node": "Send Transcribe Context To AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Context To AI Agent": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Transcribe Context To AI Agent": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}