-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
421 lines (371 loc) · 11.9 KB
/
index.js
File metadata and controls
421 lines (371 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
* @profullstack/lead-generator - Main module export
* Lead generation tool for mass email campaigns with AI personalization
*/
// Core modules
export {
parseCSV,
validateLeadData,
extractEmails,
processLeadsFromCSV,
getLeadStats
} from './src/csv-parser.js';
export {
AIService,
personalizeTemplate,
generatePlaceholderValues,
batchPersonalize,
getPersonalizationStats
} from './src/ai-service.js';
export {
EmailService,
sendEmail,
sendBatchEmails,
createEmailFromTemplate,
getEmailStats,
validateMailgunConfig
} from './src/email-service.js';
export {
VoiceService,
makeVoiceCall,
emailToVoiceScript,
isValidPhoneNumber,
formatPhoneNumber,
getVoiceServiceStatus
} from './src/voice-service.js';
// Email Templates
export {
templates,
templatesByCategory,
templatesByTone,
getTemplateById,
getTemplatesByCategory,
getTemplatesByTone,
getRandomTemplate,
getAllTemplateIds,
getAllCategories,
getAllTones
} from './src/email-templates/index.js';
// Import for internal use
import {
templates as emailTemplates,
getTemplateById as getEmailTemplateById,
getAllTemplateIds as getAllEmailTemplateIds
} from './src/email-templates/index.js';
// SMS Templates and Services
export {
smsTemplates,
smsTemplatesByCategory,
smsTemplatesByTone,
getSmsTemplateById,
getSmsTemplatesByCategory,
getSmsTemplatesByTone,
getRandomSmsTemplate,
getAllSmsTemplateIds,
getAllSmsCategories,
getAllSmsTones,
personalizeSmsTemplate
} from './src/sms-templates/index.js';
export {
SmsService,
createSmsFromTemplate,
sendBatchSmsFromTemplates
} from './src/sms-service.js';
/**
* Lead Generator class - Main orchestrator
*/
export class LeadGenerator {
constructor(config = {}) {
this.config = {
// Mailgun configuration
mailgun: {
apiKey: config.mailgunApiKey || process.env.MAILGUN_API_KEY,
domain: config.mailgunDomain || process.env.MAILGUN_DOMAIN,
baseUrl: config.mailgunBaseUrl || process.env.MAILGUN_BASE_URL
},
// OpenAI configuration
openai: {
apiKey: config.openaiApiKey || process.env.OPENAI_API_KEY,
model: config.openaiModel || process.env.OPENAI_MODEL || 'gpt-4o-mini',
maxTokens: config.openaiMaxTokens || parseInt(process.env.OPENAI_MAX_TOKENS) || 500,
temperature: config.openaiTemperature || parseFloat(process.env.OPENAI_TEMPERATURE) || 0.7
},
// Sender information
sender: {
name: config.senderName || process.env.DEFAULT_FROM_NAME,
email: config.senderEmail || process.env.DEFAULT_FROM_EMAIL,
title: config.senderTitle || 'Sales Director',
replyTo: config.replyTo || process.env.DEFAULT_REPLY_TO
},
// Campaign settings
campaign: {
batchSize: config.batchSize || parseInt(process.env.BATCH_SIZE) || 300,
delay: config.delay || parseInt(process.env.BATCH_DELAY_MS) || 1000,
maxRetries: config.maxRetries || parseInt(process.env.MAX_RETRIES) || 3,
enablePersonalization: config.enablePersonalization !== false,
trackOpens: config.trackOpens !== false,
trackClicks: config.trackClicks !== false
},
...config
};
}
/**
* Run complete lead generation campaign
* @param {string} csvFilePath - Path to CSV file with leads
* @param {Object} options - Campaign options
* @returns {Promise<Object>} Campaign results
*/
async runCampaign(csvFilePath, options = {}) {
const campaignOptions = { ...this.config.campaign, ...options };
const results = {
timestamp: new Date().toISOString(),
csvFile: csvFilePath,
config: campaignOptions
};
try {
// Step 1: Process CSV file
console.log('📄 Processing CSV file...');
const csvResults = await processLeadsFromCSV(csvFilePath);
results.csvProcessing = csvResults;
if (csvResults.validLeads.length === 0) {
throw new Error('No valid leads found in CSV file');
}
// Step 2: Select templates
let selectedTemplates = options.templates || emailTemplates;
if (options.templateId) {
const template = getEmailTemplateById(options.templateId);
if (!template) {
throw new Error(`Template not found: ${options.templateId}`);
}
selectedTemplates = [template];
}
// Step 3: Personalize emails
console.log('🤖 Personalizing emails...');
const personalizationResults = await batchPersonalize(
selectedTemplates,
csvResults.validLeads,
this.config.sender,
{
...this.config.openai,
fallbackToBasic: true,
batchSize: 10,
delay: 500
}
);
results.personalization = getPersonalizationStats(personalizationResults);
// Step 4: Create email data
const emailsToSend = personalizationResults
.filter(r => r.success)
.map(result => createEmailFromTemplate(
result.email,
result.lead,
this.config.sender,
{
trackOpens: campaignOptions.trackOpens,
trackClicks: campaignOptions.trackClicks
}
));
// Step 5: Send emails
console.log('📧 Sending emails...');
const sendResults = await sendBatchEmails(emailsToSend, {
...this.config.mailgun,
batchSize: campaignOptions.batchSize,
delay: campaignOptions.delay,
maxRetries: campaignOptions.maxRetries,
dryRun: campaignOptions.dryRun
});
results.sending = sendResults;
// Step 6: Generate final statistics
results.summary = {
totalLeads: csvResults.stats.total,
validLeads: csvResults.stats.valid,
emailsSent: sendResults.successful,
emailsFailed: sendResults.failed,
successRate: sendResults.successRate,
personalizationRate: results.personalization.aiEnhancementRate
};
return results;
} catch (error) {
results.error = error.message;
throw error;
}
}
/**
* Validate configuration
* @returns {Object} Validation result
*/
validateConfig() {
const errors = [];
// Check Mailgun config
if (!this.config.mailgun.apiKey) {
errors.push('Mailgun API key is required');
}
if (!this.config.mailgun.domain) {
errors.push('Mailgun domain is required');
}
// Check sender config
if (!this.config.sender.email) {
errors.push('Sender email is required');
}
if (!this.config.sender.name) {
errors.push('Sender name is required');
}
// OpenAI is optional but warn if personalization is enabled
if (this.config.campaign.enablePersonalization && !this.config.openai.apiKey) {
errors.push('OpenAI API key is required for AI personalization (or disable personalization)');
}
return {
isValid: errors.length === 0,
errors,
warnings: []
};
}
/**
* Get available templates
* @param {Object} filters - Template filters
* @returns {Array} Filtered templates
*/
getTemplates(filters = {}) {
let filteredTemplates = emailTemplates;
if (filters.category) {
filteredTemplates = getTemplatesByCategory(filters.category);
}
if (filters.tone) {
filteredTemplates = getTemplatesByTone(filters.tone);
}
return filteredTemplates;
}
/**
* Preview personalized email
* @param {string} templateId - Template ID
* @param {Object} leadData - Sample lead data
* @returns {Promise<Object>} Personalized email preview
*/
async previewEmail(templateId, leadData) {
const template = getEmailTemplateById(templateId);
if (!template) {
throw new Error(`Template not found: ${templateId}`);
}
return await personalizeTemplate(
template,
leadData,
this.config.sender,
{
...this.config.openai,
fallbackToBasic: true
}
);
}
}
/**
* Quick start function for simple campaigns
* @param {string} csvFilePath - Path to CSV file
* @param {Object} config - Configuration object
* @returns {Promise<Object>} Campaign results
*/
export async function quickStart(csvFilePath, config = {}) {
const generator = new LeadGenerator(config);
// Validate configuration
const validation = generator.validateConfig();
if (!validation.isValid) {
throw new Error(`Configuration invalid: ${validation.errors.join(', ')}`);
}
return await generator.runCampaign(csvFilePath, config);
}
/**
* Utility function to create sample CSV
* @param {string} filePath - Output file path
* @param {number} count - Number of sample leads
*/
export function createSampleCSV(filePath, count = 5) {
const sampleData = [
'FirstName,LastName,Company,WorkEmail,PersonalEmail,Phone,Industry,Title',
'John,Doe,Acme Corp,john.doe@acme.com,john@personal.com,555-0123,Technology,CTO',
'Jane,Smith,Beta Inc,jane.smith@beta.com,,555-0456,Healthcare,VP Engineering',
'Bob,Johnson,Gamma LLC,,bob@gmail.com,555-0789,Finance,Director',
'Alice,Williams,Delta Co,alice@delta.co,alice.w@email.com,555-0321,Marketing,Manager',
'Charlie,Brown,Echo Ltd,charlie.brown@echo.ltd,,555-0654,Consulting,Partner'
];
const csvContent = sampleData.slice(0, count + 1).join('\n');
import('fs').then(fs => {
fs.writeFileSync(filePath, csvContent);
console.log(`Sample CSV created: ${filePath}`);
});
}
// Voice Templates
export {
voiceTemplates,
voiceTemplatesByCategory,
voiceTemplatesByTone,
getVoiceTemplateById,
getVoiceTemplatesByCategory,
getVoiceTemplatesByTone,
getRandomVoiceTemplate,
getAllVoiceTemplateIds,
getAllVoiceCategories,
getAllVoiceTones
} from './src/voice-templates/index.js';
// CSV Status Tracker
export {
addStatusColumns,
updateCSVStatus,
getStatusFromCSV,
createStatusBackup,
getContactsByStatus,
EMAIL_STATUSES as EMAIL_STATUS,
VOICE_STATUSES as VOICE_STATUS,
SMS_STATUS
} from './src/csv-status-tracker.js';
// Convenience functions for SMS lead generation
export async function generateSmsLeads(csvFilePath, options = {}) {
const { SmsService } = await import('./src/sms-service.js');
const { processLeadsFromCSV } = await import('./src/csv-parser.js');
const { smsTemplates, getSmsTemplateById } = await import('./src/sms-templates/index.js');
const smsService = new SmsService(options.twilio || {});
const csvResults = await processLeadsFromCSV(csvFilePath);
let selectedTemplates = options.templates || smsTemplates;
if (options.templateId) {
const template = getSmsTemplateById(options.templateId);
if (!template) {
throw new Error(`SMS template not found: ${options.templateId}`);
}
selectedTemplates = [template];
}
return await smsService.sendBatchFromTemplates(
selectedTemplates,
csvResults.validLeads,
options
);
}
// Convenience functions for voice lead generation
export async function generateVoiceLeads(csvFilePath, options = {}) {
const { VoiceService } = await import('./src/voice-service.js');
const { processLeadsFromCSV } = await import('./src/csv-parser.js');
const { voiceTemplates, getVoiceTemplateById } = await import('./src/voice-templates/index.js');
const voiceService = new VoiceService(options.twilio || {});
const csvResults = await processLeadsFromCSV(csvFilePath);
let selectedTemplates = options.templates || voiceTemplates;
if (options.templateId) {
const template = getVoiceTemplateById(options.templateId);
if (!template) {
throw new Error(`Voice template not found: ${options.templateId}`);
}
selectedTemplates = [template];
}
return await voiceService.makeBatchCalls(
selectedTemplates,
csvResults.validLeads,
options
);
}
// Default export
export default {
LeadGenerator,
quickStart,
createSampleCSV,
templates: emailTemplates,
getTemplateById: getEmailTemplateById,
getAllTemplateIds: getAllEmailTemplateIds,
generateSmsLeads,
generateVoiceLeads
};