Skip to content

Commit

Permalink
Added chat.js, removed key from static site
Browse files Browse the repository at this point in the history
  • Loading branch information
RBvonRB committed Dec 3, 2024
1 parent 818c3df commit 361e744
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
32 changes: 32 additions & 0 deletions mooncal/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
async function getChatGPTResponse(prompt) {
const url = `https://mooncalendar.glitch.me/api/chatgpt`;

try {
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prompt })
});

if (!response.ok) {
throw new Error(`Error: ${response.statusText}`);
}

const data = await response.json();
console.log("ChatGPT response:", data.response);

return data.response; // Return the chatbot's response
} catch (error) {
console.error("Failed to get response from ChatGPT:", error);
}

return null; // Default to null if fetching fails
}

// Example Usage
getChatGPTResponse("Tell me a joke about programming.").then(response => {
if (response) {
console.log("ChatGPT says:", response);
}
});

1 change: 1 addition & 0 deletions mooncal/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ <h2 id="moonPhaseText">Laden...</h2>

<script src="stars.js"></script>
<script src="moondata.js"></script>
<script src="chat.js"></script>
</body>

</html>
5 changes: 1 addition & 4 deletions mooncal/moondata.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ async function fetchMoonPhase(location, date) {
return null;
}



function getMoonPhasePercentage(normalizedPhase) {
return Math.round(normalizedPhase * 100); // Convert to percentage and round off
}
Expand All @@ -91,10 +89,9 @@ async function initialize() {
const selectedDate = parseDate(queryDate);
const dateForApi = formatDateForAPI(selectedDate);
const location = getQueryParam('location') || 'Zurich, Switzerland'; // Default location
const apiKey = 'ABCDEFGHIJK'; // Replace with your actual API key

// Fetch the moon phase from the API
const apiMoonPhase = await fetchMoonPhase(location, dateForApi, apiKey);
const apiMoonPhase = await fetchMoonPhase(location, dateForApi);
if (apiMoonPhase !== null) {
const moonPhaseName = getMoonPhaseName(apiMoonPhase);
const moonPhaseImage = getMoonPhaseImage(apiMoonPhase);
Expand Down

0 comments on commit 361e744

Please sign in to comment.