Seth commited on
Commit
d9c2642
·
1 Parent(s): 6cc39be
Files changed (1) hide show
  1. backend/app/main.py +36 -12
backend/app/main.py CHANGED
@@ -443,30 +443,37 @@ 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
- # Smartlead requires customFields (camelCase) with custom fields defined at campaign level
 
 
 
447
  lead = {
448
  "email": email,
449
  "first_name": first_name,
450
  "last_name": last_name
451
  }
452
 
453
- # Build customFields object (camelCase) - fields must match campaign custom field definitions
454
- customFields = {}
 
455
  for i in range(1, 11):
456
  if i in contact.get('subjects', {}):
457
  subject = contact['subjects'][i]
458
  subject_str = safe_str(subject) if subject else ""
459
  if subject_str:
460
- customFields[f'subject_{i}'] = subject_str
461
  if i in contact.get('bodies', {}):
462
  body = contact['bodies'][i]
463
  body_str = safe_str(body) if body else ""
464
  if body_str:
465
- customFields[f'body_{i}'] = body_str
466
 
467
- # Only add customFields if we have any
468
- if customFields:
469
- lead["customFields"] = customFields
 
 
 
470
 
471
  leads.append(lead)
472
 
@@ -478,16 +485,22 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
478
  failed_count += 1
479
 
480
  # Batch add leads (chunk in batches of 50)
 
481
  batch_size = 50
482
  for i in range(0, len(leads), batch_size):
483
  batch = leads[i:i + batch_size]
 
 
 
 
 
484
  try:
485
- response = client.add_leads_to_campaign(campaign_id, batch)
486
  # TODO: Parse response to get added/skipped counts
487
- added_count += len(batch)
488
  except Exception as e:
489
  # Mark batch as failed
490
- for lead in batch:
491
  errors.append({
492
  "email": lead.get('email', 'unknown'),
493
  "error": str(e)
@@ -503,6 +516,16 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
503
  run.completed_at = datetime.utcnow()
504
  db.commit()
505
 
 
 
 
 
 
 
 
 
 
 
506
  return {
507
  "run_id": run_id,
508
  "campaign_id": campaign_id,
@@ -513,7 +536,8 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
513
  "skipped": skipped_count,
514
  "failed": failed_count,
515
  "errors": errors[:10], # Return first 10 errors
516
- "status": "completed"
 
517
  }
518
 
519
  except HTTPException:
 
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
  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
  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,
531
  "campaign_id": campaign_id,
 
536
  "skipped": skipped_count,
537
  "failed": failed_count,
538
  "errors": errors[:10], # Return first 10 errors
539
+ "status": "completed",
540
+ "warning": warning_message
541
  }
542
 
543
  except HTTPException: