feat(campaigns): create and dispatch a welcome campaign to new subscribers

This commit is contained in:
ryan 2025-04-17 15:14:52 +03:00
parent 17c110934f
commit 7c5074098c

View file

@ -7,7 +7,6 @@ export async function submitToListmonk(req, res) {
const listmonkUrl = process.env.LISTMONK_URL; const listmonkUrl = process.env.LISTMONK_URL;
const listmonkUsername = process.env.LISTMONK_USERNAME; const listmonkUsername = process.env.LISTMONK_USERNAME;
const listmonkPassword = process.env.LISTMONK_PASSWORD; const listmonkPassword = process.env.LISTMONK_PASSWORD;
const campaignId = process.env.PRESET_CAMPAIGN_ID;
const listmonkBaseUrl = process.env.LISTMONK_BASE_URL; const listmonkBaseUrl = process.env.LISTMONK_BASE_URL;
const listId = parseInt(process.env.LIST_ID, 10); const listId = parseInt(process.env.LIST_ID, 10);
@ -20,6 +19,7 @@ export async function submitToListmonk(req, res) {
email, email,
name, name,
lists: [listId], lists: [listId],
status: "enabled",
fields: { message } fields: { message }
}; };
@ -49,7 +49,7 @@ export async function submitToListmonk(req, res) {
'Authorization': `Basic ${auth}` 'Authorization': `Basic ${auth}`
}, },
body: JSON.stringify(payload), body: JSON.stringify(payload),
},3); },12);
if (!response.ok) { if (!response.ok) {
const errorBody = await response.text(); const errorBody = await response.text();
@ -59,63 +59,98 @@ export async function submitToListmonk(req, res) {
const result = await response.json(); const result = await response.json();
console.log('Subscriber added successfully:', result); console.log('Subscriber added successfully:', result);
// Update campaign status to "scheduled" // Create a new campaign for the subscriber
const campaignStatusUrl = `${listmonkBaseUrl}/campaigns/${campaignId}/status`; const createCampaign = async (subject, body) => {
const scheduleTime = new Date(Date.now() + 5 * 60 * 1000).toISOString(); // Schedule 5 minutes from now const createCampaignUrl = `${listmonkBaseUrl}/campaigns`;
const statusPayload = { const campaignPayload = {
status: 'scheduled', name: `Welcome email for ${email}`,
schedule: scheduleTime, subject: subject,
}; from_email: "Janek <janek@melonion.me>",
body: body,
type: "regular",
to_emails: [email],
lists: [listId],
content_type: "richtext",
status: "scheduled",
schedule: new Date().toISOString()
};
console.log('Updating campaign status using URL:', campaignStatusUrl); const response = await fetch(createCampaignUrl, {
console.log('Campaign status payload:', statusPayload); method: 'POST',
try {
const statusResponse = await fetch(campaignStatusUrl, {
method: 'PUT',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`, 'Authorization': `Basic ${auth}`,
}, },
body: JSON.stringify(statusPayload), body: JSON.stringify(campaignPayload),
}); });
if (!statusResponse.ok) { if (!response.ok) {
const errorBody = await statusResponse.text(); const errorBody = await response.text();
console.error('Response from Listmonk:', errorBody); throw new Error(`Listmonk API error (Create Campaign): ${response.statusText}. Response body: ${errorBody}`);
throw new Error(`Listmonk API error (Update Campaign Status): ${statusResponse.statusText}. Response body: ${errorBody}`);
} }
const statusResult = await statusResponse.json(); const result = await response.json();
console.log('Campaign status updated successfully:', statusResult); return result.data.id;
} catch (error) { };
console.error('Error updating campaign status:', error);
}
const statusResult = await statusResponse.json(); // Campaign subject and body content
console.log('Campaign status updated successfully:', statusResult); const subject = "👋 Welcome to the Progodyssey Family!";
const body = `
<h1>👋 Welcome to the Progodyssey Family, {{ .Subscriber.Name }}!</h1>
// Send the campaign <p>Were thrilled to have you as part of the Progodyssey Traineeship journey! 🎉
const campaignSendUrl = `${listmonkBaseUrl}/campaigns/${campaignId}/send`; This program is designed to equip you with the skills, experience, and network to thrive in the tech world and beyond.</p>
console.log('Sending campaign using URL:', campaignSendUrl);
const campaignResponse = await fetch(campaignSendUrl, { <p>Heres what you can expect in the coming weeks:</p>
method: 'POST', <ul>
headers: { <li>1 <strong>Learning Opportunities:</strong> Access to tutorials, and hands-on projects to sharpen your skills.</li>
'Content-Type': 'application/json', <li>2 <strong>Mentorship:</strong> Personal Guidance to help you navigate your journey.</li>
'Authorization': `Basic ${auth}`, <li>3 <strong>Community:</strong> A supportive group of peers and mentors working together towards success.</li>
}, </ul>
<p>To kick things off, go through the Progodyssey handbook, pick a course and submit your reflection essay to <a href="mailto:reflection@progodyssey.com">reflection@progodyssey.com</a>.</p>
<p>Janeks Blog could also have some insight for you.</p>
<p><em>Encouragement:</em><br>
<em>"Success is the sum of small efforts repeated day in and day out."</em> <cite>Robert Collier</cite></p>
<p>Lets take the first step together!<br>
If you have any questions or need assistance, feel free to reply to this email or contact us at <a href="mailto:hello@progodyssey.com">hello@progodyssey.com</a></p>
<p>Looking forward to seeing you thrive,<br>
<strong>The Progodyssey Team</strong></p>
`;
// 1. Create campaign
const newCampaignId = await createCampaign(subject, body);
console.log(`Created new campaign with ID: ${newCampaignId}`);
// 2. Dispatch campaign
const dispatchUrl = `${listmonkBaseUrl}/campaigns/${newCampaignId}/status`;
const dispatchPayload = {
status: 'running'
};
const dispatchResponse = await fetch(dispatchUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`,
},
body: JSON.stringify(dispatchPayload),
}); });
if (!campaignResponse.ok) { if (!dispatchResponse.ok) {
const errorBody = await campaignResponse.text(); const errorBody = await dispatchResponse.text();
throw new Error(`Listmonk API error (Send Campaign): ${campaignResponse.statusText}. Response body: ${errorBody}`); console.error('Response from Listmonk (Dispatch):', errorBody);
throw new Error(`Listmonk API error (Dispatch Campaign): ${dispatchResponse.statusText}. Response body: ${errorBody}`);
} }
const campaignResult = await campaignResponse.json(); const dispatchResult = await dispatchResponse.json();
console.log('Campaign sent successfully:', campaignResult); console.log('Campaign dispatched successfully:', dispatchResult);
res.status(200).json({ message: 'Subscription and welcome email sent successfully!', result });
res.status(200).json({ message: 'Subscription successful!', result });
} catch (error) { } catch (error) {
console.error('Error submitting to Listmonk:', error); console.error('Error submitting to Listmonk:', error);
res.status(500).json({ message: 'Failed to submit to Listmonk', error: error.message }); res.status(500).json({ message: 'Failed to submit to Listmonk', error: error.message });