{
  "id": "2UQv1DUZyq0doFXO",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Smart AI Support Assistant for Telegram",
  "tags": [
    {
      "id": "vBkoevN5y6qmKOjn",
      "name": "creator",
      "createdAt": "2025-11-28T06:46:33.413Z",
      "updatedAt": "2025-11-28T06:46:33.413Z"
    }
  ],
  "nodes": [
    {
      "id": "26316b57-cf86-4a89-bd9b-f4c471ab228d",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -8144,
        784
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "362d3f7e-6474-4f5b-bea7-ba1d59be3097",
      "name": "QuestionOrReply_Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        -7408,
        784
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Question",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "46793cfc-f20c-4ea6-b010-e04f086dd408",
                    "operator": {
                      "type": "boolean",
                      "operation": "false",
                      "singleValue": true
                    },
                    "leftValue": "={{$json[\"reply_flag\"]}}",
                    "rightValue": ""
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "ReplyExpert",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "b7ed7e6d-2e35-4e8c-af83-d5d7553c8179",
                    "operator": {
                      "type": "boolean",
                      "operation": "true",
                      "singleValue": true
                    },
                    "leftValue": "={{$json[\"reply_flag\"]}}",
                    "rightValue": ""
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.3
    },
    {
      "id": "e953e4a1-84ea-4ed4-8998-224d4b48cba8",
      "name": "QuestionFilter_Code",
      "type": "n8n-nodes-base.code",
      "position": [
        -7264,
        432
      ],
      "parameters": {
        "jsCode": "const keywords = [\n  \"\u043a\u0430\u043a\", \"\u0441\u043a\u043e\u043b\u044c\u043a\u043e\", \"\u043a\u043e\u0433\u0434\u0430\", \"\u0432\u043e \u0441\u043a\u043e\u043b\u044c\u043a\u043e\", \"\u043a\u0443\u0434\u0430\", \"\u043f\u043e\u0447\u0435\u043c\u0443\", \"\u043d\u0443\u0436\u043d\u043e\", \"\u043c\u043e\u0436\u043d\u043e\",\n  \"\u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c\", \"\u0433\u0434\u0435\", \"\u043a\u0442\u043e\", \"\u0437\u0430\u0447\u0435\u043c\", \"\u043e\u0431\u044f\u0437\u0430\u043d\", \"\u043d\u0430\u0434\u043e\", \"\u0441\u0442\u043e\u0438\u0442\", \"\u0434\u043e\u043b\u0436\u0435\u043d\",\n  \"\u0431\u044b\u0432\u0430\u044e\u0442\", \"\u0441 \u043a\u0435\u043c\", \"\u0434\u043b\u044f \u0447\u0435\u0433\u043e\", \"\u0435\u0441\u0442\u044c \u043b\u0438\", \"\u0443 \u043a\u043e\u0433\u043e\", \"\u043a\u043e\u043c\u0443\", \"\u043a\u0443\u0434\u0430 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f\",\n  \"\u0447\u0442\u043e \u044d\u0442\u043e\", \"\u043a\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u043e\"\n];\n\n// \u0422\u0435\u043a\u0441\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f\nconst text = $json?.message?.text || $json?.text || \"\";\n\n// \u043f\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u0441\u043b\u043e\u0432\u0430\u043c\nlet matchedByKeyword = false;\nfor (let word of keywords) {\n  if (text.toLowerCase().includes(word)) {\n    matchedByKeyword = true;\n    break;\n  }\n}\n\n// \u043f\u043e \u0437\u043d\u0430\u043a\u0443 \u0432\u043e\u043f\u0440\u043e\u0441\u0430\nconst matchedByQuestionMark = text.includes(\"?\");\n\n// \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0444\u043b\u0430\u0433: \u0432\u043e\u043f\u0440\u043e\u0441, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u0418\u041b\u0418 \u0437\u043d\u0430\u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0430\nconst matched = matchedByKeyword || matchedByQuestionMark;\n\n// \u043a\u043e\u043b-\u0432\u043e \u0441\u043b\u043e\u0432\nconst wordsCount = text.trim() ? text.trim().split(/\\s+/).length : 0;\n\nreturn [{\n  json: {\n    ...$json,\n    keywordMatch: matched,\n    matchedByKeyword,\n    matchedByQuestionMark,\n    wordsCount\n  }\n}];\n\n\n\n"
      },
      "typeVersion": 2
    },
    {
      "id": "3e2b5143-ea89-4bc4-bd97-33c4c7f4fde6",
      "name": "ValidQuestion_IF",
      "type": "n8n-nodes-base.if",
      "position": [
        -7072,
        432
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "906edb34-fa14-4570-b0fd-af339cf47d6f",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.keywordMatch }}",
              "rightValue": "true"
            },
            {
              "id": "b5b247cb-1ac1-4317-8ae6-897e25657885",
              "operator": {
                "type": "number",
                "operation": "gte"
              },
              "leftValue": "={{ $json.wordsCount }}",
              "rightValue": 2
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "f2a2ab5a-650c-49b3-9727-a29a0e470da0",
      "name": "CreateTicket_Code",
      "type": "n8n-nodes-base.code",
      "position": [
        -4384,
        416
      ],
      "parameters": {
        "jsCode": "return [{\n  json: {\n    question_id: $json.question_message_id || ($json.message && $json.message.message_id) || null,\n    chat_id: $json.group_id || $json.chat_id || ($json.message && $json.message.chat && $json.message.chat.id) || null,\n    user_id: $json.user_id || ($json.message && $json.message.from && $json.message.from.id) || null,\n    user_name: $json.username || ($json.message && $json.message.from && $json.message.from.username) || null,\n    question_text: $json.question || ($json.message && $json.message.text) || null,\n\n    // \u0441\u0442\u0430\u0440\u043e\u0435 \u043f\u043e\u043b\u0435 \u0438\u0437 \u0432\u0445\u043e\u0434\u0430\n    group_message_id: $json.group_message_id || null,\n\n    // \u041d\u041e\u0412\u041e\u0415: \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 ID \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438\u0437 Set Original Question\n    original_group_message_id: $json.original_group_message_id || ($json.message && $json.message.message_id) || null,\n\n    asked_at: new Date().toISOString(),\n    status: \"sent_to_expert\"\n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "fd625287-de5d-49d5-8135-687e4b4aa698",
      "name": "SendToExpert_Message",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -3024,
        416
      ],
      "parameters": {
        "text": "=\ud83c\udd98 \u041d\u043e\u0432\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441:\n\n<b>{{ $json.question_text }}</b>\n\n\u041e\u0442: @{{ $json.user_name }}\n\n\u0418\u041d\u0421\u0422\u0420\u0423\u041a\u0426\u0418\u042f: \u041d\u0430\u0436\u043c\u0438 \u0434\u043e\u043b\u0433\u0438\u043c \u043d\u0430\u0436\u0430\u0442\u0438\u0435\u043c \u043d\u0430 \u044d\u0442\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 > \u0412\u044b\u0431\u0435\u0440\u0438 \"Reply\" (\u041e\u0442\u0432\u0435\u0442\u0438\u0442\u044c) > \u041d\u0430\u043f\u0438\u0448\u0438 \u0441\u0432\u043e\u0439 \u043e\u0442\u0432\u0435\u0442 \u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u044c.\n\nQ:{{ $json.original_group_message_id }}\n",
        "chatId": "=YOUR_TELEGRAM_CHAT_ID",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "29c59baa-19fb-4fb4-894e-ec02622189d8",
      "name": "Reformulate_Message",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -7072,
        592
      ],
      "parameters": {
        "text": "=\u041d\u0435 \u043f\u043e\u043d\u044f\u043b \u0432\u043e\u043f\u0440\u043e\u0441 \ud83e\udd14 \u041f\u0435\u0440\u0435\u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u0443\u0439 \u0435\u0433\u043e \u2014 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439 \u0447\u0443\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u044c \u0434\u0435\u0442\u0430\u043b\u0435\u0439!",
        "chatId": "={{ $json.group_id }}",
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": "={{ $json.message.message_id }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "340bd6cb-9fc5-4009-97ae-4fdb392f0595",
      "name": "ExpertReply_Message",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -4704,
        864
      ],
      "parameters": {
        "text": "={{ $json.answer }}",
        "chatId": "={{ $json.chat_id }}",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": false,
          "reply_to_message_id": "={{ $json.question_id_from_reply }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2,
      "alwaysOutputData": false
    },
    {
      "id": "923710fd-e58f-451c-8ceb-3cc705854b51",
      "name": "IsExpert_Code",
      "type": "n8n-nodes-base.code",
      "position": [
        -6576,
        880
      ],
      "parameters": {
        "jsCode": "// \u0412\u043f\u0438\u0448\u0438 user_id \u0432\u0441\u0435\u0445 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432. \u0422\u0432\u043e\u0439 id \u0443\u0437\u043d\u0430\u0451\u0448\u044c \u0438\u0437 \u043b\u044e\u0431\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432 Telegram \u0447\u0435\u0440\u0435\u0437 n8n.\nconst experts = [\n    123456789,   // \u2190 \u0437\u0430\u043c\u0435\u043d\u0438 \u043d\u0430 \u0441\u0432\u043e\u0439 id, \u043f\u043e\u043a\u0430 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u0448\u044c\n    // \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0445 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432\n];\n\nconst userId = $json[\"message\"][\"from\"][\"id\"];\n\nreturn [{\n    json: {\n        isExpert: experts.includes(userId),\n        ...$json\n    }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "74fa4a67-0b4b-4e59-88d3-ff7ecc589982",
      "name": "IsExpert_IF",
      "type": "n8n-nodes-base.if",
      "position": [
        -6416,
        880
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "5d10945d-3273-4b7b-b071-810b7b887090",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{$json[\"isExpert\"]}}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "b6e2c1a9-9ecb-4a68-9fdd-832c401a35a5",
      "name": "PrepareReplyFlag_Code",
      "type": "n8n-nodes-base.code",
      "position": [
        -7632,
        784
      ],
      "parameters": {
        "jsCode": "const replyText = $json.message.reply_to_message?.text || \"\";\n// \u0418\u0449\u0435\u043c Q:XXX... \u0432 \u0442\u0435\u043a\u0441\u0442\u0435 (\u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e: \u043b\u044e\u0431\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u043a\u0440\u043e\u043c\u0435 \u043f\u0440\u043e\u0431\u0435\u043b\u0430)\nconst match = replyText.match(/Q:([^\\s]+)/);\nconst questionId = match ? match[1] : null;\n\nreturn [{\n  json: {\n    reply_flag: Boolean($json.message.reply_to_message),\n    question_id_from_reply: questionId,\n    ...$json\n  }\n}]\n\n\n\n"
      },
      "typeVersion": 2
    },
    {
      "id": "d8a7c0c3-ed71-4fe3-bf92-308926113bb1",
      "name": "Pinecone Vector Store",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePinecone",
      "position": [
        -4208,
        416
      ],
      "parameters": {
        "mode": "insert",
        "options": {},
        "pineconeIndex": {
          "__rl": true,
          "mode": "list",
          "value": ""
        }
      },
      "credentials": {
        "pineconeApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "d65544fe-5f43-4613-aecd-d91db55b87ab",
      "name": "Embeddings OpenAI",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        -4224,
        624
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "d0521d88-5d1e-45f5-9ba0-2b793adc5efb",
      "name": "Default Data Loader",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        -4112,
        576
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "question_message_id",
                "value": "={{ $json.question_id }}"
              },
              {
                "name": "chat_id",
                "value": "={{ $('CreateTicket_Code').item.json.chat_id }}"
              },
              {
                "name": "user_id",
                "value": "={{ $('CreateTicket_Code').item.json.user_id }}"
              },
              {
                "name": "user_name",
                "value": "={{ $('CreateTicket_Code').item.json.user_name }}"
              },
              {
                "name": "question_text",
                "value": "={{ $('CreateTicket_Code').item.json.question_text }}"
              },
              {
                "name": "answer",
                "value": "={{ $json.answer }}"
              },
              {
                "name": "group_message_id",
                "value": "={{ $('CreateTicket_Code').item.json.group_message_id }}"
              },
              {
                "name": "original_group_message_id",
                "value": "={{ $('CreateTicket_Code').item.json.original_group_message_id }}"
              }
            ]
          }
        },
        "textSplittingMode": "custom"
      },
      "typeVersion": 1.1
    },
    {
      "id": "f98b4e42-db86-4b36-8f26-b8a96759afd8",
      "name": "Recursive Character Text Splitter",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        -3904,
        704
      ],
      "parameters": {
        "options": {
          "splitCode": "markdown"
        },
        "chunkSize": 4000
      },
      "typeVersion": 1
    },
    {
      "id": "f6f174db-08dc-4a82-830d-7f0de70ca216",
      "name": "Pinecone_ParseResult_Code",
      "type": "n8n-nodes-base.code",
      "position": [
        -3328,
        416
      ],
      "parameters": {
        "jsCode": "// \u0411\u0435\u0440\u0451\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0438\u0437 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 Pinecone\nconst doc = $json.result ? $json.result[0] : $json; // \u0435\u0441\u043b\u0438 Pinecone \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 result\nreturn [{\n  json: {\n    question: doc.metadata?.question,\n    answer: doc.metadata?.answer,\n    group_id: doc.metadata?.group_id,\n    question_message_id: doc.metadata?.question_message_id,\n    ...doc.metadata, // \u0435\u0441\u043b\u0438 \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0435\u0439\n  }\n}]\n"
      },
      "typeVersion": 2
    },
    {
      "id": "04e03f39-85d6-40db-b291-5028d8e762da",
      "name": "Set_QA_for_Pinecone",
      "type": "n8n-nodes-base.set",
      "position": [
        -4320,
        864
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "c45d6ea7-97b7-4e77-aaa2-67846ff578ff",
              "name": "=question",
              "type": "string",
              "value": "={{ $('GetFirstResult').item.json.question }}"
            },
            {
              "id": "f18be8b3-40ab-4ae5-a8f9-24e3fd0e10dd",
              "name": "answer",
              "type": "string",
              "value": "={{ $json.result.text }}"
            },
            {
              "id": "4e793ea9-f20c-433d-a290-d4c06bac9e5a",
              "name": "user_id",
              "type": "string",
              "value": "={{ $('GetFirstResult').item.json.user_id }}"
            },
            {
              "id": "2b92b109-6950-4d00-a859-8cfeb706696f",
              "name": "group_id",
              "type": "string",
              "value": "={{ $('GetFirstResult').item.json.chat_id }}"
            },
            {
              "id": "5601ae32-d3e7-4f43-b133-87ecade4b47d",
              "name": "question_message_id",
              "type": "string",
              "value": "={{ $('GetFirstResult').item.json.question_id_from_reply }}"
            },
            {
              "id": "514addd7-8096-45eb-87f3-7c10505bf47b",
              "name": "username",
              "type": "string",
              "value": "={{ $('GetFirstResult').item.json.username }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "986d4e64-fb81-4610-8cba-60fdfa17f18d",
      "name": "Embeddings OpenAI1",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        -3392,
        1072
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "2fb1d308-26ae-4c4e-b8cd-1341d0dbb1d5",
      "name": "EditFields_AddUserID_Metadata",
      "type": "n8n-nodes-base.set",
      "position": [
        -4672,
        416
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "5f91485b-215e-489e-9098-c5a7c3f8790a",
              "name": "group_id",
              "type": "string",
              "value": "={{ $json.chat_id }}"
            },
            {
              "id": "f6f78d61-d984-4176-a5f1-3048184c798d",
              "name": "user_id",
              "type": "string",
              "value": "={{ $json.user_id }}"
            },
            {
              "id": "84a17448-8a0f-465e-910d-4f37c7b79358",
              "name": "question_message_id",
              "type": "string",
              "value": "={{ $json.chat_id + \"_\" + $json.user_id + \"_\" + Date.now() }}"
            },
            {
              "id": "898151b3-c938-496b-b3b5-6a2f8c503a29",
              "name": "username",
              "type": "string",
              "value": "={{ $json.user_name }}"
            },
            {
              "id": "8bad6a33-a126-4c38-8095-dd52ac3f3df0",
              "name": "question",
              "type": "string",
              "value": "={{ $json.original_question }}"
            },
            {
              "id": "84f1a65c-f25d-40b7-ad8f-bb0e5151cbdf",
              "name": "group_message_id",
              "type": "string",
              "value": "={{ $json.group_message_id }}"
            },
            {
              "id": "955c3f1b-ebcd-4e71-a298-8cc4f951ae5e",
              "name": "original_group_message_id",
              "type": "string",
              "value": "={{ $json.original_message_id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "f43d5d49-9e09-46f3-9f25-505ff1af66f9",
      "name": "AddUserID_Metadata_Reply",
      "type": "n8n-nodes-base.set",
      "position": [
        -6176,
        864
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "d26380ec-cf49-4179-8eb8-54fd85c1fa61",
              "name": "user_id",
              "type": "string",
              "value": "={{$json[\"message\"][\"from\"][\"id\"]}}"
            },
            {
              "id": "ae6cefe6-97fb-4454-a43e-c189afc54a6e",
              "name": "reply_message_id",
              "type": "string",
              "value": "={{$json[\"message\"][\"reply_to_message\"][\"message_id\"]}}"
            },
            {
              "id": "d3da310a-6353-4d30-9ac9-3e6498ea1a00",
              "name": "chat_id",
              "type": "string",
              "value": "={{$json[\"message\"][\"chat\"][\"id\"]}}"
            },
            {
              "id": "c2a7d8b0-57dc-45da-8f6a-3441933ad010",
              "name": "question_message_id",
              "type": "string",
              "value": "={{$json.message.reply_to_message.message_id}}"
            },
            {
              "id": "3fd327b7-7078-4749-abcf-2c5b6146ca2d",
              "name": "username",
              "type": "string",
              "value": "={{ $json.message.chat.username }}"
            },
            {
              "id": "bedeebd1-4c37-4117-9010-fa27be567be9",
              "name": "",
              "type": "string",
              "value": ""
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "b3fb1fef-3682-48c9-bcda-350cb432fa7e",
      "name": "Pinecone_Add_Answer",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePinecone",
      "position": [
        -3376,
        864
      ],
      "parameters": {
        "mode": "insert",
        "options": {},
        "pineconeIndex": {
          "__rl": true,
          "mode": "list",
          "value": ""
        }
      },
      "credentials": {
        "pineconeApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "f4d05ecf-34af-47e1-8f69-b4c70a37dfd3",
      "name": "GetFirstResult",
      "type": "n8n-nodes-base.code",
      "position": [
        -5136,
        864
      ],
      "parameters": {
        "jsCode": "return [items[0]];  // \u0411\u0435\u0440\u0451\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442"
      },
      "typeVersion": 2
    },
    {
      "id": "1339177a-4aa6-41c7-a864-ea03e0a2a97a",
      "name": "Check If Answer Exists",
      "type": "n8n-nodes-base.if",
      "position": [
        -5712,
        400
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "010dd600-ed01-473b-92f9-bbdad4469131",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.has_answer }}",
              "rightValue": "true"
            },
            {
              "id": "97911638-14a6-42fb-bb6f-bcedd69e517a",
              "operator": {
                "type": "number",
                "operation": "gte"
              },
              "leftValue": "={{ $json.score }}",
              "rightValue": 0.85
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "206d2dc3-1634-4fb7-9d25-f070129a8791",
      "name": "Send Auto Answer",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -5344,
        112
      ],
      "parameters": {
        "text": "={{ $json.answer }}",
        "chatId": "={{ $json.chat_id }}",
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": "={{ $json.reply_to_message_id }}\n"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "6db06bff-5cf0-4cb7-ab36-986a40616974",
      "name": "Get First Answered Document",
      "type": "n8n-nodes-base.code",
      "position": [
        -5872,
        400
      ],
      "parameters": {
        "jsCode": "const originalQuestion =\n  items[0].json.original_question ||\n  items[0].json.question ||\n  \"\u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\";\n\nconst chatId =\n  items[0].json.chat_id ||\n  items[0].json.document?.metadata?.chat_id ||\n  items[0].json.document?.metadata?.group_id ||\n  items[0].json.group_id ||\n  \"\";\n\nconst replyToMessageId =\n  items[0].json.reply_to_message_id ||\n  items[0].json.message_id ||\n  items[0].json.document?.metadata?.question_message_id ||\n  \"\";\n\nconst userId = items[0].json.userid || items[0].json.user_id || \"\";\n\n// \u0411\u0435\u0440\u0451\u043c group_message_id \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c\nconst groupMessageId =\n  items[0].json.document?.metadata?.group_message_id || \"\";\n\n// \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u043e\u0433\nconst MIN_SCORE_THRESHOLD = 0.90;\n\n// \u0418\u0449\u0435\u043c item \u0441 \u043e\u0442\u0432\u0435\u0442\u043e\u043c\nconst found = items.find(item => {\n  const j = item.json || {};\n  const meta = j.document?.metadata || {};\n\n  const answer = meta.answer || j.answer || \"\";\n  const score = j.score || 0;\n  const keywordMatch = j.keywordMatch;\n\n  const hasAnswer = !!answer && answer.trim() !== \"\";\n  const keywordOk = (typeof keywordMatch === \"boolean\") ? keywordMatch : true;\n\n  return hasAnswer && keywordOk && score >= MIN_SCORE_THRESHOLD;\n});\n\nif (found) {\n  const j = found.json || {};\n  const meta = j.document?.metadata || {};\n\n  const foundAnswer =\n    meta.answer ||\n    j.answer ||\n    \"\";\n\n  return [{\n    json: {\n      ...j,\n      chat_id: chatId,\n      answer: foundAnswer,\n      text: foundAnswer,\n      reply_to_message_id: replyToMessageId,\n      group_message_id: groupMessageId,\n      original_message_id: items[0].json.original_message_id || \"\",\n      has_answer: true,\n      score: typeof j.score !== \"undefined\" ? j.score : 0,\n      user_name:\n        meta.username ||\n        meta.user_name ||\n        j.username ||\n        j.user_name ||\n        \"\",\n      user_id: userId\n    }\n  }];\n} else {\n  const backup = items[0].json;\n\n  return [{\n    json: {\n      chat_id: chatId,\n      text: \"\",\n      reply_to_message_id: replyToMessageId,\n      group_message_id: groupMessageId,\n      question: originalQuestion,\n      original_question: originalQuestion,\n      original_message_id: items[0].json.original_message_id || \"\",\n      has_answer: false,\n      answer: \"\",\n      score: typeof backup.score !== \"undefined\" ? backup.score : 0,\n      user_name:\n        backup.document?.metadata?.username ||\n        backup.document?.metadata?.user_name ||\n        backup.username ||\n        backup.user_name ||\n        \"\",\n      user_id: userId,\n      send_to_expert: true\n    }\n  }];\n}\n\n\n\n"
      },
      "typeVersion": 2
    },
    {
      "id": "f15dc9ab-1089-4f17-bfba-54c787dbf63f",
      "name": "Set Original Question",
      "type": "n8n-nodes-base.set",
      "position": [
        -6880,
        400
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "ea95983b-c336-433c-88d6-ada87c5b5409",
              "name": "original_text",
              "type": "string",
              "value": "={{$json.message.text}}"
            },
            {
              "id": "7f887d85-4220-4338-ac9a-a72720f57203",
              "name": "original_message_id",
              "type": "string",
              "value": "={{ $json.message.message_id }}"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "295b3c87-bf6e-4b62-afc7-8acee682471c",
      "name": "Restore Original Question",
      "type": "n8n-nodes-base.set",
      "position": [
        -6096,
        400
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "9ab3de56-7e5b-4fdc-ba2b-360dd262a919",
              "name": "original_question",
              "type": "string",
              "value": "={{ $('Set Original Question').item.json.message.text }}"
            },
            {
              "id": "25bf5a2f-0722-4e19-9fe5-0a98020af9c6",
              "name": "userid",
              "type": "string",
              "value": "={{ $('Set Original Question').item.json.message.from.id }}"
            },
            {
              "id": "3c14c68c-a385-4dfb-835a-26339da8edc8",
              "name": "username",
              "type": "string",
              "value": "={{ $('Set Original Question').item.json.message.from.username }}"
            },
            {
              "id": "5cef58a8-1121-4547-991f-8db9f48f1781",
              "name": "original_message_id",
              "type": "string",
              "value": "={{ $('Set Original Question').item.json.message.message_id }}"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "c262cf80-5ac9-49a8-89c7-b3a218768053",
      "name": "Filter Similar Answers",
      "type": "n8n-nodes-base.code",
      "position": [
        -6352,
        400
      ],
      "parameters": {
        "jsCode": "// \u0415\u0441\u043b\u0438 Pinecone \u0432\u0435\u0440\u043d\u0443\u043b \u043f\u0443\u0441\u0442\u043e \u2014 \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0441 \u043f\u0443\u0441\u0442\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\nif (!items || items.length === 0) {\n  return [{ json: { empty: true, items: [] } }];\n}\n\nconst threshold = 0.85;\n\nitems.forEach(item => {\n  // \u0412\u043e\u043f\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0434\u0430\u043b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\n  const inputQuestion =\n    item.json.original_question ||\n    item.json.message?.text ||\n    item.json.document?.metadata?.question_text ||\n    \"\";\n\n  const score = item.json.score || 0;\n\n  // \u0415\u0441\u0442\u044c \u043b\u0438 \u043e\u0442\u0432\u0435\u0442 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0435\n  const hasAnswer =\n    !!item.json.document?.metadata?.answer &&\n    item.json.document.metadata.answer.trim() !== \"\";\n\n  // \u0412\u043e\u043f\u0440\u043e\u0441 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\n  const question =\n    item.json.document?.metadata?.question_text ||\n    item.json.document?.pageContent ||\n    \"\";\n\n  // \u041a\u043b\u044e\u0447\u0435\u0432\u0430\u044f \u0444\u0440\u0430\u0437\u0430 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0432\u043e\u043f\u0440\u043e\u0441\u0430\n  const keyPhrase = inputQuestion.toLowerCase().slice(0, 5);\n\n  // \u041d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u0435\u043d\n  const isRelevant = question.toLowerCase().includes(keyPhrase);\n\n  // \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0442\u043b\u0430\u0434\u043e\u0447\u043d\u0443\u044e \u0438\u043d\u0444\u0443\n  item.json.debug = { question, score, hasAnswer, isRelevant, inputQuestion };\n\n  // \u0412\u0410\u0416\u041d\u041e: \u043d\u0435 \u0437\u0430\u0442\u0438\u0440\u0430\u0435\u043c metadata, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0432\u043e\u0438 \u0444\u043b\u0430\u0433\u0438\n  if (item.json.document && item.json.document.metadata) {\n    item.json.document.metadata = {\n      ...item.json.document.metadata,\n      hasAnswer,\n      isRelevant\n    };\n  }\n});\n\nreturn items;\n"
      },
      "typeVersion": 2,
      "alwaysOutputData": true
    },
    {
      "id": "f18d9141-3b8d-4cd9-b8fd-7637a1b66d01",
      "name": "CheckCacheHit",
      "type": "n8n-nodes-base.code",
      "position": [
        -3840,
        384
      ],
      "parameters": {
        "jsCode": "const inputData = $input.all()[0].json;\nconst pineconeResult = inputData.result || [];\nconst currentQuestion = inputData.question;\n\nlet cacheHit = false;\nlet cachedAnswer = null;\nlet score = 0; // \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0437\u0434\u0435\u0441\u044c\n\nif (pineconeResult.length > 0) {\n  const bestMatch = pineconeResult[0];\n  score = bestMatch.score || bestMatch.metadata?.score || 0;\n  if (score > 0.1) {\n    cacheHit = true;\n    cachedAnswer = '\u041e\u0442\u0432\u0435\u0442 \u0438\u0437 \u043a\u044d\u0448\u0430: ' + (bestMatch.metadata?.pageContent || bestMatch.pageContent || bestMatch.answer || '\u041d\u0435\u0442 \u0442\u0435\u043a\u0441\u0442\u0430');\n  }\n}\n\nreturn [{ json: { ...inputData, cacheHit, cachedAnswer, debugScore: score } }];\n\n"
      },
      "typeVersion": 2
    },
    {
      "id": "9df1bcbf-2c75-49f2-94d6-ed7a9e6e5898",
      "name": "CacheHitIF",
      "type": "n8n-nodes-base.if",
      "position": [
        -3632,
        400
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "a44e8f76-2219-4dec-a04c-33fa451f14ff",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.cacheHit }}",
              "rightValue": "true"
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "6185eafd-dec9-4638-b406-0d18676f04f9",
      "name": "SendCachedAnswer",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -3344,
        224
      ],
      "parameters": {
        "text": "={{ $json.cachedAnswer }}",
        "chatId": "={{ $json.group_id }}",
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": "={{ $json.question_text }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "0c5f17ff-2256-45c2-b2a7-f03ec739386f",
      "name": "Extract_Original_Question",
      "type": "n8n-nodes-base.code",
      "position": [
        -5648,
        864
      ],
      "parameters": {
        "jsCode": "const questionText = $json.message?.reply_to_message?.text || \"\";\n\n// \u0420\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438\nconst lines = questionText.split('\\n\\n');\nlet originalQuestion = \"\u0432\u043e\u043f\u0440\u043e\u0441 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\";\nlet questionIdFromReply = null;\n\n// \u0411\u0435\u0440\u0451\u043c \u0442\u0435\u043a\u0441\u0442 \u0432\u043e\u043f\u0440\u043e\u0441\u0430 (\u0432\u0442\u043e\u0440\u0430\u044f \u0441\u0435\u043a\u0446\u0438\u044f)\nif (lines.length >= 2) {\n  originalQuestion = lines[1];\n}\n\n// \u041f\u0430\u0440\u0441\u0438\u043c ID (Q:XXXX...) \u043b\u044e\u0431\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\nconst match = questionText.match(/Q:([^\\s]+)/);\nif (match) {\n  questionIdFromReply = match[1];\n}\n\n// \u041e\u0442\u0432\u0435\u0442 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u0430\nconst expertAnswer = $json.message?.text || \"\";\n\n// Username (\u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c)\nconst username = $json.message?.from?.username || \"\";\n\nreturn [{\n  json: {\n    question: originalQuestion,\n    answer: expertAnswer,\n    reply_message_id: $json.reply_message_id,\n    chat_id: \"\",\n    user_id: $json.user_id,\n    username: username,\n    question_id_from_reply: questionIdFromReply\n  }\n}];\n\n"
      },
      "typeVersion": 2
    },
    {
      "id": "b7bbb9f8-c2ca-4059-a327-05c2b6786708",
      "name": "Default Data Loader1",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        -3232,
        1072
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "question",
                "value": "={{ $json.question }}"
              },
              {
                "name": "answer",
                "value": "={{ $json.answer }}"
              },
              {
                "name": "user_id",
                "value": "={{ $json.user_id }}"
              },
              {
                "name": "group_id",
                "value": "={{ $json.group_id }}"
              },
              {
                "name": "question_message_id",
                "value": "={{ $json.question_message_id }}"
              },
              {
                "name": "username",
                "value": "={{ $json.username }}"
              }
            ]
          }
        },
        "jsonData": "={{ $json.question }}",
        "jsonMode": "expressionData",
        "textSplittingMode": "custom"
      },
      "typeVersion": 1.1
    },
    {
      "id": "b0609ced-eaa2-481d-b53a-7400f75ed531",
      "name": "WrapForPinecone",
      "type": "n8n-nodes-base.code",
      "position": [
        -4032,
        864
      ],
      "parameters": {
        "jsCode": "return {\n  json: {\n    question: $json.question,\n    answer: $json.answer,\n    user_id: $json.user_id,\n    group_id: $json.group_id,\n    question_message_id: $json.question_message_id,\n    username: $json.username,\n    pageContent: $json.question\n  }\n};\n"
      },
      "typeVersion": 2
    },
    {
      "id": "03413057-7865-4976-bdb0-e9f17a0cf11c",
      "name": "Recursive Character Text Splitter1",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        -3136,
        1280
      ],
      "parameters": {
        "options": {},
        "chunkSize": 10000
      },
      "typeVersion": 1
    },
    {
      "id": "e836f4cd-ede9-4e2a-971a-810a51ba9179",
      "name": "Extract Group ID",
      "type": "n8n-nodes-base.code",
      "position": [
        -7888,
        784
      ],
      "parameters": {
        "jsCode": "return [{\n  json: {\n    ...items[0].json,\n    group_id: items[0].json.message?.chat?.id || \"\"\n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "a9460553-3e37-4322-88d6-00d5c06de34b",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        -5504,
        304
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "9375c550-588b-4598-a849-b87ad976a265",
              "name": "answer",
              "type": "string",
              "value": "={{ $json.answer }}"
            },
            {
              "id": "2a535afe-4aca-4ebd-9952-c66fd9f1c892",
              "name": "chat_id",
              "type": "string",
              "value": "={{ $json.chat_id }}"
            },
            {
              "id": "0fcb4642-ec5d-444e-9ff1-4aa23f8f15d8",
              "name": "reply_to_message_id",
              "type": "string",
              "value": "={{ $json.original_message_id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "e9027078-a26b-43d2-803c-1af0fe4e599d",
      "name": "Embeddings OpenAI3",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        -6576,
        608
      ],
      "parameters": {
        "options": {
          "dimensions": 1536
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "85ca7657-5577-4966-812f-cba6b19c4d46",
      "name": "Search Answer In Pinecone",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePinecone",
      "position": [
        -6688,
        400
      ],
      "parameters": {
        "mode": "load",
        "topK": 3,
        "prompt": "={{ $json.original_text }}",
        "options": {},
        "pineconeIndex": {
          "__rl": true,
          "mode": "list",
          "value": ""
        }
      },
      "credentials": {
        "pineconeApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "bd6b0e80-53fd-4c33-bf6a-c0f9b779d1d8",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -9120,
        192
      ],
      "parameters": {
        "width": 816,
        "height": 544,
        "content": "## How it works\n\n1. **Question detection** checks each new message in the Telegram group chat and decides whether it\u2019s actually a question.  \n2. **Knowledge base search** looks for an existing answer in the Pinecone vector store using the user\u2019s question.  \n3. **Auto\u2011reply from cache** sends the saved answer straight back to the group if a good match is found, without involving an expert.  \n4. **Escalation to expert** creates a ticket and forwards the question to a separate Telegram bot that only the expert or curator sees when no answer exists yet.  \n5. **Expert learning loop** lets the expert reply manually once; the bot stores this answer in Pinecone so future similar questions are answered automatically.\n\n## Setup steps\n\n- Connect **Telegram Trigger** to a single Telegram bot that is added as an admin to the group/supergroup and receives all user messages.  \n- Use **the same bot** for the expert: the expert\u2019s private chat with this bot is where tickets and questions are delivered (no separate expert bot needed).  \n- Set up **Pinecone**: create an index, note the environment and index name, and add your Pinecone API key to n8n credentials.  \n- Add your **AI model API key** (for example, OpenAI) and select the model used for embeddings and answer rewriting.  \n- Configure any environment variables or n8n credentials for project IDs and spaces/namespaces used in Pinecone.  \n- Test the full flow: send a question in the group, confirm that a ticket reaches the expert in a private chat, reply once, and check that the next similar question is answered automatically from the cache.\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "1cde61b9-b6c0-4b22-a547-cf6e7f136020",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -8192,
        368
      ],
      "parameters": {
        "color": 7,
        "width": 1296,
        "height": 640,
        "content": "## Incoming messages & routing\nDetects whether a new Telegram message is a user question from the group \nor a reply from the expert, and only valid questions continue through the flow."
      },
      "typeVersion": 1
    },
    {
      "id": "9d60ae99-e735-435e-80b9-6f1a00fd16d4",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -6880,
        80
      ],
      "parameters": {
        "color": 7,
        "width": 1776,
        "height": 736,
        "content": "## Search and auto\u2011answer from knowledge base\nFor valid group questions, searches the Pinecone knowledge base for a similar question and, when a good match is found, sends an automatic answer back to the group chat."
      },
      "typeVersion": 1
    },
    {
      "id": "9ac0ea3b-5e23-452d-af62-48e47042a5e5",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -4720,
        96
      ],
      "parameters": {
        "color": 7,
        "width": 1888,
        "height": 736,
        "content": "## No answer found \u2192 send to expert\nWhen no suitable answer exists in Pinecone, creates a ticket and forwards the question to the expert in a private chat with the same bot so the expert can reply manually"
      },
      "typeVersion": 1
    },
    {
      "id": "7e4de7cc-6f7a-47e5-8f5b-ab2440aacf40",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -6816,
        832
      ],
      "parameters": {
        "color": 7,
        "width": 3856,
        "height": 736,
        "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n## Expert reply \u2192 learn & update\nCaptures the expert\u2019s reply, links it to the original question, updates the Pinecone index with the new Q&A pair, and sends the expert\u2019s answer back to the group so future similar questions can be answered automatically."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "3093e193-ae45-4ce0-b7ae-0f99a4eca2e0",
  "connections": {
    "CacheHitIF": {
      "main": [
        [
          {
            "node": "SendCachedAnswer",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Pinecone_ParseResult_Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Send Auto Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IsExpert_IF": {
      "main": [
        [
          {
            "node": "AddUserID_Metadata_Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CheckCacheHit": {
      "main": [
        [
          {
            "node": "CacheHitIF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IsExpert_Code": {
      "main": [
        [
          {
            "node": "IsExpert_IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GetFirstResult": {
      "main": [
        [
          {
            "node": "ExpertReply_Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "WrapForPinecone": {
      "main": [
        [
          {
            "node": "Pinecone_Add_Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Group ID": {
      "main": [
        [
          {
            "node": "PrepareReplyFlag_Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Extract Group ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ValidQuestion_IF": {
      "main": [
        [
          {
            "node": "Set Original Question",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reformulate_Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CreateTicket_Code": {
      "main": [
        [
          {
            "node": "Pinecone Vector Store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI": {
      "ai_embedding": [
        [
          {
            "node": "Pinecone Vector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI1": {
      "ai_embedding": [
        [
          {
            "node": "Pinecone_Add_Answer",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI3": {
      "ai_embedding": [
        [
          {
            "node": "Search Answer In Pinecone",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Default Data Loader": {
      "ai_document": [
        [
          {
            "node": "Pinecone Vector Store",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "ExpertReply_Message": {
      "main": [
        [
          {
            "node": "Set_QA_for_Pinecone",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "QuestionFilter_Code": {
      "main": [
        [
          {
            "node": "ValidQuestion_IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set_QA_for_Pinecone": {
      "main": [
        [
          {
            "node": "WrapForPinecone",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Default Data Loader1": {
      "ai_document": [
        [
          {
            "node": "Pinecone_Add_Answer",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Pinecone Vector Store": {
      "main": [
        [
          {
            "node": "CheckCacheHit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PrepareReplyFlag_Code": {
      "main": [
        [
          {
            "node": "QuestionOrReply_Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Original Question": {
      "main": [
        [
          {
            "node": "Search Answer In Pinecone",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If Answer Exists": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "EditFields_AddUserID_Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Similar Answers": {
      "main": [
        [
          {
            "node": "Restore Original Question",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "QuestionOrReply_Switch": {
      "main": [
        [
          {
            "node": "QuestionFilter_Code",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "IsExpert_Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AddUserID_Metadata_Reply": {
      "main": [
        [
          {
            "node": "Extract_Original_Question",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract_Original_Question": {
      "main": [
        [
          {
            "node": "GetFirstResult",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pinecone_ParseResult_Code": {
      "main": [
        [
          {
            "node": "SendToExpert_Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Restore Original Question": {
      "main": [
        [
          {
            "node": "Get First Answered Document",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Answer In Pinecone": {
      "main": [
        [
          {
            "node": "Filter Similar Answers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get First Answered Document": {
      "main": [
        [
          {
            "node": "Check If Answer Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "EditFields_AddUserID_Metadata": {
      "main": [
        [
          {
            "node": "CreateTicket_Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Character Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Default Data Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Character Text Splitter1": {
      "ai_textSplitter": [
        [
          {
            "node": "Default Data Loader1",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    }
  }
}