Created
January 9, 2024 07:42
-
-
Save tripolskypetr/f556702f46931240cd2f5b091bb25f59 to your computer and use it in GitHub Desktop.
Google Calendar Integration https://stateful.com/blog/events-in-the-google-calendar-API
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
const { google } = require("googleapis"); | |
module.exports = async ({ req, res, log }) => { | |
const CREDENTIALS = JSON.parse(process.env.CALENDAR_CREDENTIALS); | |
const SCOPES = "https://www.googleapis.com/auth/calendar"; | |
const CALENDAR_ID = process.env.CALENDAR_ID; | |
const CALENDAR_TIMEOFFSET = process.env.CALENDAR_TIMEOFFSET; | |
const CALENDAR_TIMEZONE = process.env.CALENDAR_TIMEZONE; | |
const { | |
summary = "This is summary", | |
description = "This is the description", | |
unixtime = Date.now(), | |
calendarId = CALENDAR_ID, | |
} = JSON.parse(req.body); | |
const calendar = google.calendar({ version: "v3" }); | |
const auth = new google.auth.JWT( | |
CREDENTIALS.client_email, | |
null, | |
CREDENTIALS.private_key, | |
SCOPES | |
); | |
const dateTimeForCalander = (unixtime) => { | |
const date = new Date(unixtime); | |
const year = date.getFullYear(); | |
let month = date.getMonth() + 1; | |
if (month < 10) { | |
month = `0${month}`; | |
} | |
let day = date.getDate(); | |
if (day < 10) { | |
day = `0${day}`; | |
} | |
let hour = date.getHours(); | |
if (hour < 10) { | |
hour = `0${hour}`; | |
} | |
let minute = date.getMinutes(); | |
if (minute < 10) { | |
minute = `0${minute}`; | |
} | |
const newDateTime = `${year}-${month}-${day}T${hour}:${minute}:00.000${CALENDAR_TIMEOFFSET}`; | |
const event = new Date(Date.parse(newDateTime)); | |
const startDate = event; | |
const endDate = new Date( | |
new Date(startDate).setHours(startDate.getHours() + 1) | |
); | |
return { | |
start: startDate, | |
end: endDate, | |
}; | |
}; | |
const insertEvent = async (event) => { | |
const response = await calendar.events.insert({ | |
auth: auth, | |
calendarId, | |
resource: event, | |
}); | |
if (response["status"] == 200 && response["statusText"] === "OK") { | |
return "Event Inserted sucessfully"; | |
} else { | |
return "Failed to insert event"; | |
} | |
}; | |
const dateTime = dateTimeForCalander(unixtime); | |
const event = { | |
summary, | |
description, | |
start: { | |
dateTime: dateTime["start"], | |
timeZone: CALENDAR_TIMEZONE, | |
}, | |
end: { | |
dateTime: dateTime["end"], | |
timeZone: CALENDAR_TIMEZONE, | |
}, | |
}; | |
let result = ""; | |
try { | |
log("Insert event begin"); | |
result = await insertEvent(event); | |
log(`Insert event end RESULT=${result}`); | |
} catch (error) { | |
error(`Insert event error ERROR=${String(error)}`); | |
throw error; | |
} | |
return res.send( | |
JSON.stringify({ | |
response: result, | |
error: false, | |
}) | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment