Skip to content
// Instant replies: a short SMS and a fuller email, written from the assigned agent.
const s = $('Settings (edit me)').first().json;
const l = $input.first().json;
const money = (n) => '$' + Number(n).toLocaleString('en-US');
const home = (m) => `${m.address}, ${m.city}: ${money(m.price)}${m.type === 'rent' ? '/mo' : ''}, ${m.beds} bd / ${m.baths} ba`;
const agentFirst = l.agent.name.split(' ')[0];
const about = l.asked_listing?.address ?? (l.property || 'your home search');
const availability = l.asked_listing ? (l.asked_listing.status === 'active' ? "It's still available" : "That one just went under contract, but I have similar homes") : 'Happy to help';

let sms;
let subject;
let body;
if (l.intent === 'seller') {
  sms = `Hi ${l.first_name}, it's ${agentFirst} with ${s.brokerage}. Thanks for reaching out about selling! I can put together a free home value report this week. When's a good time for a quick call? ${s.booking_link}`;
  subject = `Your home value report, ${l.first_name}`;
  body = `Hi ${l.first_name},\n\nThanks for reaching out about selling your home. I'll prepare a free, no-obligation market analysis based on recent sales nearby.\n\nPick a time that suits you for a 15-minute call: ${s.booking_link}\n\nBest,\n${l.agent.name}\n${s.brokerage} · ${l.agent.phone}`;
} else if (l.is_duplicate) {
  sms = `Hi ${l.first_name}, ${agentFirst} again from ${s.brokerage}. Got your note about ${about}. Want to see it? Reply with a day that works or book here: ${s.booking_link}`;
  subject = `Re: ${about}`;
  body = `Hi ${l.first_name},\n\nThanks for reaching out again about ${about}. I'd love to show it to you. Book a time here: ${s.booking_link}\n\nBest,\n${l.agent.name}\n${s.brokerage} · ${l.agent.phone}`;
} else {
  sms = `Hi ${l.first_name}, it's ${agentFirst} with ${s.brokerage}. Thanks for asking about ${about}! ${availability}. Want to see it this week? Reply with a day/time or book: ${s.booking_link}`;
  subject = `${about}: details + ${l.matches.length} similar home${l.matches.length === 1 ? '' : 's'}`;
  body = `Hi ${l.first_name},\n\nThanks for your interest in ${about}. ${availability}.\n` +
    (l.matches.length ? `\nYou might also like:\n${l.matches.map((m) => `• ${home(m)}\n  ${m.url}`).join('\n')}\n` : '') +
    `\nWant to see ${l.asked_listing ? 'it' : 'some homes'} in person? Book a showing here: ${s.booking_link}` +
    (l.pre_approved ? '' : `\n\nIf you're buying, getting pre-approved first makes your offer stronger. Happy to introduce a lender.`) +
    `\n\nBest,\n${l.agent.name}\n${s.brokerage} · ${l.agent.phone}`;
}
sms = (sms + ' Reply STOP to opt out.').slice(0, 320);

return [{ json: { ...l, messages: { sms, email: { to: l.email, subject, body } } } }];