{
  "name": "Windows 遠端控制 (Telegram)",
  "nodes": [
    {
      "parameters": {
        "updates": ["message"],
        "additionalFields": {}
      },
      "id": "telegram-trigger",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.1,
      "position": [250, 300],
      "webhookId": "windows-control-webhook",
      "credentials": {
        "telegramApi": {
          "id": "telegram-bot",
          "name": "WOOO Telegram Bot"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false
          },
          "conditions": [
            {
              "id": "condition-windows",
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "windows",
              "operator": {
                "type": "string",
                "operation": "contains"
              }
            },
            {
              "id": "condition-chrome",
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "chrome",
              "operator": {
                "type": "string",
                "operation": "contains"
              }
            },
            {
              "id": "condition-search",
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "搜尋",
              "operator": {
                "type": "string",
                "operation": "contains"
              }
            }
          ],
          "combinator": "or"
        }
      },
      "id": "if-windows",
      "name": "是 Windows 指令?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [470, 300]
    },
    {
      "parameters": {
        "jsCode": "// 解析用戶訊息，轉換為 Windows 指令\nconst message = $input.first().json.message.text.toLowerCase();\nlet command = '';\nlet description = '';\n\n// 搜尋模式\nconst searchMatch = message.match(/搜尋[\\s]*[「「]?([^」」]+)[」」]?/i) || \n                   message.match(/search[\\s]+(.+)/i) ||\n                   message.match(/chrome[\\s]+(.+)/i);\n\nif (searchMatch) {\n  const query = encodeURIComponent(searchMatch[1].trim());\n  command = `start chrome https://www.google.com/search?q=${query}`;\n  description = `搜尋: ${searchMatch[1]}`;\n} else if (message.includes('開啟 chrome') || message.includes('打開 chrome')) {\n  command = 'start chrome';\n  description = '開啟 Chrome 瀏覽器';\n} else if (message.includes('關閉 chrome') || message.includes('close chrome')) {\n  command = 'taskkill /IM chrome.exe /F';\n  description = '關閉 Chrome 瀏覽器';\n} else if (message.includes('系統資訊') || message.includes('systeminfo')) {\n  command = 'systeminfo | findstr /C:\"OS\" /C:\"Memory\"';\n  description = '查看系統資訊';\n} else if (message.includes('程式列表') || message.includes('tasklist')) {\n  command = 'tasklist | findstr chrome';\n  description = '查看執行中的程式';\n} else {\n  // 預設：嘗試搜尋最後的關鍵字\n  const words = message.replace(/windows|電腦|上|用|chrome|搜尋|在/gi, '').trim();\n  if (words) {\n    const query = encodeURIComponent(words);\n    command = `start chrome https://www.google.com/search?q=${query}`;\n    description = `搜尋: ${words}`;\n  }\n}\n\nreturn {\n  command,\n  description,\n  chatId: $input.first().json.message.chat.id,\n  originalMessage: $input.first().json.message.text\n};"
      },
      "id": "parse-command",
      "name": "解析指令",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [690, 200]
    },
    {
      "parameters": {
        "command": "={{ 'ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 administrator@192.168.0.99 \"' + $json.command + '\"' }}"
      },
      "id": "ssh-execute",
      "name": "SSH 執行指令",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [910, 200]
    },
    {
      "parameters": {
        "chatId": "={{ $('解析指令').item.json.chatId }}",
        "text": "=✅ **Windows 遠端控制**\n\n📝 動作: {{ $('解析指令').item.json.description }}\n💻 電腦: WOOO-SUPER (192.168.0.99)\n\n{{ $json.stdout ? '📤 輸出:\\n```\\n' + $json.stdout.substring(0, 500) + '\\n```' : '✔️ 指令已執行' }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "id": "telegram-success",
      "name": "回報成功",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1130, 200],
      "credentials": {
        "telegramApi": {
          "id": "telegram-bot",
          "name": "WOOO Telegram Bot"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('解析指令').item.json.chatId }}",
        "text": "=❌ **執行失敗**\n\n📝 動作: {{ $('解析指令').item.json.description }}\n⚠️ 錯誤: {{ $json.stderr || '無法連線到 Windows 電腦' }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "id": "telegram-error",
      "name": "回報錯誤",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1130, 400],
      "credentials": {
        "telegramApi": {
          "id": "telegram-bot",
          "name": "WOOO Telegram Bot"
        }
      }
    },
    {
      "parameters": {},
      "id": "no-op",
      "name": "忽略 (非 Windows 指令)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [690, 400]
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "是 Windows 指令?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "是 Windows 指令?": {
      "main": [
        [
          {
            "node": "解析指令",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "忽略 (非 Windows 指令)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "解析指令": {
      "main": [
        [
          {
            "node": "SSH 執行指令",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SSH 執行指令": {
      "main": [
        [
          {
            "node": "回報成功",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "Windows",
      "id": "windows"
    },
    {
      "name": "Telegram",
      "id": "telegram"
    }
  ],
  "triggerCount": 1,
  "meta": {
    "instanceId": "momo-n8n"
  }
}
