{
  "schema_version": "1.0.0",
  "id": "support-reply-from-macro-sheet",
  "title": "Draft a support reply from the macro sheet",
  "category": "support",
  "difficulty": "advanced",
  "featured": false,
  "description": "Route a support email to a queue, fetch that queue's approved macro from a sheet, and let AI personalise it into a Gmail draft for the agent.",
  "tags": [
    "support",
    "gmail",
    "macros",
    "ai-drafts",
    "google-sheets"
  ],
  "apps": [
    "Gmail",
    "Code by Zapier",
    "Google Sheets",
    "AI by Zapier"
  ],
  "plan": {
    "minimum": "Professional",
    "reasons": [
      "multi-step Zap: 4 steps after the trigger",
      "AI by Zapier"
    ]
  },
  "tasks": {
    "typical": 4,
    "max": 4
  },
  "trigger": {
    "app": "Gmail",
    "event": "New Labeled Email",
    "kind": "polling",
    "title": "New email under the Support label",
    "why": "Fires for each new email under the Support label.",
    "config": {
      "Label": "Support"
    },
    "outputs": [
      "message_id",
      "thread_id",
      "from_email",
      "from_name",
      "to",
      "subject",
      "date",
      "body_plain",
      "snippet",
      "labels",
      "attachment_count",
      "attachment_names"
    ],
    "sample": "sample-trigger.json"
  },
  "steps": [
    {
      "key": "route",
      "kind": "code",
      "app": "Code by Zapier",
      "event": "Run JavaScript",
      "title": "Pick the queue",
      "why": "The queue (billing, account, bug, sales, general) is the key into the macro sheet.",
      "code": {
        "language": "javascript",
        "module": "assign-ticket",
        "file": "code/assign-ticket.js"
      },
      "input_data": {
        "subject": "{{trigger.subject}}",
        "body": "{{trigger.body_plain}}",
        "from_email": "{{trigger.from_email}}",
        "ticket_id": "{{trigger.message_id}}"
      },
      "outputs": [
        "queue",
        "priority",
        "assignee",
        "matched_keyword",
        "is_vip"
      ]
    },
    {
      "key": "macro",
      "kind": "search",
      "app": "Google Sheets",
      "event": "Lookup Spreadsheet Row",
      "title": "Fetch the approved macro for that queue",
      "why": "One row per queue with the approved wording and the policy limits the AI must respect.",
      "fields": {
        "Drive": "My Google Drive",
        "Spreadsheet": "Support macros",
        "Worksheet": "Macros",
        "Lookup Column": "queue",
        "Lookup Value": "{{route.queue}}",
        "Should this step be considered a \"success\" when nothing is found?": "Yes"
      },
      "outputs": [
        "row_number",
        "_zap_search_was_found_status",
        "macro_text",
        "policy_notes"
      ]
    },
    {
      "key": "draft",
      "kind": "ai",
      "app": "AI by Zapier",
      "event": "Analyze and Return Data",
      "title": "Personalise the macro",
      "why": "The model may only adapt the approved text to this email. It is told not to add promises, which keeps the draft inside policy.",
      "fields": {
        "Model": "Default (fast)",
        "Instructions": "You draft support replies. Start from the approved macro and adapt it to the customer's email: use their name, refer to their specific situation, keep the macro's facts and promises unchanged, and do not add any new promise, discount, or date. Follow the policy notes. Return reply (plain text, under 160 words, no subject line).",
        "Input data": "Customer email from {{trigger.from_name}}:\n{{trigger.body_plain}}\n\nApproved macro:\n{{macro.macro_text}}\n\nPolicy notes:\n{{macro.policy_notes}}",
        "Output fields": "reply"
      },
      "outputs": [
        "reply"
      ]
    },
    {
      "key": "reply",
      "kind": "action",
      "app": "Gmail",
      "event": "Create Draft Reply",
      "title": "Create the draft reply in the thread",
      "why": "A draft, never a send. The agent reads, edits, and sends from Gmail.",
      "fields": {
        "Thread": "{{trigger.thread_id}}",
        "To": "{{trigger.from_email}}",
        "Body Type": "plain",
        "Body": "{{draft.reply}}"
      },
      "outputs": [
        "id"
      ]
    }
  ]
}
