Created
July 14, 2023 15:24
-
-
Save AntonFriberg/698cda177f9009561a55c53d87d58519 to your computer and use it in GitHub Desktop.
Create Event from Booking Email using Google App Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What to add to the parsed ISO date to get it in correct timezone | |
TZ_POSTFIX = 'T00:00:00+02:00'; | |
// Email subjects to look at | |
SUBJECT_NEW_BOOKING = 'New Booking - Some Service'; | |
// Matches '2023-07-14 00:00 - 2023-07-14 00:00' like strings with groups for each date | |
DATE_RANGE_PATTERN = new RegExp('(\\d{4}-\\d{2}-\\d{2}) 00:00 - (\\d{4}-\\d{2}-\\d{2}) 00:00'); | |
// Regex patterns to extract other content such as name, apartment, email | |
NAME_PATTERN = new RegExp('<td>Name:<\\/td>[\\n\\s]+<td>(.*)<\\/td>'); | |
APARTMENT_PATTERN = new RegExp('<td>Apartment:<\\/td>[\\n\\s]+<td>(.*)<\\/td>'); | |
EMAIL_PATTERN = new RegExp('<td>Email:<\\/td>[\\n\\s]+<td>(.*)<\\/td>'); | |
function createEventFromNewBookings() { | |
matches = findUnreadBookings(SUBJECT_NEW_BOOKING) | |
for (var i = 0; i < matches.length; i++) { | |
var body = matches[i]; | |
// Extract date from email body | |
var [from_date, to_date] = extractDate(body); | |
// Extract other useful content from email body | |
var [name, apartment, email] = extractContent(body); | |
// Create Calendar event | |
createCalendarEvent(name, apartment, email, from_date, to_date); | |
} | |
} | |
function findUnreadBookings(subject) { | |
// Extract HTML body from email for unread matches | |
var threads = GmailApp.search('subject:"' + subject + '"'); | |
var matches = []; | |
for (var i = 0; i < threads.length; i++) { | |
var thread = threads[i]; | |
if (thread.isUnread()) { | |
var emailMessage = thread.getMessages()[0].getBody(); | |
matches.push(emailMessage); | |
thread.markRead(); | |
} | |
} | |
return matches; | |
} | |
function extractDate(body) { | |
// Extract dates from HTML Body string | |
var match = DATE_RANGE_PATTERN.exec(body) | |
var from_date = new Date(match[1] + TZ_POSTFIX); | |
var to_date = new Date(match[2] + TZ_POSTFIX); | |
Logger.log('From date: ' + from_date); | |
Logger.log('To date: ' + to_date); | |
// It turns out that Smart Brf sends one email per date | |
// so the from and to dates will always be the same. | |
// We only return one of them. | |
return [from_date, to_date]; | |
} | |
function extractContent(body) { | |
// Extract name, apartment, email from HTML Body string | |
var name = NAME_PATTERN.exec(body)[1]; | |
var apartment = APARTMENT_PATTERN.exec(body)[1]; | |
var email = EMAIL_PATTERN.exec(body)[1]; | |
Logger.log('Name: ' + name); | |
Logger.log('Apartment: ' + apartment); | |
Logger.log('Email: ' + email); | |
return [name, apartment, email]; | |
} | |
function createCalendarEvent(name, apartment, email, from_date, to_date) { | |
// Create calendar event from the given information | |
var title = apartment + ' ' + name; | |
var desc = 'Namn: ' + name + '\n' + 'Lägenhet: ' + apartment + '\n' + 'Email: ' + email + '\n' | |
if to_date <= from_date { | |
// Create for a single day | |
var event = CalendarApp.getDefaultCalendar().createAllDayEvent( | |
title, | |
from_date, | |
{description: desc} | |
); | |
} else { | |
// Create for multiple days | |
var event = CalendarApp.getDefaultCalendar().createAllDayEvent( | |
title, | |
from_date, | |
to_date, | |
{description: desc} | |
); | |
Logger.log('Event ID: ' + event.getId()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment