Preview
Open Original
Initialize the directory for the Lambda function
To begin, create and initialize the directory for your Lambda function:
mkdir titan-schools-lambda
cd titan-schools-lambda
npm init -y
npm install axios
Code for index.js
This is the main code for your index.js file:
The MMM used Axios, so i went with that, there may be an easier way.
Copy code
const axios = require('axios');
exports.handler = async (event) => {
const buildingId = '<your buildingID>';
const districtId = '<your districtID>';
const formatDate = (dateObject) => {
return `${dateObject.getMonth() + 1}-${dateObject.getDate()}-${dateObject.getFullYear()}`;
};
const today = new Date();
const startDate = formatDate(today);
const params = {
buildingId,
districtId,
startDate,
};
try {
const response = ...
Initialize the directory for the Lambda function
To begin, create and initialize the directory for your Lambda function:
mkdir titan-schools-lambda
cd titan-schools-lambda
npm init -y
npm install axios
Code for index.js
This is the main code for your index.js file:
The MMM used Axios, so i went with that, there may be an easier way.
Copy code
const axios = require('axios');
exports.handler = async (event) => {
const buildingId = '<your buildingID>';
const districtId = '<your districtID>';
const formatDate = (dateObject) => {
return `${dateObject.getMonth() + 1}-${dateObject.getDate()}-${dateObject.getFullYear()}`;
};
const today = new Date();
const startDate = formatDate(today);
const params = {
buildingId,
districtId,
startDate,
};
try {
const response = await axios.get('https://api.linqconnect.com/api/FamilyMenu', { params });
const menus = extractMenusByDate(response.data);
const htmlResponse = generateHTMLResponse(menus);
return {
statusCode: 200,
headers: { 'Content-Type': 'text/html' },
body: htmlResponse,
};
} catch (error) {
console.error(error);
return {
statusCode: 500,
body: 'Error fetching the menu data',
};
}
};
const extractMenusByDate = (apiResponse) => {
if (!apiResponse.FamilyMenuSessions) {
return '<p>No menu data available.</p>';
}
const menus = apiResponse.FamilyMenuSessions
.filter(session => session.ServingSession === "Lunch") // Filter for Lunch
.map(session => {
return session.MenuPlans[0].Days.map(day => {
const date = day.Date;
const dayOfWeek = new Date(date).toLocaleString('en-US', { weekday: 'long' });
// Filter for meals with CategoryName "Main Event"
const meals = day.MenuMeals.map(meal => {
return meal.RecipeCategories
.filter(category => category.CategoryName === "Main Event") // Filter for "Main Event"
.map(category => {
return category.Recipes.map(recipe => `- ${recipe.RecipeName}`).join('<br>');
}).join('<br>');
}).join('<br>');
return `<h3>${date} (${dayOfWeek})</h3><p>${meals}</p>`;
}).join('');
}).join('');
return menus;
};
const generateHTMLResponse = (menus) => {
return `
<!DOCTYPE html>
<html>
<head>
<title>TitanSchools Menu</title>
<style>
body {
font-size: 18px;
color: white;
}
h3 {
font-size: 24px;
color: white;
}
p {
font-size: 18px;
color: white;
}
ul {
font-size: 18px;
color: white;
}
</style>
<title>TitanSchools Menu</title>
</head>
<body>
<h1>Today's Menu</h1>
${menus}
</body>
</html>
`;
};
On windows, it didnt seem to like when i compressed the folder, so i had to do this in powershell then upload that as the lambda:
Compress-Archive -Path .\* -DestinationPath titan-schools-lambda.zip
API Gateway
https://console.aws.amazon.com/apigateway/ Walk through the steps for http and it lets you attach to a lambda. you may need to enable CORS