-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (75 loc) · 3.34 KB
/
index.js
File metadata and controls
87 lines (75 loc) · 3.34 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
const axios = require('axios');
const querystring = require('querystring');
exports.handler = async (event) => {
try {
console.log("Received event:", JSON.stringify(event));
let body;
// Check if the body is base64 encoded
if (event.isBase64Encoded) {
console.log("Decoding base64 encoded body");
const decodedBody = Buffer.from(event.body, 'base64').toString('utf-8');
body = querystring.parse(decodedBody); // Parse as URL-encoded data
} else {
// If not base64 encoded, directly parse the URL-encoded body
body = querystring.parse(event.body);
}
console.log("Parsed body:", JSON.stringify(body));
// Prepare the message content based on available fields
let embed = {
title: body.product_name ? `Product: ${body.product_name}` : "New Sale Notification",
description: `A new sale has been made via Gumroad.`,
fields: [],
color: 5814783 // Color of the embed
};
// Add fields dynamically based on the available data
for (const [key, value] of Object.entries(body)) {
if (value) {
// Format the key to be more readable
let formattedKey = key.replace(/_/g, ' ').replace(/\b\w/g, char => char.toUpperCase());
// Handle nested fields like card[expiry_year]
formattedKey = formattedKey.replace(/\[(.*?)\]/g, ' $1');
embed.fields.push({ name: formattedKey, value: String(value), inline: true });
}
}
console.log("Embed:", JSON.stringify(embed));
// Discord API setup
const discordApiUrl = `https://discord.com/api/v10/channels/${process.env.DISCORD_CHANNEL_ID}/messages`; // Updated with the correct channel ID
// Function to send a message to Discord
const sendToDiscord = async (messageBody) => {
const response = await axios.post(discordApiUrl, messageBody, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bot ${process.env.DISCORD_BOT_TOKEN}`
}
});
if (response.status !== 200) {
console.error('Error sending message to Discord:', response.statusText);
}
};
// Split fields into chunks of 25 and send each chunk as a separate message
const chunkSize = 25;
for (let i = 0; i < embed.fields.length; i += chunkSize) {
const chunk = embed.fields.slice(i, i + chunkSize);
const messageBody = {
embeds: [{ ...embed, fields: chunk }]
};
if (i === 0) {
messageBody.content = "**New Gumroad Ping Received!**";
} else {
messageBody.embeds[0].title = "Additional Fields";
messageBody.embeds[0].description = "Here are some additional fields from the sale.";
}
await sendToDiscord(messageBody);
}
return {
statusCode: 200,
body: JSON.stringify({ message: 'Success' })
};
} catch (error) {
console.error('Error:', error);
return {
statusCode: 500,
body: JSON.stringify({ message: 'Internal Server Error' })
};
}
};