{
  "name": "n8n-text-flow",
  "nodes": [
    {
      "parameters": {
        "promptType": "define",
        "text": "=`\u0422\u044b \u2014 \u043c\u0443\u0434\u0440\u044b\u0439 \u0438 \u043e\u0447\u0435\u043d\u044c \u0434\u043e\u0431\u0440\u044b\u0439 \u043d\u0430\u0441\u0442\u0430\u0432\u043d\u0438\u043a \u043d\u0430 \u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441\u0435.\n\u0422\u0432\u043e\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u2014 \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043c\u0430\u0448\u043d\u044e\u044e \u0440\u0430\u0431\u043e\u0442\u0443.\n\n\u0418\u043c\u044f \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u0430: {{ $('Edit Fields').item.json.student_name }}\n\u041f\u043e\u043b: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438 \u043f\u043e\u043b \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u0430 \u0438 \u0441\u0442\u0440\u043e\u0433\u043e \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0439 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u0433\u043b\u0430\u0433\u043e\u043b\u043e\u0432 (\u0441\u0434\u0435\u043b\u0430\u043b/\u0441\u0434\u0435\u043b\u0430\u043b\u0430, \u043c\u043e\u043b\u043e\u0434\u0435\u0446/\u0443\u043c\u043d\u0438\u0446\u0430, \u0441\u043c\u043e\u0433/\u0441\u043c\u043e\u0433\u043b\u0430).\n\n\u041a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0437\u0430\u0434\u0430\u043d\u0438\u044f:\n{{ $('Edit Fields').item.json.lesson_desc }}\n\n---\n\u0420\u0410\u0411\u041e\u0422\u0410 \u0421\u0422\u0423\u0414\u0415\u041d\u0422\u0410:\n{{ $(\"Merge\").first() && $(\"Merge\").first().json.data ? \"[\u041f\u0420\u0418\u041a\u0420\u0415\u041f\u041b\u0415\u041d\u041e \u0418\u0417\u041e\u0411\u0420\u0410\u0416\u0415\u041d\u0418\u0415]\" : \"\" }}\n{{ $('Edit Fields').item.json.hw_text }}\n---\n\n\u0422\u0412\u041e\u0418 \u041f\u0420\u0410\u0412\u0418\u041b\u0410:\n1. \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0439\u0441\u044f \u043a \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0438 \u043f\u0438\u0448\u0438 \u0444\u0438\u0434\u0431\u0435\u043a \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u0440\u043e\u0434\u0435 (\u043e\u043d/\u043e\u043d\u0430).\n2. \u0415\u0441\u043b\u0438 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u0435\u0441\u0442\u044c \u0442\u0435\u0433[\u041f\u0420\u0418\u041a\u0420\u0415\u041f\u041b\u0415\u041d\u041e \u0418\u0417\u041e\u0411\u0420\u0410\u0416\u0415\u041d\u0418\u0415], \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0439 \u0435\u0433\u043e \u0438 \u0443\u043f\u043e\u043c\u044f\u043d\u0438. \u0415\u0441\u043b\u0438 \u0442\u0435\u0433\u0430 \u043d\u0435\u0442, \u041d\u0418\u041a\u041e\u0413\u0414\u0410 \u043d\u0435 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u0439 \u0440\u0438\u0441\u0443\u043d\u043a\u0438/\u0444\u043e\u0442\u043e.\n3. \u0415\u0441\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430 \u043e\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u0430\u044f \u0438 \u043f\u043e \u0442\u0435\u043c\u0435 \u2014 \u0441\u0442\u0430\u0432\u044c \"is_approved\": true.\n4. \u0415\u0441\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043f\u0430\u0440\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \"777\", \"\u043e\u043a\") \u0438\u043b\u0438 \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u0430\u044f \u2014 \u0441\u0442\u0440\u043e\u0433\u043e \"is_approved\": false.\n5. \u0422\u0432\u043e\u0439 \"feedback_text\" \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043b\u0438\u0447\u043d\u044b\u043c, \u043e\u0431\u043e\u0434\u0440\u044f\u044e\u0449\u0438\u043c \u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0442\u044c \u0432\u0435\u0440\u0434\u0438\u043a\u0442.\n\n\u041e\u0442\u0432\u0435\u0442 \u0421\u0422\u0420\u041e\u0413\u041e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON. JSON \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0432\u0430 \u043a\u043b\u044e\u0447\u0430: \"is_approved\" (boolean) \u0438 \"feedback_text\" (string). \u0412\u0435\u0440\u043d\u0438 \u0442\u043e\u043b\u044c\u043a\u043e JSON \u0431\u0435\u0437 \u043c\u0430\u0440\u043a\u0434\u0430\u0443\u043d-\u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0438 (\u0431\u0435\u0437 json).`",
        "options": {
          "systemMessage": "\u0412\u0435\u0440\u043d\u0438 \u0422\u041e\u041b\u042c\u041a\u041e \u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0439 JSON \u0431\u0435\u0437 \u043c\u0430\u0440\u043a\u0434\u0430\u0443\u043d-\u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0438 (\u0431\u0435\u0437 ```json), \u0431\u0435\u0437 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0439 \u0438 \u043b\u0438\u0448\u043d\u0438\u0445 \u0441\u043b\u043e\u0432."
        }
      },
      "id": "f584d596-9e0d-453d-8ef6-f1e60fd59feb",
      "name": "Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.7,
      "position": [
        304,
        384
      ],
      "retryOnFail": true,
      "notesInFlow": true,
      "alwaysOutputData": true,
      "maxTries": 5,
      "waitBetweenTries": 350,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "model": "qwen/qwen3.5-flash-02-23",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "typeVersion": 1,
      "position": [
        208,
        736
      ],
      "id": "696d5957-3f3a-41e6-9823-1b083d539c87",
      "name": "OpenRouter Chat Model",
      "credentials": {
        "openRouterApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "aa46a723-619e-42e9-8e51-49ba51813718",
        "authentication": "headerAuth",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -976,
        288
      ],
      "id": "6b18db91-9dc6-4974-8b78-7b8249ef2988",
      "name": "Webhook-homework",
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "080ee5fb-782f-47d6-9d64-98ec8e8c52f2",
              "name": "=student_user_id",
              "value": "={{ $json.body.student_user_id }}",
              "type": "string"
            },
            {
              "id": "78fcca79-e818-4b3c-803f-94deda4f3104",
              "name": "course_id",
              "value": "={{ $json.body.course_numeric_id }}",
              "type": "string"
            },
            {
              "id": "969420d2-791b-40a4-924d-ec7e332d3d6b",
              "name": "lesson_num",
              "value": "={{ $json.body.lesson_num }}",
              "type": "string"
            },
            {
              "id": "2d2c5f78-ecaf-408d-acdb-ff6d7a4d086d",
              "name": "admin_chat_id",
              "value": "={{ $json.body.admin_group_id }}",
              "type": "string"
            },
            {
              "id": "5b60f83f-1667-4f43-9ab5-e7aad6d988f1",
              "name": "target_message_id",
              "value": "={{ $json.body.original_admin_message_id || $json.body.admin_message_id }}",
              "type": "string"
            },
            {
              "id": "c324098f-8660-4435-acef-09352a9a0a45",
              "name": "student_name",
              "value": "={{ ($json.body.user_fullname || $json.body.student_name || '\u0421\u0442\u0443\u0434\u0435\u043d\u0442').split(' ')[0] }}",
              "type": "string"
            },
            {
              "id": "753df8c9-e2c2-4ea6-aef4-5608ef8f92b1",
              "name": "hw_text",
              "value": "={{ $json.body.homework_text || '[\u0422\u0435\u043a\u0441\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442]' }}",
              "type": "string"
            },
            {
              "id": "2653261d-4f8b-494d-8bb3-975e69a96b0f",
              "name": "hw_file_id",
              "value": "={{ $json.body.homework_file_id || '' }}",
              "type": "string"
            },
            {
              "id": "2168a0e7-3cb9-44a2-b21c-85050c3405b2",
              "name": "lesson_desc",
              "value": "={{ $json.body.lesson_assignment_description ||'\u041e\u0446\u0435\u043d\u0438\u0442\u044c \u0414\u0417' }}",
              "type": "string"
            },
            {
              "id": "e55934e8-9d40-41ff-bba3-2a0919b1847e",
              "name": "",
              "value": "",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -720,
        288
      ],
      "id": "0a69ffd5-8c95-4d51-81e4-927c7a5c1048",
      "name": "Edit Fields",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Webhook-homework').item.json.body.callback_webhook_url_result }}\n",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-CALLBACK-SIGNATURE",
              "value": "=500"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "feedback_text",
              "value": "={{ $('Code').item.json.feedback_text }}"
            },
            {
              "name": "is_approved",
              "value": "={{ $('Code').item.json.is_approved }}"
            },
            {
              "name": "original_admin_message_id",
              "value": "={{ $('Webhook-homework').item.json.body.original_admin_message_id || $('Webhook-homework').item.json.body.admin_message_id }}"
            },
            {
              "name": "student_user_id",
              "value": "={{ $('Edit Fields').item.json.student_user_id }}"
            },
            {
              "name": "course_numeric_id",
              "value": "={{ $('Webhook-homework').item.json.body.course_numeric_id }}"
            },
            {
              "name": "lesson_num",
              "value": "={{ $('Webhook-homework').item.json.body.lesson_num }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1008,
        320
      ],
      "id": "9b9f511b-e63b-4e68-be9d-f492029149a4",
      "name": "HTTP Request",
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "mode": "combine",
        "combineBy": "combineByPosition",
        "options": {
          "includeUnpaired": true
        }
      },
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        32,
        448
      ],
      "id": "50aeb7e0-16d6-4b4e-9595-88d6ef1bc633",
      "name": "Merge",
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Webhook-homework').item.json.body.callback_webhook_url_result }}\n",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-CALLBACK-SIGNATURE",
              "value": "=500"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "feedback_text",
              "value": "=OK  \u0412\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043e\u0434\u043e\u0431\u0440\u0438\u043b\u0438 \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435!"
            },
            {
              "name": "is_approved",
              "value": "=True"
            },
            {
              "name": "original_admin_message_id",
              "value": "={{ $('Webhook-homework').item.json.body.original_admin_message_id }}"
            },
            {
              "name": "student_user_id",
              "value": "={{ $('Edit Fields').item.json.student_user_id }}"
            },
            {
              "name": "course_numeric_id",
              "value": "={{ $('Webhook-homework').item.json.body.course_numeric_id }}"
            },
            {
              "name": "lesson_num",
              "value": "={{ $('Webhook-homework').item.json.body.lesson_num }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1024,
        608
      ],
      "id": "7f936b8d-dfe5-4a16-a5f1-f496b1961371",
      "name": "HTTP Request2",
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "disabled": true,
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $json.homework_file_id }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -336,
        480
      ],
      "id": "08842b94-8efb-4963-aca8-a3699ea9e9ac",
      "name": "Get a file",
      "alwaysOutputData": false,
      "retryOnFail": true,
      "waitBetweenTries": 200,
      "maxTries": 2,
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "5b1189a6-6cee-49af-ad61-c91de051a0ea",
              "leftValue": "={{ $('Edit Fields').item.json.hw_file_id  }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -496,
        640
      ],
      "id": "931a2758-9023-4b38-a7fa-921481ad78d8",
      "name": "If"
    },
    {
      "parameters": {
        "jsCode": "const rawOutput = $('Agent').first().json.output;\n\nlet isApproved = true; \nlet feedbackText = `\u0410\u0433\u0435\u043d\u0442 \u0432\u0435\u0440\u043d\u0443\u043b \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442, \u043d\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u043e\u0434\u043e\u0431\u0440\u0435\u043d\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \\n\\n(\u041e\u0442\u0432\u0435\u0442 \u0418\u0418: ${rawOutput})`;\n\nconst jsonMatch = rawOutput.match(/(\\{[\\s\\S]*\\})/);\n\nif (jsonMatch && jsonMatch[1]) {\n  try {\n    const parsedData = JSON.parse(jsonMatch[1]);\n    if (typeof parsedData.is_approved === 'boolean') isApproved = parsedData.is_approved;\n    if (typeof parsedData.feedback_text === 'string') feedbackText = parsedData.feedback_text;\n  } catch (e) {\n    console.error(\"Parse error:\", e.message);\n  }\n} else {\n  feedbackText = rawOutput;\n  isApproved = true; \n}\n\nreturn {\n  json: {\n    is_approved: isApproved,\n    feedback_text: feedbackText\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        768,
        352
      ],
      "id": "06d0295a-492d-4f41-897a-7c8171ccabed",
      "name": "Code",
      "onError": "continueErrorOutput"
    }
  ],
  "connections": {
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Agent": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook-homework": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a file": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ],
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Get a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "58396c2e-09c3-44dc-a396-6c6a5b7f863e",
  "id": "2VqyCrDCshzIrBot",
  "tags": []
}