{
  "name": "Gmail・Claude・Data Tableで在庫と来店予約の返信下書きを作る",
  "nodes": [
    {
      "id": "245ee77f-1d89-4c15-8ab8-6a60fc29f8b1",
      "name": "5分ごとに実行",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.4,
      "position": [
        0,
        464
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 5
            }
          ]
        }
      }
    },
    {
      "id": "e9f56f3f-33e6-4995-9340-8529e829f238",
      "name": "未処理のメールを取得",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        224,
        464
      ],
      "parameters": {
        "resource": "message",
        "operation": "getAll",
        "limit": 10,
        "simple": false,
        "filters": {
          "q": "in:inbox -in:drafts -label:ai-drafted -label:needs-human newer_than:1d to:support@example.com",
          "readStatus": "both"
        }
      }
    },
    {
      "id": "8678c78b-37ec-4b98-b955-419ed6c77208",
      "name": "全件完了",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        736,
        0
      ],
      "parameters": {}
    },
    {
      "id": "23300dbf-0599-4449-992f-e93e689ff622",
      "name": "用件を分類",
      "type": "@n8n/n8n-nodes-langchain.textClassifier",
      "typeVersion": 1.1,
      "position": [
        672,
        304
      ],
      "parameters": {
        "inputText": "=件名: {{ $json.subject }}\n本文: {{ $json.text }}",
        "categories": {
          "categories": [
            {
              "category": "在庫の問い合わせ",
              "description": "商品の在庫、価格、サイズ、取り扱いの有無についての質問"
            },
            {
              "category": "来店予約",
              "description": "店舗に来店する日時の相談や予約"
            }
          ]
        },
        "options": {
          "fallback": "other"
        }
      }
    },
    {
      "id": "9a526e73-e688-427b-b3f1-2c33fbcce5ab",
      "name": "Claude Haiku 4.5",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1.5,
      "position": [
        752,
        528
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "claude-haiku-4-5-20251001",
          "cachedResultName": "Claude Haiku 4.5"
        }
      }
    },
    {
      "id": "a0fef6df-585a-481d-ace0-9d0fabcaf737",
      "name": "1通ずつ処理",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        448,
        464
      ],
      "parameters": {
        "batchSize": 1
      }
    },
    {
      "id": "9405f7d7-eac6-4ade-8fbc-a6b5d68d2f1d",
      "name": "商品の種類を抽出",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1.2,
      "position": [
        1024,
        112
      ],
      "parameters": {
        "text": "={{ $json.text }}",
        "schemaType": "fromAttributes",
        "attributes": {
          "attributes": [
            {
              "name": "category",
              "type": "string",
              "required": true,
              "description": "問い合わせの商品の種類。ソファ、テーブル、チェア、照明、ラグ、収納 のいずれか。当てはまらなければ その他"
            },
            {
              "name": "request",
              "type": "string",
              "description": "色・サイズ・素材など、お客様の希望を短くまとめたもの"
            }
          ]
        }
      }
    },
    {
      "id": "24e6dcd0-2227-46c3-8d9e-0788b7665b6d",
      "name": "在庫を検索",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        1376,
        112
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "category",
              "condition": "eq",
              "keyValue": "={{ $json.output.category }}"
            }
          ]
        },
        "returnAll": true
      },
      "alwaysOutputData": true
    },
    {
      "id": "1ecf45f5-3c6d-4bfe-98c3-a0e29a8407f3",
      "name": "検索結果をまとめる",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1600,
        112
      ],
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "products"
      }
    },
    {
      "id": "0d5de888-87e5-4dfc-8572-a9fda6b227c1",
      "name": "在庫の返信文を作る",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.9,
      "position": [
        1824,
        112
      ],
      "parameters": {
        "promptType": "define",
        "text": "=お客様のメール\n件名: {{ $(\"1通ずつ処理\").first().json.subject }}\n本文:\n{{ $(\"1通ずつ処理\").first().json.text }}\n\n在庫データ（JSON）:\n{{ JSON.stringify($json.products) }}",
        "messages": {
          "messageValues": [
            {
              "type": "SystemMessagePromptTemplate",
              "message": "あなたは家具・インテリアの店のスタッフです。お客様のメールへの返信本文を日本語で書いてください。\n\n守ること:\n- 在庫・価格・入荷予定は、与えられたデータに書かれていることだけを書く。データに無い商品は「取り扱いがない」と伝える\n- 配送、納期、割引、取り置きなど、データに無いことは約束しない。聞かれていて分からないことは「担当者から改めてご連絡いたします」と書く\n- プレーンテキストで書く。** や # などの記号を使わない\n- 「在庫データ」「SKU」「システム」などの社内の言葉と、商品コードを書かない\n- 宛名は本文の署名から「○○様」とし、「お問い合わせありがとうございます。」で始める。「いつも」「お世話になって」「お疲れさま」は使わない\n- 署名は「インテリアショップ スタッフ」\n\n本文だけを出力してください。"
            }
          ]
        }
      }
    },
    {
      "id": "76281eb1-c96a-453c-8ee2-db86a04bdc90",
      "name": "在庫の下書きを作る",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        2176,
        112
      ],
      "parameters": {
        "resource": "draft",
        "operation": "create",
        "subject": "=Re: {{ $(\"1通ずつ処理\").first().json.subject }}",
        "emailType": "text",
        "message": "={{ $json.text }}",
        "options": {
          "threadId": "={{ $(\"1通ずつ処理\").first().json.threadId }}",
          "sendTo": "={{ $(\"1通ずつ処理\").first().json.from.value[0].address }}"
        }
      }
    },
    {
      "id": "a1f57973-5b44-4d87-b988-f968df12ff6d",
      "name": "ai-draftedラベルを付ける",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        2400,
        336
      ],
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $(\"1通ずつ処理\").first().json.id }}",
        "labelIds": []
      }
    },
    {
      "id": "9c11cc61-85e6-4f63-94e7-ad92f806050b",
      "name": "来店希望を抽出",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1.2,
      "position": [
        1024,
        304
      ],
      "parameters": {
        "text": "=今日は {{ $now.setZone('Asia/Tokyo').toFormat('yyyy-MM-dd') }}（{{ $now.setZone('Asia/Tokyo').setLocale('ja').toFormat('cccc') }}）です。\n\n{{ $json.text }}",
        "schemaType": "fromAttributes",
        "attributes": {
          "attributes": [
            {
              "name": "desired_date",
              "type": "string",
              "required": true,
              "description": "来店を希望する日付。YYYY-MM-DD 形式"
            },
            {
              "name": "time_preference",
              "type": "string",
              "description": "希望の時間帯。HH:MM、午前、午後、指定なし のいずれか"
            }
          ]
        }
      }
    },
    {
      "id": "c2bb8401-289a-4013-a3dd-22a5bbd130f0",
      "name": "空き枠を検索",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        1376,
        304
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "slot_date",
              "condition": "eq",
              "keyValue": "={{ $json.output.desired_date }}"
            }
          ]
        },
        "returnAll": true
      },
      "alwaysOutputData": true
    },
    {
      "id": "3866800d-cb50-4318-900a-b43f4f2d5bbe",
      "name": "枠をまとめる",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1600,
        304
      ],
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "slots"
      }
    },
    {
      "id": "c1b52cde-0d8d-4537-84ef-00ed616a0d40",
      "name": "予約の返信文を作る",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.9,
      "position": [
        1824,
        304
      ],
      "parameters": {
        "promptType": "define",
        "text": "=お客様のメール\n件名: {{ $(\"1通ずつ処理\").first().json.subject }}\n本文:\n{{ $(\"1通ずつ処理\").first().json.text }}\n\n抽出した希望: {{ JSON.stringify($(\"来店希望を抽出\").first().json.output) }}\n\nその日の来店枠（JSON）:\n{{ JSON.stringify($json.slots) }}",
        "messages": {
          "messageValues": [
            {
              "type": "SystemMessagePromptTemplate",
              "message": "あなたは家具・インテリアの店のスタッフです。来店の相談メールへの返信本文を日本語で書いてください。\n\n守ること:\n- 来店枠は与えられたデータだけを根拠にする。booked が capacity 未満の枠だけが空いている。枠のデータが無い日は来店を受け付けていない\n- 空きがあれば候補の時間を案内し、ご希望の時間を返信してもらえれば予約を確定すると伝える。このメールの時点では予約は確定していない\n- 来店枠以外のこと（在庫、価格など）を聞かれていたら、答えずに「担当者から改めてご連絡いたします」と書く\n- プレーンテキストで書く。** や # などの記号を使わない\n- 「データ」「抽出」「システム」などの社内の言葉を書かない\n- 宛名は本文の署名から「○○様」とし、「お問い合わせありがとうございます。」で始める。「いつも」「お世話になって」「お疲れさま」は使わない\n- 署名は「インテリアショップ スタッフ」\n\n本文だけを出力してください。"
            }
          ]
        }
      }
    },
    {
      "id": "e766c71e-dfc2-4da7-a1d2-5ba8bca125c4",
      "name": "予約の下書きを作る",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        2176,
        304
      ],
      "parameters": {
        "resource": "draft",
        "operation": "create",
        "subject": "=Re: {{ $(\"1通ずつ処理\").first().json.subject }}",
        "emailType": "text",
        "message": "={{ $json.text }}",
        "options": {
          "threadId": "={{ $(\"1通ずつ処理\").first().json.threadId }}",
          "sendTo": "={{ $(\"1通ずつ処理\").first().json.from.value[0].address }}"
        }
      }
    },
    {
      "id": "d315df71-67cd-4419-ac47-79c455535b4f",
      "name": "needs-humanラベルを付ける",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        1088,
        496
      ],
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $(\"1通ずつ処理\").first().json.id }}",
        "labelIds": []
      }
    },
    {
      "id": "833aac14-6c6d-4d0a-8b70-34fe05d39c2d",
      "name": "Sticky Note 7588069f",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -420,
        120
      ],
      "parameters": {
        "content": "## 取り込んだら最初にやること\n1. Gmail と Anthropic の認証情報を、各ノードで選ぶ\n2. Data Table を2つ作る（shop_products と shop_visit_slots。列は配布ページを参照）\n3. 「在庫を検索」「空き枠を検索」で、作ったテーブルを選ぶ\n4. Gmail にラベル ai-drafted と needs-human を作り、2つのラベルノードで選ぶ\n5. 「未処理のメールを取得」の検索条件にある to:support@example.com を、問い合わせを受けるアドレスに書き換える（書き換えるまで1通も処理されません）\n6. 手動で1回実行し、下書きを読んでから Publish する\n\n**このワークフローはメールを送信しません。** 下書きを人が読んでから送ってください。",
        "color": 4,
        "width": 400,
        "height": 420
      }
    }
  ],
  "connections": {
    "5分ごとに実行": {
      "main": [
        [
          {
            "node": "未処理のメールを取得",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "未処理のメールを取得": {
      "main": [
        [
          {
            "node": "1通ずつ処理",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "用件を分類": {
      "main": [
        [
          {
            "node": "商品の種類を抽出",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "来店希望を抽出",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "needs-humanラベルを付ける",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude Haiku 4.5": {
      "ai_languageModel": [
        [
          {
            "node": "用件を分類",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "商品の種類を抽出",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "在庫の返信文を作る",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "来店希望を抽出",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "予約の返信文を作る",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "1通ずつ処理": {
      "main": [
        [
          {
            "node": "全件完了",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "用件を分類",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "商品の種類を抽出": {
      "main": [
        [
          {
            "node": "在庫を検索",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "在庫を検索": {
      "main": [
        [
          {
            "node": "検索結果をまとめる",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "検索結果をまとめる": {
      "main": [
        [
          {
            "node": "在庫の返信文を作る",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "在庫の返信文を作る": {
      "main": [
        [
          {
            "node": "在庫の下書きを作る",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "在庫の下書きを作る": {
      "main": [
        [
          {
            "node": "ai-draftedラベルを付ける",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ai-draftedラベルを付ける": {
      "main": [
        [
          {
            "node": "1通ずつ処理",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "来店希望を抽出": {
      "main": [
        [
          {
            "node": "空き枠を検索",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "空き枠を検索": {
      "main": [
        [
          {
            "node": "枠をまとめる",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "枠をまとめる": {
      "main": [
        [
          {
            "node": "予約の返信文を作る",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "予約の返信文を作る": {
      "main": [
        [
          {
            "node": "予約の下書きを作る",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "予約の下書きを作る": {
      "main": [
        [
          {
            "node": "ai-draftedラベルを付ける",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "needs-humanラベルを付ける": {
      "main": [
        [
          {
            "node": "1通ずつ処理",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "timezone": "Asia/Tokyo"
  },
  "pinData": {}
}
