{
  "id": "HPVAZGksUMoYkWlX",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "School Notice Distribution Workflow via WhatsApp and Email",
  "tags": [],
  "nodes": [
    {
      "id": "4ed6aade-7953-401e-9c87-54f159b81428",
      "name": "Daily Notice Check - 9 AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -500,
        400
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 9
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "cdd53457-2296-42db-ba46-444e2ac7ab6b",
      "name": "Validate Notice Data",
      "type": "n8n-nodes-base.if",
      "position": [
        -60,
        400
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.title}}",
              "operation": "isNotEmpty"
            },
            {
              "value1": "={{$json.message}}",
              "operation": "isNotEmpty"
            },
            {
              "value1": "={{$json.targetAudience}}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "752ae286-9f2d-4c7e-8e01-1ff92966a9c4",
      "name": "Process Notice Distribution",
      "type": "n8n-nodes-base.code",
      "position": [
        160,
        400
      ],
      "parameters": {
        "jsCode": "const notices = $('Read Notices').all();\nconst contacts = $('Read Stakeholder Contacts').all();\n\nconst processedNotices = [];\n\nfor (const notice of notices) {\n  const noticeData = notice.json;\n  \n  const targetAudience = noticeData.targetAudience.toLowerCase();\n  const relevantContacts = contacts.filter(contact => {\n    const contactData = contact.json;\n    \n    if (targetAudience === 'all') return true;\n    if (targetAudience === 'students' && contactData.type === 'Student') return true;\n    if (targetAudience === 'parents' && contactData.type === 'Parent') return true;\n    if (targetAudience === 'teachers' && contactData.type === 'Teacher') return true;\n    if (targetAudience === 'staff' && contactData.type === 'Staff') return true;\n    \n    return false;\n  });\n  \n  for (const contact of relevantContacts) {\n    processedNotices.push({\n      noticeId: noticeData.noticeId || 'NOTICE-' + Date.now(),\n      title: noticeData.title,\n      message: noticeData.message,\n      priority: noticeData.priority || 'Medium',\n      targetAudience: noticeData.targetAudience,\n      contactName: contact.json.name,\n      contactEmail: contact.json.email,\n      contactPhone: contact.json.phone,\n      contactType: contact.json.type,\n      distributionDate: new Date().toISOString().split('T')[0],\n      status: 'Ready for Distribution'\n    });\n  }\n}\n\nreturn processedNotices.map(item => ({ json: item }));"
      },
      "typeVersion": 2
    },
    {
      "id": "9d1fd210-9737-4756-a70d-002c899e3f0f",
      "name": "Prepare Email Content",
      "type": "n8n-nodes-base.code",
      "position": [
        380,
        300
      ],
      "parameters": {
        "jsCode": "const noticeData = $input.first().json;\n\nconst emailSubject = `[${noticeData.priority} Priority] ${noticeData.title}`;\nconst emailBody = `Dear ${noticeData.contactName},\n\n${noticeData.message}\n\n---\nNotice Details:\n- Priority: ${noticeData.priority}\n- Target Audience: ${noticeData.targetAudience}\n- Distribution Date: ${noticeData.distributionDate}\n- Notice ID: ${noticeData.noticeId}\n\nFor any questions or concerns, please contact the school administration.\n\nBest regards,\nSchool Administration Team`;\n\nreturn [{\n  json: {\n    to: noticeData.contactEmail,\n    subject: emailSubject,\n    body: emailBody,\n    recipientName: noticeData.contactName,\n    noticeId: noticeData.noticeId,\n    priority: noticeData.priority\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "5b817e81-22d2-4618-848e-62b0c286e1b4",
      "name": "Prepare WhatsApp Content",
      "type": "n8n-nodes-base.code",
      "position": [
        380,
        500
      ],
      "parameters": {
        "jsCode": "const noticeData = $input.first().json;\n\nconst whatsappMessage = `\ud83c\udfeb *${noticeData.title}*\\n\\n${noticeData.message}\\n\\n\ud83d\udccb Notice ID: ${noticeData.noticeId}\\n\u26a1 Priority: ${noticeData.priority}\\n\ud83d\udcc5 Date: ${noticeData.distributionDate}\\n\\n_This is an official school notice. Please keep this for your records._`;\n\nreturn [{\n  json: {\n    phone: noticeData.contactPhone,\n    message: whatsappMessage,\n    recipientName: noticeData.contactName,\n    noticeId: noticeData.noticeId,\n    priority: noticeData.priority\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "88cc77ed-4861-4a1a-b4bb-0c491e58a709",
      "name": "Send Email Notice",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        600,
        300
      ],
      "parameters": {
        "text": "={{$json.body}}",
        "options": {},
        "subject": "={{$json.subject}}",
        "toEmail": "={{$json.to}}",
        "fromEmail": "user@example.com",
        "emailFormat": "text"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "5b58440e-6a47-4ba0-94ba-19b998d4204e",
      "name": "Send WhatsApp Notice",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        600,
        500
      ],
      "parameters": {
        "url": "https://graph.facebook.com/v17.0/FROM_PHONE_NUMBER_ID/messages",
        "options": {},
        "jsonBody": "={\n  \"messaging_product\": \"whatsapp\",\n  \"to\": \"{{ $json.phone }}\",\n  \"type\": \"text\",\n  \"text\": {\n    \"body\": \"{{ $json.message }}\"\n  }\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_TOKEN_HERE"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "5c4b3929-cb7d-45d7-804e-b7281adef0e7",
      "name": "Update Notice Status",
      "type": "n8n-nodes-base.microsoftExcel",
      "position": [
        820,
        400
      ],
      "parameters": {
        "options": {},
        "dataMode": "autoMap",
        "resource": "worksheet",
        "workbook": {
          "__rl": true,
          "mode": "id",
          "value": "=notices-workbook-001"
        },
        "operation": "update",
        "worksheet": {
          "__rl": true,
          "mode": "id",
          "value": "=notices-sheet-001"
        },
        "columnToMatchOn": "=title"
      },
      "credentials": {
        "microsoftExcelOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "e77a2dd6-1487-4632-b6f6-9e9b6237fc8f",
      "name": "Workflow Documentation",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -560,
        -220
      ],
      "parameters": {
        "color": 4,
        "width": 640,
        "height": 400,
        "content": "### **School Notice Distribution Workflow Components**\n\n**\ud83d\udcc5 Triggers:**\n* **Daily Notice Check - 9 AM** - Scheduled trigger for automated daily notice distribution\n\n**\ud83d\udcca Data Processing:**\n* **Read Notices** - Fetches notices from Excel database\n* **Validate Notice Data** - Ensures notice data completeness and format\n* **Process Notice Distribution** - Matches notices with target audiences\n\n**\ud83d\udce7 Communication:**\n* **Prepare Email Content** - Creates personalized email messages\n* **Prepare WhatsApp Content** - Formats WhatsApp messages with emojis\n* **Send Email Notice** - Distributes via email to stakeholders\n* **Send WhatsApp Notice** - Sends WhatsApp messages via Business API\n\n**\ud83d\udcc8 Reporting:**\n* **Update Notice Status** - Marks notices as distributed"
      },
      "typeVersion": 1
    },
    {
      "id": "003b88f7-1cc6-4170-94ff-32d25a6bb758",
      "name": "Read Notices",
      "type": "n8n-nodes-base.microsoftExcel",
      "position": [
        -280,
        400
      ],
      "parameters": {
        "filters": {
          "fields": [
            {
              "value": "Pending",
              "column": "Status"
            }
          ]
        },
        "resource": "worksheet",
        "workbook": {
          "__rl": true,
          "mode": "id",
          "value": "=notices-workbook-001"
        }
      },
      "credentials": {
        "microsoftExcelOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2046bf84-b64c-40b9-9270-8bbc4455f878",
  "connections": {
    "Read Notices": {
      "main": [
        [
          {
            "node": "Validate Notice Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email Notice": {
      "main": [
        [
          {
            "node": "Update Notice Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp Notice": {
      "main": [
        [
          {
            "node": "Update Notice Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Notice Status": {
      "main": [
        []
      ]
    },
    "Validate Notice Data": {
      "main": [
        [
          {
            "node": "Process Notice Distribution",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Prepare Email Content": {
      "main": [
        [
          {
            "node": "Send Email Notice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare WhatsApp Content": {
      "main": [
        [
          {
            "node": "Send WhatsApp Notice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Notice Check - 9 AM": {
      "main": [
        [
          {
            "node": "Read Notices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Notice Distribution": {
      "main": [
        [
          {
            "node": "Prepare Email Content",
            "type": "main",
            "index": 0
          },
          {
            "node": "Prepare WhatsApp Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}