const { SESClient, SendEmailCommand } = require("@aws-sdk/client-ses");
let fetch;
(async () => {
fetch = (await import("node-fetch")).default;
})();
console.log("AWS REGION: ${process.env.MY_AWS_REGION}");
console.log("AWS ACCESS KEY ID: ${process.env.MY_AWS_ACCESS_KEY_ID ? "SET" : "NOT SET"}");
console.log("EMAIL USER: ${process.env.EMAIL_USER}");
console.log("TELEGRAM BOT TOKEN SET: ${process.env.TELEGRAM_BOT_TOKEN ? "YES" : "NO"}");
console.log("TELEGRAM USER SET: ${process.env.TELEGRAM_USER ? "YES" : "NO"}");
const sesClient = new SESClient({
region: process.env.MY_AWS_REGION,
credentials: {
accessKeyId: process.env.MY_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.MY_AWS_SECRET_ACCESS_KEY,
},
});
exports.handler = async function (event, context) {
console.log("Lambda function triggered");
if (!event.body) {
console.error("Request body is missing");
return {
statusCode: 400,
body: JSON.stringify({ message: "Missing request body" }),
};
}
let body;
// Support for both JSON and URL-encoded data
const contentType =
event.headers["content-type"] || event.headers["Content-Type"] || "";
if (contentType.includes("application/x-www-form-urlencoded")) {
const qs = require("querystring");
body = qs.parse(event.body);
} else {
try {
body = JSON.parse(event.body);
} catch (error) {
console.error("Invalid JSON input:", error);
return {
statusCode: 400,
body: JSON.stringify({ message: "Invalid JSON input" }),
};
}
}
const { name, sendSwitcher, message, contactInfo } = body;
console.log("Received request with sendSwitcher: ${sendSwitcher}");
if (sendSwitcher === "telegram") {
const telegramMessage = `Name: ${name}
Telegram: ${contactInfo}
Message: ${message}`;
const telegramURL = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`;
console.log("Telegram Message: ${telegramMessage}");
console.log("Sending request to Telegram API: ${telegramURL}");
try {
const response = await fetch(telegramURL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: process.env.TELEGRAM_USER,
text: telegramMessage,
}),
});
const data = await response.json();
console.log("Telegram API Response: ${JSON.stringify(data)}");
if (!response.ok) {
throw new Error(`Telegram API Error: ${JSON.stringify(data)}`);
}
return {
statusCode: 200,
body: JSON.stringify({
message: "Request sent via Telegram",
data,
}),
};
} catch (error) {
console.error("Telegram API error: ${error.message}");
return {
statusCode: 500,
body: JSON.stringify({
message: "Sending error via Telegram",
error: error.message,
}),
};
}
} else if (sendSwitcher === "email") {
console.log("Preparing to send email...");
const emailParams = {
Destination: { ToAddresses: [process.env.EMAIL_USER] },
Message: {
Body: {
Text: {
Data: `Name: ${name}
Email: ${contactInfo}
Message: ${message}`,
},
},
Subject: { Data: "Form Request" },
},
Source: process.env.EMAIL_USER,
};
try {
const data = await sesClient.send(
new SendEmailCommand(emailParams)
);
console.log("Email sent successfully: ${JSON.stringify(data)}");
return {
statusCode: 200,
body: JSON.stringify({
message: "Request sent via email",
data,
}),
};
} catch (error) {
console.error("Email send error: ${error.message}");
return {
statusCode: 500,
body: JSON.stringify({
message: "Error sending via email",
error: error.message,
}),
};
}
}
return {
statusCode: 400,
body: JSON.stringify({ message: "Invalid sendSwitcher" }),
};
};
const { SESClient, SendEmailCommand } = require("@aws-sdk/client-ses");
let fetch;
(async () => {
fetch = (await import("node-fetch")).default;
})();
console.log("AWS REGION: ${process.env.MY_AWS_REGION}");
console.log("AWS ACCESS KEY ID: ${process.env.MY_AWS_ACCESS_KEY_ID ? "SET" : "NOT SET"}");
console.log("EMAIL USER: ${process.env.EMAIL_USER}");
console.log("TELEGRAM BOT TOKEN SET: ${process.env.TELEGRAM_BOT_TOKEN ? "YES" : "NO"}");
console.log("TELEGRAM USER SET: ${process.env.TELEGRAM_USER ? "YES" : "NO"}");
const sesClient = new SESClient({
region: process.env.MY_AWS_REGION,
credentials: {
accessKeyId: process.env.MY_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.MY_AWS_SECRET_ACCESS_KEY,
},
});
exports.handler = async function (event, context) {
console.log("Lambda function triggered");
if (!event.body) {
console.error("Request body is missing");
return {
statusCode: 400,
body: JSON.stringify({ message: "Missing request body" }),
};
}
let body;
// Support for both JSON and URL-encoded data
const contentType =
event.headers["content-type"] || event.headers["Content-Type"] || "";
if (contentType.includes("application/x-www-form-urlencoded")) {
const qs = require("querystring");
body = qs.parse(event.body);
} else {
try {
body = JSON.parse(event.body);
} catch (error) {
console.error("Invalid JSON input:", error);
return {
statusCode: 400,
body: JSON.stringify({ message: "Invalid JSON input" }),
};
}
}
const { name, sendSwitcher, message, contactInfo } = body;
console.log("Received request with sendSwitcher: ${sendSwitcher}");
if (sendSwitcher === "telegram") {
const telegramMessage = `Name: ${name}
Telegram: ${contactInfo}
Message: ${message}`;
const telegramURL = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`;
console.log("Telegram Message: ${telegramMessage}");
console.log("Sending request to Telegram API: ${telegramURL}");
try {
const response = await fetch(telegramURL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: process.env.TELEGRAM_USER,
text: telegramMessage,
}),
});
const data = await response.json();
console.log("Telegram API Response: ${JSON.stringify(data)}");
if (!response.ok) {
throw new Error(`Telegram API Error: ${JSON.stringify(data)}`);
}
return {
statusCode: 200,
body: JSON.stringify({
message: "Request sent via Telegram",
data,
}),
};
} catch (error) {
console.error("Telegram API error: ${error.message}");
return {
statusCode: 500,
body: JSON.stringify({
message: "Sending error via Telegram",
error: error.message,
}),
};
}
} else if (sendSwitcher === "email") {
console.log("Preparing to send email...");
const emailParams = {
Destination: { ToAddresses: [process.env.EMAIL_USER] },
Message: {
Body: {
Text: {
Data: `Name: ${name}
Email: ${contactInfo}
Message: ${message}`,
},
},
Subject: { Data: "Form Request" },
},
Source: process.env.EMAIL_USER,
};
try {
const data = await sesClient.send(
new SendEmailCommand(emailParams)
);
console.log("Email sent successfully: ${JSON.stringify(data)}");
return {
statusCode: 200,
body: JSON.stringify({
message: "Request sent via email",
data,
}),
};
} catch (error) {
console.error("Email send error: ${error.message}");
return {
statusCode: 500,
body: JSON.stringify({
message: "Error sending via email",
error: error.message,
}),
};
}
}
return {
statusCode: 400,
body: JSON.stringify({ message: "Invalid sendSwitcher" }),
};
};
const { SESClient, SendEmailCommand } = require("@aws-sdk/client-ses");
let fetch;
(async () => {
fetch = (await import("node-fetch")).default;
})();
console.log("AWS REGION: ${process.env.MY_AWS_REGION}");
console.log("AWS ACCESS KEY ID: ${process.env.MY_AWS_ACCESS_KEY_ID ? "SET" : "NOT SET"}");
console.log("EMAIL USER: ${process.env.EMAIL_USER}");
console.log("TELEGRAM BOT TOKEN SET: ${process.env.TELEGRAM_BOT_TOKEN ? "YES" : "NO"}");
console.log("TELEGRAM USER SET: ${process.env.TELEGRAM_USER ? "YES" : "NO"}");
const sesClient = new SESClient({
region: process.env.MY_AWS_REGION,
credentials: {
accessKeyId: process.env.MY_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.MY_AWS_SECRET_ACCESS_KEY,
},
});
exports.handler = async function (event, context) {
console.log("Lambda function triggered");
if (!event.body) {
console.error("Request body is missing");
return {
statusCode: 400,
body: JSON.stringify({ message: "Missing request body" }),
};
}
let body;
// Support for both JSON and URL-encoded data
const contentType =
event.headers["content-type"] || event.headers["Content-Type"] || "";
if (contentType.includes("application/x-www-form-urlencoded")) {
const qs = require("querystring");
body = qs.parse(event.body);
} else {
try {
body = JSON.parse(event.body);
} catch (error) {
console.error("Invalid JSON input:", error);
return {
statusCode: 400,
body: JSON.stringify({ message: "Invalid JSON input" }),
};
}
}
const { name, sendSwitcher, message, contactInfo } = body;
console.log("Received request with sendSwitcher: ${sendSwitcher}");
if (sendSwitcher === "telegram") {
const telegramMessage = `Name: ${name}
Telegram: ${contactInfo}
Message: ${message}`;
const telegramURL = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`;
console.log("Telegram Message: ${telegramMessage}");
console.log("Sending request to Telegram API: ${telegramURL}");
try {
const response = await fetch(telegramURL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: process.env.TELEGRAM_USER,
text: telegramMessage,
}),
});
const data = await response.json();
console.log("Telegram API Response: ${JSON.stringify(data)}");
if (!response.ok) {
throw new Error(`Telegram API Error: ${JSON.stringify(data)}`);
}
return {
statusCode: 200,
body: JSON.stringify({
message: "Request sent via Telegram",
data,
}),
};
} catch (error) {
console.error("Telegram API error: ${error.message}");
return {
statusCode: 500,
body: JSON.stringify({
message: "Sending error via Telegram",
error: error.message,
}),
};
}
} else if (sendSwitcher === "email") {
console.log("Preparing to send email...");
const emailParams = {
Destination: { ToAddresses: [process.env.EMAIL_USER] },
Message: {
Body: {
Text: {
Data: `Name: ${name}
Email: ${contactInfo}
Message: ${message}`,
},
},
Subject: { Data: "Form Request" },
},
Source: process.env.EMAIL_USER,
};
try {
const data = await sesClient.send(
new SendEmailCommand(emailParams)
);
console.log("Email sent successfully: ${JSON.stringify(data)}");
return {
statusCode: 200,
body: JSON.stringify({
message: "Request sent via email",
data,
}),
};
} catch (error) {
console.error("Email send error: ${error.message}");
return {
statusCode: 500,
body: JSON.stringify({
message: "Error sending via email",
error: error.message,
}),
};
}
}
return {
statusCode: 400,
body: JSON.stringify({ message: "Invalid sendSwitcher" }),
};
};