Seth commited on
Commit
65523ca
·
1 Parent(s): d9c2642
Files changed (1) hide show
  1. backend/app/main.py +12 -35
backend/app/main.py CHANGED
@@ -443,37 +443,28 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
443
  continue
444
 
445
  # Build lead object - Smartlead API doesn't allow "company" or "title" fields
446
- # NOTE: Smartlead API appears to reject customFields/custom_variables in lead payload
447
- # Custom fields may need to be defined in Smartlead UI first, or via a different API endpoint
448
- # For now, we'll add leads with basic info only
449
- # The custom variables (subject_1, body_1, etc.) may need to be handled differently
450
  lead = {
451
  "email": email,
452
  "first_name": first_name,
453
  "last_name": last_name
454
  }
455
 
456
- # Store custom variables separately for potential future use
457
- # These would need to be added via Smartlead UI or a different API method
458
- custom_vars_data = {}
459
- for i in range(1, 11):
460
  if i in contact.get('subjects', {}):
461
  subject = contact['subjects'][i]
462
  subject_str = safe_str(subject) if subject else ""
463
  if subject_str:
464
- custom_vars_data[f'subject_{i}'] = subject_str
465
  if i in contact.get('bodies', {}):
466
  body = contact['bodies'][i]
467
  body_str = safe_str(body) if body else ""
468
  if body_str:
469
- custom_vars_data[f'body_{i}'] = body_str
470
-
471
- # Store in a comment/metadata field for reference (won't be sent to API)
472
- # TODO: Find correct Smartlead API method to add custom variables per lead
473
- lead['_metadata'] = {
474
- 'custom_vars': custom_vars_data,
475
- 'note': 'Custom variables need to be added via Smartlead UI or API after lead creation'
476
- }
477
 
478
  leads.append(lead)
479
 
@@ -485,22 +476,16 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
485
  failed_count += 1
486
 
487
  # Batch add leads (chunk in batches of 50)
488
- # Remove _metadata before sending to API
489
  batch_size = 50
490
  for i in range(0, len(leads), batch_size):
491
  batch = leads[i:i + batch_size]
492
- # Clean batch - remove internal _metadata field
493
- clean_batch = []
494
- for lead in batch:
495
- clean_lead = {k: v for k, v in lead.items() if k != '_metadata'}
496
- clean_batch.append(clean_lead)
497
  try:
498
- response = client.add_leads_to_campaign(campaign_id, clean_batch)
499
  # TODO: Parse response to get added/skipped counts
500
- added_count += len(clean_batch)
501
  except Exception as e:
502
  # Mark batch as failed
503
- for lead in clean_batch:
504
  errors.append({
505
  "email": lead.get('email', 'unknown'),
506
  "error": str(e)
@@ -516,15 +501,7 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
516
  run.completed_at = datetime.utcnow()
517
  db.commit()
518
 
519
- # Warning message about custom variables
520
- warning_message = None
521
- if added_count > 0:
522
- warning_message = (
523
- "Leads were added successfully, but custom variables (subject_1, body_1, etc.) "
524
- "could not be set via API. These need to be added manually in Smartlead UI, or "
525
- "the campaign sequences may not work correctly. Please check Smartlead documentation "
526
- "for the correct method to add custom variables per lead."
527
- )
528
 
529
  return {
530
  "run_id": run_id,
 
443
  continue
444
 
445
  # Build lead object - Smartlead API doesn't allow "company" or "title" fields
446
+ # Try passing custom variables as direct top-level fields (not nested)
447
+ # Smartlead sequences use {{subject_1}}, {{body_1}}, etc. - these should be passed as direct fields
 
 
448
  lead = {
449
  "email": email,
450
  "first_name": first_name,
451
  "last_name": last_name
452
  }
453
 
454
+ # Add custom variables as direct top-level fields on the lead object
455
+ # Smartlead sequences use {{subject_1}}, {{body_1}}, etc. - pass values as direct fields
456
+ # Only include fields up to steps_count to match the campaign sequences
457
+ for i in range(1, request.steps_count + 1):
458
  if i in contact.get('subjects', {}):
459
  subject = contact['subjects'][i]
460
  subject_str = safe_str(subject) if subject else ""
461
  if subject_str:
462
+ lead[f'subject_{i}'] = subject_str
463
  if i in contact.get('bodies', {}):
464
  body = contact['bodies'][i]
465
  body_str = safe_str(body) if body else ""
466
  if body_str:
467
+ lead[f'body_{i}'] = body_str
 
 
 
 
 
 
 
468
 
469
  leads.append(lead)
470
 
 
476
  failed_count += 1
477
 
478
  # Batch add leads (chunk in batches of 50)
 
479
  batch_size = 50
480
  for i in range(0, len(leads), batch_size):
481
  batch = leads[i:i + batch_size]
 
 
 
 
 
482
  try:
483
+ response = client.add_leads_to_campaign(campaign_id, batch)
484
  # TODO: Parse response to get added/skipped counts
485
+ added_count += len(batch)
486
  except Exception as e:
487
  # Mark batch as failed
488
+ for lead in batch:
489
  errors.append({
490
  "email": lead.get('email', 'unknown'),
491
  "error": str(e)
 
501
  run.completed_at = datetime.utcnow()
502
  db.commit()
503
 
504
+ # No warning needed - custom variables are now passed as direct fields
 
 
 
 
 
 
 
 
505
 
506
  return {
507
  "run_id": run_id,