From 8ecef72c1893fcc88a79aed15bd0c3c5ce4dfbd2 Mon Sep 17 00:00:00 2001 From: Kenichiro NOGI Date: Sat, 5 Sep 2026 14:41:11 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20n8n=20Public=20API=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E7=94=A8=E3=81=AE=E5=85=B1=E9=80=9A=E3=82=B9=E3=82=AF=E3=83=AA?= =?UTF-8?q?=E3=83=97=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scripts/deploy-workflow.js | 41 +++++++++++++++++++ .../healthcheck-survey-bot/scripts/n8n-api.js | 30 ++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 NodeSrv/apps/healthcheck-survey-bot/scripts/deploy-workflow.js create mode 100644 NodeSrv/apps/healthcheck-survey-bot/scripts/n8n-api.js diff --git a/NodeSrv/apps/healthcheck-survey-bot/scripts/deploy-workflow.js b/NodeSrv/apps/healthcheck-survey-bot/scripts/deploy-workflow.js new file mode 100644 index 00000000..1685344a --- /dev/null +++ b/NodeSrv/apps/healthcheck-survey-bot/scripts/deploy-workflow.js @@ -0,0 +1,41 @@ +// scripts/deploy-workflow.js +const fs = require("node:fs"); +const path = require("node:path"); +const { request } = require("./n8n-api"); + +async function main() { + const [, , filePath, ...rest] = process.argv; + if (!filePath) { + console.error("使い方: node scripts/deploy-workflow.js [--id=<既存workflowId>]"); + process.exit(1); + } + + const idArg = rest.find((a) => a.startsWith("--id=")); + const existingId = idArg ? idArg.slice("--id=".length) : null; + + const fullPath = path.resolve(filePath); + const definition = JSON.parse(fs.readFileSync(fullPath, "utf8")); + const body = { + name: definition.name, + nodes: definition.nodes, + connections: definition.connections, + settings: definition.settings || {}, + }; + + const { status, body: result } = existingId + ? await request("PUT", `/workflows/${existingId}`, body) + : await request("POST", "/workflows", body); + + console.log("HTTP status:", status); + console.log(JSON.stringify(result, null, 2)); + + if (status >= 200 && status < 300 && result.id) { + console.log(`\nワークフローID: ${result.id}`); + console.log("README.mdの「n8nリソースID一覧」へ記録すること。"); + } +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/NodeSrv/apps/healthcheck-survey-bot/scripts/n8n-api.js b/NodeSrv/apps/healthcheck-survey-bot/scripts/n8n-api.js new file mode 100644 index 00000000..d34371a2 --- /dev/null +++ b/NodeSrv/apps/healthcheck-survey-bot/scripts/n8n-api.js @@ -0,0 +1,30 @@ +// scripts/n8n-api.js +const N8N_BASE_URL = "https://n8n32.next-hd.net/api/v1"; +const N8N_API_KEY = process.env.N8N_API_KEY; + +if (!N8N_API_KEY) { + throw new Error( + "環境変数 N8N_API_KEY が未設定です。NodeSrv/apps/n8n/docs/n8n-guide.md 2章のPublic API Keyを設定してください。" + ); +} + +async function request(method, path, body) { + const res = await fetch(`${N8N_BASE_URL}${path}`, { + method, + headers: { + "X-N8N-API-KEY": N8N_API_KEY, + "Content-Type": "application/json", + }, + body: body ? JSON.stringify(body) : undefined, + }); + const text = await res.text(); + let json; + try { + json = text ? JSON.parse(text) : null; + } catch { + json = text; + } + return { status: res.status, body: json }; +} + +module.exports = { request };