function sendNotificationToNtfy(message) { var ntfyTopic = "iss_scheduler"; // Replace with your ntfy.sh topic var url = "https://ntfy.sh/" + ntfyTopic; var options = { 'method': 'post', 'payload': message }; try { UrlFetchApp.fetch(url, options); Logger.log("Notification sent to ntfy.sh"); } catch (error) { Logger.log("Error sending notification to ntfy.sh: " + error); } } function createCalendarEntry() { var labelName = "SpotTheStation"; // Replace with your label name var recipients = ["email1@gmail.com", "email2@gmail.com", "email3@gmail.com", "email4@gmail.com"]; // Replace with the email addresses of the recipients var label = GmailApp.getUserLabelByName(labelName); var threads = label.getThreads(); var monthMap = { 'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12' }; for (var i = 0; i < threads.length; i++) { var messages = threads[i].getMessages(); for (var j = 0; j < messages.length; j++) { var message = messages[j]; var subject = message.getSubject(); var body = message.getPlainBody(); // Parse date, month, day, and start time from the body var dateTimePattern = /Time: (\w{3}) (\w{3}) (\d{2}) ([0-9: ]+) ([AP]M)/; var dateTimeMatch = dateTimePattern.exec(body); if (dateTimeMatch) { var monthAbbreviation = dateTimeMatch[2]; var month = monthMap[monthAbbreviation]; var date = dateTimeMatch[3]; var startTimeString = dateTimeMatch[4]; var meridianAMPM = dateTimeMatch[5]; // Convert start time to 24-hour format var startTime = convertTo24HourFormat(month, date, startTimeString, meridianAMPM); // Calculate end time by adding 15 minutes to the start time var endTime = new Date(startTime.getTime() + 15 * 60000); // 15 minutes in milliseconds // Check if an event with the same subject exists var events = CalendarApp.getDefaultCalendar().getEvents(startTime, endTime); var eventExists = events.some(function(event) { return event.getTitle() === subject; }); if (!eventExists) { // Remove date and time portion from the body var bodyWithoutDateTime = body.replace(dateTimeMatch[0], ''); // Remove comma and space before "Visible" in the note var description = bodyWithoutDateTime.replace(/, Visible/g, 'Visible'); // Create calendar event var calendar = CalendarApp.getDefaultCalendar(); var event = calendar.createEvent(subject, startTime, endTime); event.setDescription(description); event.addEmailReminder(30); // Add all recipients as guests recipients.forEach(function(recipient) { event.addGuest(recipient); }); Logger.log("Created event: " + event.getTitle()); Logger.log("Month: " + month + "\nDay: " + date + "\nStart Time: " + startTimeString + "\nStart String: " + startTime + "\nEnd String: " + endTime + "\nAM or PM: " + meridianAMPM); // Send notification to ntfy.sh var notificationMessage = "New ISS sighting scheduled: " + subject + " on " + startTime.toLocaleString(); sendNotificationToNtfy(notificationMessage); } else { Logger.log("Skipping calendar entry. Event with the same subject already exists: " + subject); } } else { Logger.log("Failed to parse date, month, day, and start time from email: " + subject); } } } } function convertTo24HourFormat(month, date, timeString, meridian) { var [hours, minutes] = timeString.split(":"); hours = parseInt(hours, 10); minutes = parseInt(minutes, 10); if (meridian === "PM" && hours !== 12) { hours += 12; } else if (meridian === "AM" && hours === 12) { hours = 0; } var startTime = new Date(); startTime.setFullYear(new Date().getFullYear()); startTime.setMonth(parseInt(month) - 1); startTime.setDate(parseInt(date)); startTime.setHours(hours); startTime.setMinutes(minutes); startTime.setSeconds(0); startTime.setMilliseconds(0); return startTime; }