Compare commits

...

2 commits

3 changed files with 101 additions and 46 deletions

View file

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

View file

@ -7,7 +7,6 @@ export async function submitToListmonk(req, res) {
const listmonkUrl = process.env.LISTMONK_URL;
const listmonkUsername = process.env.LISTMONK_USERNAME;
const listmonkPassword = process.env.LISTMONK_PASSWORD;
const campaignId = process.env.PRESET_CAMPAIGN_ID;
const listmonkBaseUrl = process.env.LISTMONK_BASE_URL;
const listId = parseInt(process.env.LIST_ID, 10);
@ -20,21 +19,37 @@ export async function submitToListmonk(req, res) {
email,
name,
lists: [listId],
status: "enabled",
fields: { message }
};
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 {
// Add subscriber
const response = await fetch(listmonkUrl, {
const response = await fetchWithRetry(listmonkUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`
},
body: JSON.stringify(payload),
});
},12);
if (!response.ok) {
const errorBody = await response.text();
@ -44,63 +59,98 @@ export async function submitToListmonk(req, res) {
const result = await response.json();
console.log('Subscriber added successfully:', result);
// Update campaign status to "scheduled"
const campaignStatusUrl = `${listmonkBaseUrl}/campaigns/${campaignId}/status`;
const scheduleTime = new Date(Date.now() + 5 * 60 * 1000).toISOString(); // Schedule 5 minutes from now
const statusPayload = {
status: 'scheduled',
schedule: scheduleTime,
};
// Create a new campaign for the subscriber
const createCampaign = async (subject, body) => {
const createCampaignUrl = `${listmonkBaseUrl}/campaigns`;
const campaignPayload = {
name: `Welcome email for ${email}`,
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);
console.log('Campaign status payload:', statusPayload);
try {
const statusResponse = await fetch(campaignStatusUrl, {
method: 'PUT',
const response = await fetch(createCampaignUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`,
},
body: JSON.stringify(statusPayload),
body: JSON.stringify(campaignPayload),
});
if (!statusResponse.ok) {
const errorBody = await statusResponse.text();
console.error('Response from Listmonk:', errorBody);
throw new Error(`Listmonk API error (Update Campaign Status): ${statusResponse.statusText}. Response body: ${errorBody}`);
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`Listmonk API error (Create Campaign): ${response.statusText}. Response body: ${errorBody}`);
}
const statusResult = await statusResponse.json();
console.log('Campaign status updated successfully:', statusResult);
} catch (error) {
console.error('Error updating campaign status:', error);
}
const statusResult = await statusResponse.json();
console.log('Campaign status updated successfully:', statusResult);
const result = await response.json();
return result.data.id;
};
// Send the campaign
const campaignSendUrl = `${listmonkBaseUrl}/campaigns/${campaignId}/send`;
console.log('Sending campaign using URL:', campaignSendUrl);
// Campaign subject and body content
const subject = "👋 Welcome to the Progodyssey Family!";
const body = `
<h1>👋 Welcome to the Progodyssey Family, {{ .Subscriber.Name }}!</h1>
const campaignResponse = await fetch(campaignSendUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`,
},
<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 (!campaignResponse.ok) {
const errorBody = await campaignResponse.text();
throw new Error(`Listmonk API error (Send Campaign): ${campaignResponse.statusText}. Response body: ${errorBody}`);
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 campaignResult = await campaignResponse.json();
console.log('Campaign sent successfully:', campaignResult);
const dispatchResult = await dispatchResponse.json();
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) {
console.error('Error submitting to Listmonk:', error);
res.status(500).json({ message: 'Failed to submit to Listmonk', error: error.message });

5
testDns.js Normal file
View file

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