{
  "id": "3t4JhODy5nElDpwd",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Supervisor",
  "tags": [],
  "nodes": [
    {
      "id": "6ed56a8f-dbd3-4d51-9959-0980dc8b7f6e",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        -416,
        -352
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "7374aeb6-2a79-474b-90ee-783e412f5247",
              "operator": {
                "type": "number",
                "operation": "equals"
              },
              "leftValue": "={{ $json.message.from.id }}",
              "rightValue": 6419832574
            },
            {
              "id": "6de78c03-587a-47e9-9b91-452183a83982",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.message.entities[0].type }}",
              "rightValue": "bot_command"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "f352760a-fd98-4133-a050-aabaf079a3d5",
      "name": "Error Authorization",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -144,
        -160
      ],
      "parameters": {
        "text": "=Hi ~ {{ $json.message.chat.username }}\nYou are not authorized, thk !",
        "chatId": "={{ $json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": "={{ $json.message.message_id }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "154f84bf-370b-4dfb-8f08-99f2b4976ebe",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        96,
        -512
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "start",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "032f2ecc-3100-43cc-82db-aa44866cdf4d",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.command }}",
                    "rightValue": "start"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "help",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "d73cb511-1cef-488c-b5b8-13d31f900f97",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.command }}",
                    "rightValue": "help"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "add",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "8d2cbba2-dbf6-43e7-a2ea-7bb4c0d52627",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.command }}",
                    "rightValue": "add"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "list",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "29a24cc8-d341-40a7-bf1d-660e078bca25",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.command }}",
                    "rightValue": "list"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "delete",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "953a9f37-fd41-4b60-b9da-c88a3d15c02c",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.command }}",
                    "rightValue": "delete"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "b373c3aa-0187-4eda-ac34-a7888375b2d4",
      "name": "Test return success",
      "type": "n8n-nodes-base.telegram",
      "position": [
        656,
        -880
      ],
      "parameters": {
        "text": "=Hi ~ {{ $json.message.chat.username }}\nTest return Success !",
        "chatId": "={{ $('If').first().json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": "={{ $('If').first().json.message.message_id }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "953baaa1-cf0a-4f8e-b559-6570872dbb35",
      "name": "Parse user text",
      "type": "n8n-nodes-base.code",
      "position": [
        -144,
        -464
      ],
      "parameters": {
        "jsCode": "// \u4ece\u8f93\u5165\u6570\u636e\u4e2d\u83b7\u53d6\u6d88\u606f\u7684\u6838\u5fc3\u4fe1\u606f\nconst message = $input.item.json.message;\nconst text = message.text.trim(); // \u4f7f\u7528 trim() \u79fb\u9664\u9996\u5c3e\u591a\u4f59\u7a7a\u683c\nconst chatId = message.chat.id;\n\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6807\u51c6\u7684\u7ed3\u679c\u5bf9\u8c61\nconst result = {\n  chatId: chatId,\n  command: null,\n  symbol: null,\n  interval: null,\n  prompt: null,\n  error: null\n};\n\n// \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u6355\u83b7\u6307\u4ee4\u548c\u5176\u540e\u7684\u6240\u6709\u5185\u5bb9\nconst baseRegex = /^\\/(\\w+)\\s*(.*)$/;\nconst baseMatch = text.match(baseRegex);\n\n// \u5982\u679c\u8fde\u57fa\u7840\u7684 /<command> \u683c\u5f0f\u90fd\u5339\u914d\u4e0d\u4e0a\uff0c\u76f4\u63a5\u8fd4\u56de\u9519\u8bef\nif (!baseMatch) {\n  result.error = \"\u65e0\u6cd5\u8bc6\u522b\u7684\u6307\u4ee4\u683c\u5f0f\u3002\";\n  return result;\n}\n\n// \u63d0\u53d6\u6307\u4ee4\u548c\u53c2\u6570\u90e8\u5206\nconst command = baseMatch[1].toLowerCase();\nconst argsString = baseMatch[2]; // \u6307\u4ee4\u540e\u9762\u7684\u6240\u6709\u53c2\u6570\u5b57\u7b26\u4e32\n\nresult.command = command;\n\n// \u4f7f\u7528 switch \u8bed\u53e5\u6765\u6839\u636e\u4e0d\u540c\u7684\u6307\u4ee4\u8fdb\u884c\u89e3\u6790\nswitch (command) {\n  case 'add':\n    // \u89e3\u6790 /add SYMBOL INTERVAL PROMPT\n    const addArgs = argsString.match(/^([A-Z0-9]+)\\s+([a-zA-Z0-9]+)\\s+(.*)$/);\n    if (addArgs) {\n      result.symbol = addArgs[1];\n      result.interval = addArgs[2];\n      result.prompt = addArgs[3];\n    } else {\n      result.error = \"`/add` \u6307\u4ee4\u683c\u5f0f\u9519\u8bef\u3002\u6b63\u786e\u683c\u5f0f: /add <SYMBOL> <INTERVAL> <PROMPT>\";\n    }\n    break;\n\n  case 'search':\n    // \u89e3\u6790 /search SYMBOL INTERVAL\n    const searchArgs = argsString.match(/^([A-Z0-9]+)\\s+([a-zA-Z0-9]+)$/);\n    if (searchArgs) {\n      result.symbol = searchArgs[1];\n      result.interval = searchArgs[2];\n    } else {\n      result.error = \"`/search` \u6307\u4ee4\u683c\u5f0f\u9519\u8bef\u3002\u6b63\u786e\u683c\u5f0f: /search <SYMBOL> <INTERVAL>\";\n    }\n    break;\n\n  case 'start':\n  case 'help':\n    // \u8fd9\u4e24\u4e2a\u6307\u4ee4\u6ca1\u6709\u53c2\u6570\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u989d\u5916\u89e3\u6790\n    // \u5982\u679c argsString \u4e0d\u662f\u7a7a\u7684\uff0c\u8bf4\u660e\u7528\u6237\u591a\u8f93\u5165\u4e86\u5185\u5bb9\uff0c\u4f46\u6211\u4eec\u9009\u62e9\u5ffd\u7565\n    break;\n  case 'list':\n    break;\n  case 'delete':\n    // \u89e3\u6790 /delete SYMBOL\n    // \u6b63\u5219\u8868\u8fbe\u5f0f\u53ea\u5339\u914d\u4e00\u4e2a\u53c2\u6570 (\u4ea4\u6613\u5bf9)\n    const deleteArgs = argsString.match(/^([A-Z0-9]+)$/);\n    if (deleteArgs) {\n      result.symbol = deleteArgs[1];\n    } else {\n      result.error = \"`/delete` \u6307\u4ee4\u683c\u5f0f\u9519\u8bef\u3002\u6b63\u786e\u683c\u5f0f: /delete <SYMBOL>\";\n    }\n    break;\n\n  default:\n    // \u5982\u679c\u6307\u4ee4\u4e0d\u5728\u6211\u4eec\u7684\u5904\u7406\u5217\u8868\u91cc\n    result.error = `\u672a\u77e5\u7684\u6307\u4ee4: /${command}`;\n    break;\n}\n\n// \u8fd4\u56de\u6700\u7ec8\u89e3\u6790\u7684\u7ed3\u679c\nreturn result;"
      },
      "typeVersion": 2
    },
    {
      "id": "8084b2ff-9fc1-4256-9bd0-1b22f67121cc",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        880,
        -528
      ],
      "parameters": {
        "jsCode": "// \u4ece\u8f93\u5165\u6570\u636e\u4e2d\u83b7\u53d6\u53c2\u6570\nconst symbol = $('Switch').first().json.symbol || 'BTCUSDT';\nconst interval = $('Switch').first().json.interval || '1h';\nconst prompt = $('Switch').first().json.prompt || '\u8dcc\u7834\u65b0\u4f4e';\n\n// \u6839\u636e interval \u8bbe\u7f6e\u8c03\u5ea6\u89c4\u5219\nlet scheduleRule;\nif (interval === '1h') {\n  scheduleRule = {\n    \"interval\": [\n      {\n        \"field\": \"hours\",\n        \"hoursInterval\": 1\n      }\n    ]\n  };\n} else if (interval === '15m') {\n  scheduleRule = {\n    \"interval\": [\n      {\n        \"field\": \"minutes\",\n        \"minutesInterval\": 15\n      }\n    ]\n  };\n} else if (interval === '1d') {\n  scheduleRule = {\n    \"interval\": [\n      {\n        \"field\": \"days\",\n        \"daysInterval\": 1\n      }\n    ]\n  };\n} else {\n  scheduleRule = {\n    \"interval\": [\n      {\n        \"field\": \"hours\",\n        \"hoursInterval\": 1\n      }\n    ]\n  };\n}\n\n// \u4fee\u590d Code \u8282\u70b9\u7684 JavaScript \u4ee3\u7801 - \u53bb\u6389\u53cc\u91cd\u8f6c\u4e49\nconst codeNodeJS = \"// --- \u6211\u4eec\u53ea\u4fee\u6539\u4e86\u8fd9\u4e00\u884c ---\\n// \u83b7\u53d6 AI \u8282\u70b9\u8f93\u51fa\u7684\u3001\u5d4c\u5957\u8f83\u6df1\u7684\u539f\u59cb\u5b57\u7b26\u4e32\\nconst aiRawOutput = $json.content.parts[0].text;\\n\\n// --- \u540e\u7eed\u4ee3\u7801\u4e0e\u4e4b\u524d\u5b8c\u5168\u76f8\u540c\uff0c\u65e0\u9700\u6539\u52a8 ---\\n\\n// \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u88ab \u00b4\u00b4\u00b4 \u5305\u88f9\u7684 JSON \u5b57\u7b26\u4e32\u90e8\u5206\\nconst jsonMatch = aiRawOutput.match(/\\\\{[\\\\s\\\\S]*\\\\}/);\\n\\n// \u68c0\u67e5\u662f\u5426\u6210\u529f\u627e\u5230\u4e86 JSON \u5b57\u7b26\u4e32\\nif (jsonMatch && jsonMatch[0]) {\\n  try {\\n    // \u5c06\u63d0\u53d6\u51fa\u6765\u7684\u7eaf JSON \u5b57\u7b26\u4e32\uff0c\u89e3\u6790\u6210\u4e00\u4e2a\u771f\u6b63\u7684 JSON \u5bf9\u8c61\\n    const parsedData = JSON.parse(jsonMatch[0]);\\n    \\n    // \u5c06\u8fd9\u4e2a\u89e3\u6790\u540e\u7684\u5bf9\u8c61\u4f5c\u4e3a\u672c\u8282\u70b9\u7684\u8f93\u51fa\\n    return {\\n      json: parsedData\\n    };\\n  } catch (error) {\\n    // \u5982\u679c\u89e3\u6790\u5931\u8d25\uff0c\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\\n    return { json: { error: \\\"\u89e3\u6790JSON\u5931\u8d25\\\", details: error.message } };\\n  }\\n}\\n\\n// \u5982\u679c\u8fde JSON \u5b57\u7b26\u4e32\u90fd\u627e\u4e0d\u5230\uff0c\u4e5f\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\\nreturn { json: { error: \\\"\u5728AI\u8f93\u51fa\u4e2d\u672a\u627e\u5230JSON\u5757\\\" } };\";\n\n// \u751f\u6210\u7b26\u5408 n8n API \u8981\u6c42\u7684 workflow JSON\nconst workflowJson = {\n  \"name\": \"WatchDog-\" + symbol,\n  \"nodes\": [\n    {\n      \"parameters\": {\n        \"operation\": \"executeQuery\",\n        \"query\": \"SELECT * FROM watchdog WHERE symbol = '\" + symbol + \"';\",\n        \"options\": {}\n      },\n      \"type\": \"n8n-nodes-base.postgres\",\n      \"typeVersion\": 2,\n      \"position\": [-176, -80],\n      \"name\": \"Execute a SQL query\",\n      \"credentials\": {\n        \"postgres\": {\n          \"id\": \"cOj0Kt9xrVLHTEvg\",\n          \"name\": \"Self-PostgreSQL\"\n        }\n      }\n    },\n    {\n      \"parameters\": {\n        \"rule\": scheduleRule\n      },\n      \"type\": \"n8n-nodes-base.scheduleTrigger\",\n      \"typeVersion\": 1,\n      \"position\": [-864, -80],\n      \"name\": \"Schedule Trigger\"\n    },\n    {\n      \"parameters\": {\n        \"assignments\": {\n          \"assignments\": [\n            {\n              \"id\": \"7931ff5e-f133-4a07-b81a-7a2c3251e73d\",\n              \"name\": \"symbol\",\n              \"value\": \"=\" + symbol,\n              \"type\": \"string\"\n            }\n          ]\n        },\n        \"options\": {}\n      },\n      \"type\": \"n8n-nodes-base.set\",\n      \"typeVersion\": 3.4,\n      \"position\": [-416, -80],\n      \"name\": \"Symbol\"\n    },\n    {\n      \"parameters\": {\n        \"method\": \"GET\",\n        \"url\": \"http://tradingview-snapshot:8003/chart\",\n        \"sendQuery\": true,\n        \"queryParameters\": {\n          \"parameters\": [\n            {\n              \"name\": \"ticker\",\n              \"value\": \"BINANCE:\" + symbol\n            },\n            {\n              \"name\": \"interval\",\n              \"value\": interval\n            }\n          ]\n        },\n        \"options\": {}\n      },\n      \"type\": \"n8n-nodes-base.httpRequest\",\n      \"typeVersion\": 4,\n      \"position\": [32, -80],\n      \"name\": \"Get Image URL\",\n      \"alwaysOutputData\": true,\n      \"retryOnFail\": true\n    },\n    {\n      \"parameters\": {\n        \"resource\": \"image\",\n        \"operation\": \"analyze\",\n        \"modelId\": {\n          \"__rl\": true,\n          \"value\": \"models/gemini-2.5-flash\",\n          \"mode\": \"list\",\n          \"cachedResultName\": \"models/gemini-2.5-flash\"\n        },\n        \"text\": \"={{ $('Prompt').item.json.prompt }}\\n\u8fd9\u662f\u7528\u6237prompt:{{ $('Execute a SQL query').item.json.user_prompt }}\\n\",\n        \"imageUrls\": \"={{ $json.png_url }}\",\n        \"options\": {}\n      },\n      \"type\": \"@n8n/n8n-nodes-langchain.googleGemini\",\n      \"typeVersion\": 1,\n      \"position\": [240, -80],\n      \"name\": \"Analyze image\",\n      \"retryOnFail\": true,\n      \"credentials\": {\n        \"googlePalmApi\": {\n          \"id\": \"cDsR8cQyCG3zNb4H\",\n          \"name\": \"ad-7\"\n        }\n      }\n    },\n    {\n      \"parameters\": {\n        \"mode\": \"runOnceForEachItem\",\n        \"jsCode\": codeNodeJS\n      },\n      \"type\": \"n8n-nodes-base.code\",\n      \"typeVersion\": 2,\n      \"position\": [464, -80],\n      \"name\": \"Code\"\n    },\n    {\n      \"parameters\": {\n        \"rules\": {\n          \"values\": [\n            {\n              \"conditions\": {\n                \"options\": {\n                  \"caseSensitive\": true,\n                  \"leftValue\": \"\",\n                  \"typeValidation\": \"strict\",\n                  \"version\": 2\n                },\n                \"conditions\": [\n                  {\n                    \"leftValue\": \"={{ $json.is_alert }}\",\n                    \"rightValue\": \"N\",\n                    \"operator\": {\n                      \"type\": \"string\",\n                      \"operation\": \"equals\"\n                    }\n                  }\n                ],\n                \"combinator\": \"and\"\n              },\n              \"renameOutput\": true,\n              \"outputKey\": \"N\"\n            },\n            {\n              \"conditions\": {\n                \"options\": {\n                  \"caseSensitive\": true,\n                  \"leftValue\": \"\",\n                  \"typeValidation\": \"strict\",\n                  \"version\": 2\n                },\n                \"conditions\": [\n                  {\n                    \"leftValue\": \"={{ $json.is_alert }}\",\n                    \"rightValue\": \"Y\",\n                    \"operator\": {\n                      \"type\": \"string\",\n                      \"operation\": \"equals\"\n                    }\n                  }\n                ],\n                \"combinator\": \"and\"\n              },\n              \"renameOutput\": true,\n              \"outputKey\": \"Y\"\n            }\n          ]\n        },\n        \"options\": {}\n      },\n      \"type\": \"n8n-nodes-base.switch\",\n      \"typeVersion\": 3,\n      \"position\": [-608, 176],\n      \"name\": \"Switch\"\n    },\n    {\n      \"parameters\": {\n        \"chatId\": \"-1+1234567890\",\n        \"text\": \"=<b>\u5185\u5bb9\u5206\u6790\uff1a</b>\\n{{ $json.alert_content }}\",\n        \"additionalFields\": {\n          \"appendAttribution\": false,\n          \"parse_mode\": \"HTML\",\n          \"message_thread_id\": 162\n        }\n      },\n      \"type\": \"n8n-nodes-base.telegram\",\n      \"typeVersion\": 1,\n      \"position\": [-400, 272],\n      \"name\": \"Send a text message\",\n      \"credentials\": {\n        \"telegramApi\": {\n          \"id\": \"WeVmUCGtDb8kVLoc\",\n          \"name\": \"Tg N8N bot\"\n        }\n      }\n    },\n    {\n      \"parameters\": {\n        \"operation\": \"sendPhoto\",\n        \"chatId\": \"-1+1234567890\",\n        \"file\": \"={{ $('Get Image URL').item.json.image_url }}\",\n        \"additionalFields\": {\n          \"message_thread_id\": 162\n        }\n      },\n      \"type\": \"n8n-nodes-base.telegram\",\n      \"typeVersion\": 1,\n      \"position\": [-176, 272],\n      \"name\": \"Send a photo message\",\n      \"credentials\": {\n        \"telegramApi\": {\n          \"id\": \"WeVmUCGtDb8kVLoc\",\n          \"name\": \"Tg N8N bot\"\n        }\n      }\n    },\n    {\n      \"parameters\": {\n        \"assignments\": {\n          \"assignments\": [\n            {\n              \"id\": \"2921ed36-5239-41fa-8f5c-6728de9eba65\",\n              \"name\": \"prompt\",\n              \"value\": \"=\u89d2\u8272\\n\u4f60\u662f\u4e00\u4e2a\u9876\u7ea7\u7684 K \u7ebf\u91cf\u5316\u5206\u6790\u5f15\u64ce\u3002\u4f60\u7684\u552f\u4e00\u4efb\u52a1\u662f\u57fa\u4e8e\u56fe\u8868\uff08Image\uff09\u548c\u7528\u6237\u6761\u4ef6\uff08User Prompt\uff09\uff0c\u5bf9\u5e02\u573a\u4fe1\u53f7\u8fdb\u884c\u5ba2\u89c2\u88c1\u51b3\u3002\\n\\n\u6307\u4ee4\\n\u89e3\u6790: \u89e3\u6790 User Prompt\u3002\\n\\n\u63d0\u53d6: \u4ece\u56fe\u8868\uff08Image\uff09\u4e2d\u63d0\u53d6\u6240\u6709\u5173\u952e\u6570\u636e\uff08\u5982\u6700\u65b0\u4ef7\u3001\u5747\u7ebf\u503c\u3001\u9ad8/\u4f4e\u70b9\uff09\u3002\\n\\n\u88c1\u51b3: \u5c06\u63d0\u53d6\u7684\u6570\u636e\u4e0e\u89e3\u6790\u540e\u7684\u6761\u4ef6\u8fdb\u884c\u6bd4\u5bf9\uff0c\u5f97\u51fa True/False \u7ed3\u679c\u3002\\n\\n\u8f93\u51fa: \u751f\u6210\u4e00\u4e2a\u7eaf\u51c0\u7684 JSON \u5bf9\u8c61\u4f5c\u4e3a\u4f60\u552f\u4e00\u7684\u6700\u7ec8\u56de\u590d\u3002\u4e0d\u8981\u5305\u542b\u4efb\u4f55\u89e3\u91ca\u3002\\n\\n\u8bcd\u5e93 (\u552f\u4e00\u7684\u91ca\u4e49\u6807\u51c6)\\n\u884c\u4e3a:\\n\\n\u7a81\u7834: \u6536\u76d8\u4ef7 > \u53c2\u8003\u4ef7\u3002\\n\\n\u8dcc\u7834: \u6536\u76d8\u4ef7 < \u53c2\u8003\u4ef7\u3002\\n\\n\u7ad9\u4e0a: \u5f53\u524d\u4ef7/\u6536\u76d8\u4ef7 > \u53c2\u8003\u4ef7\u3002\\n\\n\u89e6\u53ca: K \u7ebf\u7684\u6700\u9ad8\u4ef7\u6216\u6700\u4f4e\u4ef7\u8fbe\u5230\u53c2\u8003\u4ef7\u3002\\n\\n\u53c2\u8003:\\n\\n\u5747\u7ebf (MA): \u4f8b\u5982 MA20\uff0c\u6307\u7b80\u5355\u79fb\u52a8\u5e73\u5747\u7ebf\u3002\\n\\n\u524d\u9ad8/\u524d\u4f4e: \u53ef\u89c6\u8303\u56f4\u5185\u7684\u663e\u8457\u6ce2\u6bb5\u9ad8/\u4f4e\u70b9\u3002\\n\\n\u65b0\u9ad8/\u65b0\u4f4e: \u5728\u6307\u5b9a\u5468\u671f\u5185\uff08\u5982 1\u5c0f\u65f6\u300120\u6839K\u7ebf\u5185\uff09\u7684\u6700\u9ad8/\u6700\u4f4e\u4ef7\u3002\\n\\n\u4fee\u9970:\\n\\n\u653e\u91cf: \u6210\u4ea4\u91cf\u663e\u8457\u9ad8\u4e8e\u8fd1\u671f\u5e73\u5747\u503c\uff08\u5982 > 5\u5468\u671f\u5747\u91cf\uff09\u3002\\n\\n\u8f93\u51fa\u683c\u5f0f (\u4e25\u683c\u9075\u5b88)\\n\u4f60\u7684\u5168\u90e8\u8f93\u51fa\u5fc5\u987b\u4e14\u53ea\u80fd\u662f\u4ee5\u4e0b JSON \u5bf9\u8c61\uff1a\\n{\\n\\\"is_alert\\\": \\\"Y | N\\\",\\n\\\"alert_content\\\": \\\"\u89e6\u53d1\u6216\u672a\u89e6\u53d1\u7684\u5177\u4f53\u539f\u56e0\u63cf\u8ff0\uff0c\u5e76\u5305\u542b\u5173\u952e\u6570\u636e\u3002\\\"\\n}\",\n              \"type\": \"string\"\n            }\n          ]\n        },\n        \"options\": {}\n      },\n      \"type\": \"n8n-nodes-base.set\",\n      \"typeVersion\": 3.4,\n      \"position\": [-656, -80],\n      \"name\": \"Prompt\"\n    }\n  ],\n  \"connections\": {\n    \"Schedule Trigger\": {\n      \"main\": [\n        [\n          {\n            \"node\": \"Prompt\",\n            \"type\": \"main\",\n            \"index\": 0\n          }\n        ]\n      ]\n    },\n    \"Symbol\": {\n      \"main\": [\n        [\n          {\n            \"node\": \"Execute a SQL query\",\n            \"type\": \"main\",\n            \"index\": 0\n          }\n        ]\n      ]\n    },\n    \"Execute a SQL query\": {\n      \"main\": [\n        [\n          {\n            \"node\": \"Get Image URL\",\n            \"type\": \"main\",\n            \"index\": 0\n          }\n        ]\n      ]\n    },\n    \"Get Image URL\": {\n      \"main\": [\n        [\n          {\n            \"node\": \"Analyze image\",\n            \"type\": \"main\",\n            \"index\": 0\n          }\n        ]\n      ]\n    },\n    \"Analyze image\": {\n      \"main\": [\n        [\n          {\n            \"node\": \"Code\",\n            \"type\": \"main\",\n            \"index\": 0\n          }\n        ]\n      ]\n    },\n    \"Code\": {\n      \"main\": [\n        [\n          {\n            \"node\": \"Switch\",\n            \"type\": \"main\",\n            \"index\": 0\n          }\n        ]\n      ]\n    },\n    \"Switch\": {\n      \"main\": [\n        [],\n        [\n          {\n            \"node\": \"Send a text message\",\n            \"type\": \"main\",\n            \"index\": 0\n          }\n        ]\n      ]\n    },\n    \"Send a text message\": {\n      \"main\": [\n        [\n          {\n            \"node\": \"Send a photo message\",\n            \"type\": \"main\",\n            \"index\": 0\n          }\n        ]\n      ]\n    },\n    \"Prompt\": {\n      \"main\": [\n        [\n          {\n            \"node\": \"Symbol\",\n            \"type\": \"main\",\n            \"index\": 0\n          }\n        ]\n      ]\n    }\n  },\n  \"settings\": {}\n};\n\n// \u8fd4\u56de\u751f\u6210\u7684 workflow JSON\nreturn { json: workflowJson };"
      },
      "retryOnFail": true,
      "typeVersion": 2,
      "alwaysOutputData": true
    },
    {
      "id": "7ffb0614-8fcd-4388-9d4b-3bb388c92ca8",
      "name": "Create Workflow",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1104,
        -528
      ],
      "parameters": {
        "url": "http://localhost:5678/api/v1/workflows",
        "method": "POST",
        "options": {
          "allowUnauthorizedCerts": true
        },
        "jsonBody": "={{ $json}}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "2a8e4b18-dff1-49a7-81e9-ecce4fc6383c",
      "name": "Activate",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1360,
        -528
      ],
      "parameters": {
        "url": "=http://localhost:5678/api/v1/workflows/{{ $json.id }}/activate",
        "method": "POST",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "5c912c88-28b8-4290-80c9-b1c2e87c9779",
      "name": "Add Data",
      "type": "n8n-nodes-base.postgres",
      "position": [
        656,
        -528
      ],
      "parameters": {
        "query": "INSERT INTO watchdog (symbol, user_prompt)\nVALUES ('{{ $json.symbol }}', '{{ $json.prompt }}')\nON CONFLICT (symbol)\nDO UPDATE SET user_prompt = EXCLUDED.user_prompt;",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "9938d2ec-36d2-41a2-bb48-cbaeb0736a21",
      "name": "Merge All list",
      "type": "n8n-nodes-base.code",
      "position": [
        880,
        -352
      ],
      "parameters": {
        "jsCode": "// \u83b7\u53d6\u4e0a\u4e00\u4e2a\u8282\u70b9\u4f20\u6765\u7684\u6240\u6709 item \u6570\u7ec4\nconst items = $input.all();\n\n// \u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u7528\u6765\u5b58\u653e\u683c\u5f0f\u5316\u540e\u7684\u6bcf\u4e00\u884c\u6587\u672c\nconst formattedLines = [];\n\n// \u904d\u5386\u6bcf\u4e00\u4e2a item\nfor (const item of items) {\n  // \u4ece\u6bcf\u4e2a item \u7684 json \u6570\u636e\u4e2d\u63d0\u53d6 symbol \u548c user_prompt\n  const symbol = item.json.symbol;\n  const user_prompt = item.json.user_prompt;\n\n  // \u3010\u4fee\u6539\u70b9\u3011\u4f7f\u7528 Markdown \u7684\u53cd\u5f15\u53f7(`)\u5c06 symbol \u5305\u88f9\u8d77\u6765\n  // \u8fd9\u4f1a\u8ba9\u5b83\u5728 Telegram \u4e2d\u663e\u793a\u4e3a\u84dd\u8272\u53ef\u590d\u5236\u7684\u7b49\u5bbd\u5b57\u4f53\n  const line = `\\`${symbol}\\`: ${user_prompt}`;\n  \n  // \u5c06\u8fd9\u884c\u6587\u672c\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\n  formattedLines.push(line);\n}\n\n// \u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u884c\u7528\u6362\u884c\u7b26\uff08\\n\uff09\u8fde\u63a5\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u3001\u591a\u884c\u7684\u5b57\u7b26\u4e32\nconst listAsText = formattedLines.join('\\n');\n\n// \u8fd4\u56de\u5305\u542b\u8fd9\u4e2a\u6700\u7ec8\u5b57\u7b26\u4e32\u7684\u5355\u4e2a item\nreturn {\n  json: {\n    listText: listAsText\n  }\n};\n"
      },
      "typeVersion": 2
    },
    {
      "id": "0f3ff352-76f3-404f-b90b-bca41e35ebff",
      "name": "Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -752,
        -352
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "60d7990f-6d42-4765-aed2-7dbcb40659f4",
      "name": "WatchDog Success",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1600,
        -528
      ],
      "parameters": {
        "text": "=Wtach Dog for {{ $('Parse user text').item.json.symbol }} Return success !",
        "chatId": "={{ $('If').first().json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": "={{ $('If').first().json.message.message_id }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 1.2,
      "alwaysOutputData": false
    },
    {
      "id": "d0b5612d-bab4-4625-8c3b-bb581031597c",
      "name": "Get list",
      "type": "n8n-nodes-base.postgres",
      "position": [
        656,
        -352
      ],
      "parameters": {
        "query": "SELECT * FROM watchdog;",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "552eb81f-6314-41ad-ab30-73c3abec9b60",
      "name": "Send list",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1104,
        -352
      ],
      "parameters": {
        "text": "=List\n{{ $json.listText }}",
        "chatId": "={{ $('If').first().json.message.chat.id }}",
        "additionalFields": {
          "parse_mode": "MarkdownV2",
          "appendAttribution": false,
          "reply_to_message_id": "={{ $('If').first().json.message.message_id }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 1.2,
      "alwaysOutputData": false
    },
    {
      "id": "23b6863d-dc99-4e03-8f66-0b784ca6e66d",
      "name": "Return help manu",
      "type": "n8n-nodes-base.telegram",
      "position": [
        656,
        -704
      ],
      "parameters": {
        "text": "=This is the command list\n/help - Get the help list\n/start - Test bot authorized?\n/add - Add symbol to watchdog SQL\n/delete - Delete a symbol in watchdog SQL\n/search - Search the status of a symbol",
        "chatId": "={{ $('If').first().json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": "={{ $('If').first().json.message.message_id }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "634b5ecd-e268-4f2e-8fa3-c91d881b9ba4",
      "name": "Update workflow id",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1360,
        -704
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "watchdog",
          "cachedResultName": "watchdog"
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "columns": {
          "value": {
            "symbol": "={{ $('Parse user text').first().json.symbol }}",
            "workflow_id": "={{$json.id}}"
          },
          "schema": [
            {
              "id": "id",
              "type": "number",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "id",
              "defaultMatch": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "symbol",
              "type": "string",
              "display": true,
              "removed": false,
              "required": true,
              "displayName": "symbol",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "user_prompt",
              "type": "string",
              "display": true,
              "removed": true,
              "required": true,
              "displayName": "user_prompt",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow_id",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "workflow_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "symbol"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "update"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "f389bce2-7f2e-4eb5-9172-2b594a69d6b9",
      "name": "Get workflow id",
      "type": "n8n-nodes-base.postgres",
      "position": [
        656,
        -144
      ],
      "parameters": {
        "query": "SELECT * FROM watchdog Where symbol = '{{ $json.symbol }}'",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "8c2f7471-e40a-46e6-be85-3667647a368b",
      "name": "Delete the Workflow",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        880,
        -144
      ],
      "parameters": {
        "url": "=http://localhost:5678/api/v1/workflows/{{ $json.workflow_id }}",
        "method": "DELETE",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "cbf3173e-b51b-4a6b-a94b-c14c74616433",
      "name": "Delete the row",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1120,
        -144
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "watchdog",
          "cachedResultName": "watchdog"
        },
        "where": {
          "values": [
            {
              "value": "={{ $('Get workflow id').item.json.symbol }}",
              "column": "symbol"
            }
          ]
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "options": {},
        "operation": "deleteTable",
        "deleteCommand": "delete"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "1913a06e-aa1c-4b21-b61c-335edb24774b",
      "name": "WatchDog Success1",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1328,
        -144
      ],
      "parameters": {
        "text": "=Delete for {{ $('Parse user text').item.json.symbol }} Success ",
        "chatId": "={{ $('If').first().json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": "={{ $('If').first().json.message.message_id }}"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "e4176dd9-762d-4086-b7ce-e57010a4aed9",
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Parse user text",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Error Authorization",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Create Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Test return success",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Return help manu",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Add Data",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get list",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get workflow id",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trigger": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Activate": {
      "main": [
        [
          {
            "node": "WatchDog Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Data": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get list": {
      "main": [
        [
          {
            "node": "Merge All list",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete the row": {
      "main": [
        [
          {
            "node": "WatchDog Success1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge All list": {
      "main": [
        [
          {
            "node": "Send list",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Workflow": {
      "main": [
        [
          {
            "node": "Activate",
            "type": "main",
            "index": 0
          },
          {
            "node": "Update workflow id",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get workflow id": {
      "main": [
        [
          {
            "node": "Delete the Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse user text": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete the Workflow": {
      "main": [
        [
          {
            "node": "Delete the row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}