{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "2d468c09-fe3a-4c8a-b4af-817a762a8d7a",
      "name": "Send SMS",
      "type": "n8n-nodes-base.twilio",
      "notes": "Send SMS using Twilio",
      "position": [
        -60,
        420
      ],
      "parameters": {
        "to": "={{ $json.message.content.to }}",
        "from": "+1xxxxxxxxxx",
        "message": "={{ $json.message.content.message }}",
        "options": {}
      },
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ad400bf5-b77d-4f76-be46-fe6cc76e5a0c",
      "name": "OpenAI",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        -660,
        720
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1",
          "cachedResultName": "GPT-4.1"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are \u201cMarCom AI\u201d \u2013 a specialist that writes outbound messages for Salesforce Campaigns.\n{\n  \"contactMethod\": \"SMS | Email | Telegram | WhatsApp | (null)\",\n  \"phone\": \"(###) ###-#### or empty\",\n  \"email\": \"user@example.com or empty\",\n  \"campaignDescription\": \"Full campaign description text\"\n}\n\nYour job\n\nPick the channel in contactMethod.\n\u2022 If it is null, default to Email if an email exists; otherwise default to SMS.\n\nCraft one concise, personalized outbound message that:\n\u2022 References the key value proposition(s) and date/location found in campaignDescription.\n\u2022 Includes an explicit next-step call-to-action (book demo, visit booth, etc.).\n\nObey channel rules:\n\u2022 SMS / WhatsApp / Telegram \u2192 \u2264 160 characters, no subject.\n\u2022 Email \u2192 Provide a subject \u2264 60 chars and a body \u2264 120 words.\n\nReturn a SINGLE JSON object only \u2013 no extra commentary \u2013 with exactly these keys:\n{\n  \"method\": \"SMS | Email | Telegram | WhatsApp\",\n  \"from\": \"PowerSwitch Marketing <user@example.com>\",\n  \"to\": \"<email or phone based on method>\",\n  \"subject\": \"<for Email only, otherwise omit>\",\n  \"message\": \"<text body or SMS content>\"\n}\n"
            },
            {
              "role": "assistant",
              "content": "=Here is the record data for this recipient:\n\n{\n  \"contactMethod\": \"{{ $json.Contact.ContactMethod__c }}\",\n  \"phone\": \"{{ $json.Phone }}\",\n  \"email\": \"{{ $json.Email }}\",\n  \"campaignDescription\": \"{{ $('Fetch Campaign').item.json.Description }}\"\n}\n\nGenerate the outbound communication in the required JSON format."
            }
          ]
        },
        "jsonOutput": true
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "b9e3bee1-83c7-4a60-94fe-110236d0c8a9",
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        -64,
        620
      ],
      "parameters": {
        "html": "={{ $json.message.content.message }}",
        "options": {},
        "subject": "={{ $json.message.content.subject }}",
        "toEmail": "={{ $json.message.content.to }}",
        "fromEmail": "user@example.com"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "d89a36de-0978-430e-a248-a7cd7e73ee37",
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        156,
        720
      ],
      "parameters": {
        "url": "=https:/xxxxx.my.salesforce.com/services/data/v60.0/sobjects/CampaignMember/{{ $('Fetch Campaign Members').item.json.Id }}",
        "method": "PATCH",
        "options": {},
        "jsonBody": "={\n      \"Processed__c\" : true\n} ",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "salesforceOAuth2Api"
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "649c91de-560e-47e4-b498-217246ced614",
      "name": "Fetch Campaign",
      "type": "n8n-nodes-base.salesforce",
      "position": [
        -1100,
        720
      ],
      "parameters": {
        "query": "=SELECT Id, Description,Name  FROM Campaign",
        "resource": "search"
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f5a813de-06b0-40a8-baff-1a51a9857647",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1180,
        520
      ],
      "parameters": {
        "content": "## Fetch Campaigns\n\nRetrieve campaigns that still require processing.\n\n\n\n\n\n\n\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "d208f08e-6fc8-47fa-a1e1-e2bcae764d5e",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -920,
        460
      ],
      "parameters": {
        "height": 220,
        "content": "## Fetch Campaign Members\nRetrieve all members associated with the campaigns returned in the previous step. Make the specific query ID for your Campaign\n\n\n\n\n\n\n\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "ca76386b-6908-4541-9ff2-8fb69b3237a2",
      "name": "Fetch Campaign Members",
      "type": "n8n-nodes-base.salesforce",
      "position": [
        -880,
        720
      ],
      "parameters": {
        "query": "=SELECT Id, CampaignId, Contact.ContactMethod__c, Email, Phone FROM CampaignMember where CampaignId = '{{ $json.Id }}'",
        "resource": "search"
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e72ebcee-0d97-4e57-b0f8-167ea0fe9d27",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -660,
        440
      ],
      "parameters": {
        "height": 240,
        "content": "## Generate Personalized Messages\nUse OpenAI to convert each campaign\u2019s description into tailored messages for every member, selecting the appropriate channel (SMS, Email, WhatsApp, or Telegram)."
      },
      "typeVersion": 1
    },
    {
      "id": "553f9b9b-20f2-4ead-a2d1-1111c21bf176",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -400,
        520
      ],
      "parameters": {
        "content": "## Communication Method Mapping\nAssign channel codes as follows: SMS = 0, Email = 1, WhatsApp = 2, Telegram = 3.\n\n\n\n\n\n\n\n\n\nAsk ChatGPT\n"
      },
      "typeVersion": 1
    },
    {
      "id": "44581a57-558b-4aa6-a27d-e03e62fc9393",
      "name": "Communication Method Switch",
      "type": "n8n-nodes-base.switch",
      "notes": "Route to Email flow",
      "position": [
        -284,
        699
      ],
      "parameters": {
        "rules": {
          "rules": [
            {
              "value2": "SMS"
            },
            {
              "output": 1,
              "value2": "Email"
            },
            {
              "output": 2,
              "value2": "Whatsapp"
            },
            {
              "output": 3,
              "value2": "Telegram"
            }
          ]
        },
        "value1": "={{ $json.message.content.method }}",
        "dataType": "string"
      },
      "typeVersion": 1
    },
    {
      "id": "4ac45980-237a-41ba-adcd-f3cd6945699d",
      "name": "Send Whatsapp",
      "type": "n8n-nodes-base.twilio",
      "notes": "Send SMS using Twilio",
      "position": [
        -64,
        820
      ],
      "parameters": {
        "to": "={{ $json.message.content.to }}",
        "from": "+1xxxxxxxxxx",
        "message": "={{ $json.message.content.message }}",
        "options": {},
        "toWhatsapp": true
      },
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "44bf7301-b466-48aa-b1c8-490fd8cb6e01",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        160,
        480
      ],
      "parameters": {
        "height": 220,
        "content": "## Mark Campaign Members as Processed\nUpdate each CampaignMember record to indicate that the personalized message has been generated and the member has been processed."
      },
      "typeVersion": 1
    },
    {
      "id": "0e592700-06db-4cc9-ad37-924360f113a6",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1320,
        720
      ],
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "typeVersion": 1.2
    }
  ],
  "connections": {
    "OpenAI": {
      "main": [
        [
          {
            "node": "Communication Method Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send SMS": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Whatsapp": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Campaign": {
      "main": [
        [
          {
            "node": "Fetch Campaign Members",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Fetch Campaign",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Campaign Members": {
      "main": [
        [
          {
            "node": "OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Communication Method Switch": {
      "main": [
        [
          {
            "node": "Send SMS",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Whatsapp",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}