{
  "name": "WF-SDR-TOOLS (Agent Email + Notify)",
  "nodes": [
    {
      "id": "webhook1",
      "name": "Agent Tool Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        250,
        300
      ],
      "parameters": {
        "path": "retell-sdr-tool",
        "httpMethod": "POST",
        "responseMode": "responseNode",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "normalize1",
      "name": "Normalize Retell Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        470,
        300
      ],
      "parameters": {
        "jsCode": "var body=$json.body||{}; var headers=$json.headers||{}; var signature=headers['x-retell-signature']||''; var args=body.args||body; var hasAction=args.action||body.action; if(!signature && !hasAction){ return [{ json: { action:'invalid', error:'Missing Retell signature and action field' } }]; } var call=body.call||{}; var metadata=call.metadata||body.metadata||{}; return [{ json: { action:args.action||body.action||'unknown', firstName:args.firstName||'', lastName:args.lastName||'', companyName:args.companyName||'', email:args.email||'', phone:args.phone||'', callback_day:args.callback_day||'', callback_time:args.callback_time||'', notes:args.notes||'', interest_level:args.interest_level||'', plan:args.plan||'pro', closeLeadId:metadata.close_lead_id||args.closeLeadId||'', contactId:metadata.contact_id||args.contactId||'', callId:call.call_id||'', toNumber:call.to_number||'' } }];"
      }
    },
    {
      "id": "switch1",
      "name": "Route Action",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        700,
        300
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "combinator": "and",
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "send_vsl",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "renameOutput": "Send VSL"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "combinator": "and",
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "book_callback",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "renameOutput": "Book Callback"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "combinator": "and",
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "hot_lead",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "renameOutput": "Hot Lead"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "combinator": "and",
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "send_payment_link",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "renameOutput": "Payment Link"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "combinator": "and",
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "send_vsl_sms",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "renameOutput": "Send VSL SMS"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "combinator": "and",
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "mark_dnc",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              },
              "renameOutput": "Mark DNC"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "sendEmail1",
      "name": "Send VSL Email",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        940,
        60
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.close.com/api/v1/activity/email/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"lead_id\": \"{{ $json.closeLeadId }}\",\n  \"to\": [\"{{ $json.email }}\"],\n  \"sender\": \"user@example.com\",\n  \"subject\": \"Quick look at what we discussed, {{ $json.firstName }}\",\n  \"body_text\": \"Hey {{ $json.firstName }}, here is the 2-minute walkthrough: https://www.voicesdr.ai/see-how-it-works ... Talk soon, Laura, VoiceSDR\",\n  \"status\": \"outbox\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "updateDemoSent",
      "name": "Update Lead Demo Sent",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1160,
        60
      ],
      "parameters": {
        "method": "PUT",
        "url": "=https://api.close.com/api/v1/lead/{{ $('Normalize Retell Data').first().json.closeLeadId }}/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"status_id\": \"stat_JkhhQYXcbMQXXixqSBlmq35po9YWwgD0cn2XnNXP8z2\",\n  \"custom.cf_6mtaf50dQHiwZD5wS7VElsL6XLGY3wuzpr1wqdsJcAC\": \"https://www.voicesdr.ai/see-how-it-works\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "createTask1",
      "name": "Create Close CRM Task",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        940,
        240
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.close.com/api/v1/task/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"lead_id\": \"{{ $json.closeLeadId }}\",\n  \"text\": \"CALLBACK BOOKED by Laura SDR\\n{{ $json.firstName }} {{ $json.lastName }} at {{ $json.companyName }}\\nDay: {{ $json.callback_day }}\\nTime: {{ $json.callback_time }}\",\n  \"is_complete\": false\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "updateLeadStatus",
      "name": "Update Lead Appointment Set",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1160,
        240
      ],
      "parameters": {
        "method": "PUT",
        "url": "=https://api.close.com/api/v1/lead/{{ $('Normalize Retell Data').first().json.closeLeadId }}/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"status_id\": \"stat_UTHYC5H4tj4TYMi7ySnfB02rSlhEYdOgWWTFHOfXFRU\",\n  \"custom.cf_W1sgGnp5Xqs0IiKN5FToDh149dkToz6pnxMkEDfEMKb\": \"{{ $('Normalize Retell Data').first().json.callback_day }} {{ $('Normalize Retell Data').first().json.callback_time }}\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "callbackSms",
      "name": "Send Callback Confirmation SMS",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1380,
        240
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.close.com/api/v1/activity/sms/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"lead_id\": \"{{ $('Normalize Retell Data').first().json.closeLeadId }}\",\n  \"local_phone\": \"+15555550100\",\n  \"remote_phone\": \"{{ $('Normalize Retell Data').first().json.toNumber }}\",\n  \"text\": \"Hi {{ $('Normalize Retell Data').first().json.firstName }}, this is Laura from VoiceSDR. Confirming your callback for {{ $('Normalize Retell Data').first().json.callback_day }} at {{ $('Normalize Retell Data').first().json.callback_time }}.\",\n  \"direction\": \"outbound\",\n  \"status\": \"outbox\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "hotLeadSlack",
      "name": "Slack Hot Lead Alert",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        940,
        420
      ],
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "id",
          "value": "C08CZQV5QV7"
        },
        "text": "=:fire: *HOT LEAD from Laura SDR!*\n\n*{{ $json.firstName }} {{ $json.lastName }}* at *{{ $json.companyName }}*\n:email: {{ $json.email || 'not captured' }}\n:phone: Call ID: {{ $json.callId }}\n\n*CALL THEM WITHIN 5 MINUTES!*",
        "otherOptions": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "sendPaymentEmail",
      "name": "Send Payment Email",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        940,
        600
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.close.com/api/v1/activity/email/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"lead_id\": \"{{ $json.closeLeadId }}\",\n  \"to\": [\"{{ $json.email }}\"],\n  \"sender\": \"user@example.com\",\n  \"subject\": \"Ready to get started, {{ $json.firstName }}?\",\n  \"body_text\": \"Here is the link to get started: https://www.voicesdr.ai/pricing . Pro Plan $1,500/mo, Enterprise $3,500/mo. Talk soon, Laura, VoiceSDR\",\n  \"status\": \"outbox\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "updatePaymentSent",
      "name": "Update Lead Payment Sent",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1160,
        600
      ],
      "parameters": {
        "method": "PUT",
        "url": "=https://api.close.com/api/v1/lead/{{ $('Normalize Retell Data').first().json.closeLeadId }}/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"status_id\": \"stat_hil86olYhMqD05K6UK0LInMnRnK06u04is5kACpxIOi\",\n  \"custom.cf_V5jsUjRfHPrssrsBdo2o2L4QwgZrk5WyJzGwJzNadpn\": \"https://www.voicesdr.ai/pricing\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "sendVslSms1",
      "name": "Send VSL SMS",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        940,
        780
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.close.com/api/v1/activity/sms/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"lead_id\": \"{{ $json.closeLeadId }}\",\n  \"local_phone\": \"+15555550100\",\n  \"remote_phone\": \"{{ $json.phone || $json.toNumber }}\",\n  \"text\": \"Hey {{ $json.firstName }}, Laura from VoiceSDR here. Here's the quick overview: https://www.voicesdr.ai/see-how-it-works - takes 2 min. Reply with questions!\",\n  \"direction\": \"outbound\",\n  \"status\": \"outbox\"\n}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "setDncStatus",
      "name": "Set DNC Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        940,
        960
      ],
      "parameters": {
        "method": "PUT",
        "url": "=https://api.close.com/api/v1/lead/{{ $json.closeLeadId }}/",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ 'status_id': 'stat_OXnpX5vuI6Ujl1AOFHpG4lNsZY5Ek0s9tpzJXIHmw10', 'custom.cf_YvT1cF5nYedJGt3kLR87jhCNZ1vsXIVmVIXQqnaTnrp': true }) }}",
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "stop_outbound",
      "name": "Stop Outbound Sequence",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1600,
        400
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "var data=$('Normalize Retell Data').first().json; var closeLeadId=data.closeLeadId||''; var action=data.action||'unknown'; var closeApiKey='YOUR_CLOSE_API_KEY'; var outboundStatus='completed'; if(action==='book_callback'){ outboundStatus='paused'; } if(action==='mark_dnc'){ outboundStatus='dnc'; } if(closeLeadId){ try{ await this.helpers.httpRequest({ method:'PUT', url:'https://api.close.com/api/v1/lead/'+closeLeadId+'/', headers:{'Content-Type':'application/json'}, auth:{username:closeApiKey,password:''}, body:{ 'custom.cf_U9vqoYVuHP0wx0fu9pdQzRYPWsIZSEFNfmIueOnII9v':outboundStatus } }); }catch(e){} try{ await this.helpers.httpRequest({ method:'POST', url:'https://YOUR_CONVEX_DEPLOYMENT/api/outbound-schedule/update', headers:{'Content-Type':'application/json'}, body:{ closeLeadId:closeLeadId, outboundStatus:outboundStatus, lastOutcome:action+'_during_call', lastCallAt:Date.now() } }); }catch(e){} } return { json: { outboundStopped:true, status:outboundStatus, action:action } };"
      }
    },
    {
      "id": "respond_tool",
      "name": "Respond Tool Success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1820,
        400
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, action: $json.action || 'completed', outboundStopped: $json.outboundStopped || false }) }}"
      }
    }
  ],
  "connections": {
    "Agent Tool Webhook": {
      "main": [
        [
          {
            "node": "Normalize Retell Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Retell Data": {
      "main": [
        [
          {
            "node": "Route Action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route Action": {
      "main": [
        [
          {
            "node": "Send VSL Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create Close CRM Task",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Slack Hot Lead Alert",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Payment Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send VSL SMS",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set DNC Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send VSL Email": {
      "main": [
        [
          {
            "node": "Update Lead Demo Sent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Lead Demo Sent": {
      "main": [
        [
          {
            "node": "Stop Outbound Sequence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Close CRM Task": {
      "main": [
        [
          {
            "node": "Update Lead Appointment Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Lead Appointment Set": {
      "main": [
        [
          {
            "node": "Send Callback Confirmation SMS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Callback Confirmation SMS": {
      "main": [
        [
          {
            "node": "Stop Outbound Sequence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slack Hot Lead Alert": {
      "main": [
        [
          {
            "node": "Stop Outbound Sequence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Payment Email": {
      "main": [
        [
          {
            "node": "Update Lead Payment Sent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Lead Payment Sent": {
      "main": [
        [
          {
            "node": "Stop Outbound Sequence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send VSL SMS": {
      "main": [
        [
          {
            "node": "Stop Outbound Sequence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set DNC Status": {
      "main": [
        [
          {
            "node": "Stop Outbound Sequence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stop Outbound Sequence": {
      "main": [
        [
          {
            "node": "Respond Tool Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false
}