Compare commits

..

No commits in common. "7c5074098c0033718269b53d44f60a7d968eccc1" and "b01a40a1fd71dd0abfcb8c784670fc38c20c8d63" have entirely different histories.

3 changed files with 50 additions and 105 deletions

View file

@ -6,7 +6,7 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "NODE_OPTIONS='--dns-result-order=ipv4first' node src/server.js" "start": "node src/server.js"
}, },
"author": "ryan", "author": "ryan",
"license": "ISC", "license": "ISC",

View file

@ -7,6 +7,7 @@ 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);
@ -19,37 +20,21 @@ export async function submitToListmonk(req, res) {
email, email,
name, name,
lists: [listId], lists: [listId],
status: "enabled",
fields: { message } fields: { message }
}; };
console.log('Sending request to Listmonk with payload:', payload); console.log('Sending request to Listmonk with payload:', payload);
async function fetchWithRetry(url, options, retries = 3) {
for (let i = 0; i < retries; i++) {
console.log(`Attempt ${i + 1} sending request to ${url}`);
try {
const res = await fetch(url, options);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res;
} catch (err) {
console.log(`Attempt ${i + 1} failed: ${err}`);
if (i === retries - 1) throw err;
await new Promise(resolve => setTimeout(resolve, 500));
}
}
}
try { try {
// Add subscriber // Add subscriber
const response = await fetchWithRetry(listmonkUrl, { const response = await fetch(listmonkUrl, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Basic ${auth}` 'Authorization': `Basic ${auth}`
}, },
body: JSON.stringify(payload), body: JSON.stringify(payload),
},12); });
if (!response.ok) { if (!response.ok) {
const errorBody = await response.text(); const errorBody = await response.text();
@ -59,98 +44,63 @@ 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);
// Create a new campaign for the subscriber // Update campaign status to "scheduled"
const createCampaign = async (subject, body) => { const campaignStatusUrl = `${listmonkBaseUrl}/campaigns/${campaignId}/status`;
const createCampaignUrl = `${listmonkBaseUrl}/campaigns`; const scheduleTime = new Date(Date.now() + 5 * 60 * 1000).toISOString(); // Schedule 5 minutes from now
const campaignPayload = { const statusPayload = {
name: `Welcome email for ${email}`, status: 'scheduled',
subject: subject, schedule: scheduleTime,
from_email: "Janek <janek@melonion.me>", };
body: body,
type: "regular",
to_emails: [email],
lists: [listId],
content_type: "richtext",
status: "scheduled",
schedule: new Date().toISOString()
};
const response = await fetch(createCampaignUrl, { console.log('Updating campaign status using URL:', campaignStatusUrl);
method: 'POST', console.log('Campaign status payload:', statusPayload);
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(campaignPayload), body: JSON.stringify(statusPayload),
}); });
if (!response.ok) { if (!statusResponse.ok) {
const errorBody = await response.text(); const errorBody = await statusResponse.text();
throw new Error(`Listmonk API error (Create Campaign): ${response.statusText}. Response body: ${errorBody}`); console.error('Response from Listmonk:', errorBody);
throw new Error(`Listmonk API error (Update Campaign Status): ${statusResponse.statusText}. Response body: ${errorBody}`);
} }
const result = await response.json(); const statusResult = await statusResponse.json();
return result.data.id; console.log('Campaign status updated successfully:', statusResult);
}; } catch (error) {
console.error('Error updating campaign status:', error);
// Campaign subject and body content
const subject = "👋 Welcome to the Progodyssey Family!";
const body = `
<h1>👋 Welcome to the Progodyssey Family, {{ .Subscriber.Name }}!</h1>
<p>Were thrilled to have you as part of the Progodyssey Traineeship journey! 🎉
This program is designed to equip you with the skills, experience, and network to thrive in the tech world and beyond.</p>
<p>Heres what you can expect in the coming weeks:</p>
<ul>
<li>1 <strong>Learning Opportunities:</strong> Access to tutorials, and hands-on projects to sharpen your skills.</li>
<li>2 <strong>Mentorship:</strong> Personal Guidance to help you navigate your journey.</li>
<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 (!dispatchResponse.ok) {
const errorBody = await dispatchResponse.text();
console.error('Response from Listmonk (Dispatch):', errorBody);
throw new Error(`Listmonk API error (Dispatch Campaign): ${dispatchResponse.statusText}. Response body: ${errorBody}`);
} }
const dispatchResult = await dispatchResponse.json(); const statusResult = await statusResponse.json();
console.log('Campaign dispatched successfully:', dispatchResult); console.log('Campaign status updated successfully:', statusResult);
res.status(200).json({ message: 'Subscription and welcome email sent successfully!', result }); // Send the campaign
const campaignSendUrl = `${listmonkBaseUrl}/campaigns/${campaignId}/send`;
console.log('Sending campaign using URL:', campaignSendUrl);
const campaignResponse = await fetch(campaignSendUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`,
},
});
if (!campaignResponse.ok) {
const errorBody = await campaignResponse.text();
throw new Error(`Listmonk API error (Send Campaign): ${campaignResponse.statusText}. Response body: ${errorBody}`);
}
const campaignResult = await campaignResponse.json();
console.log('Campaign sent successfully:', campaignResult);
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 });

View file

@ -1,5 +0,0 @@
import dns from 'dns';
dns.lookup('mailer.melonion.me', { all: true }, (err, addresses) => {
console.log(addresses);
});