I want to create a line notify which can send the event detail from my google calendar everyday.
I can get the title, description, location...etc, but I don't see the conference data in calendar API.
I use Google Apps Script to run the code.
Here is my code.
const Now = new Date();
const Start = new Date(new Date().setHours(0, 0, 0, 0));
const End = new Date(new Date().setHours(23, 59, 59, 999));
const calendarData = calendar.getEvents(Start, End);
function Notify() {
var NotifyContents = '';
var i = 1;
calendarData.forEach(item =>{
if (Now <= item.getStartTime()) {
NotifyContents = (item.getTitle() != "") ? ("\n" i ". " item.getTitle() "\n") : ("\n\nNo Title\n");
NotifyContents = (item.getDescription() != "") ? item.getDescription() "\n" : "";
NotifyContents = (item.getStartTime() != "" && item.getEndTime() != "") ? "Time:" item.getStartTime().toLocaleTimeString() "-" item.getEndTime().toLocaleTimeString() "\n": "";
NotifyContents = (item.getconferencedata() != "") ? ("\n" i ". " item.getconferencedata()) : ("No Conference\n");
i ;
}
}
)
if (typeof NotifyContents === 'string' && NotifyContents.length === 0) {
return;
}
NotifyTokens.forEach(function(value){
UrlFetchApp.fetch("https://notify-api.line.me/api/notify", {
"method" : "post",
"payload" : {"message" : NotifyContents},
"headers" : {"Authorization" : "Bearer " value}
});
});
}
Reference - Calendar API Link
CodePudding user response:
In order to retrieve the meet link from the event, it seems that in the current stage, Calendar API is required to be used. When this is reflected in your script, how about the following modification?
Modified script:
Before you use this script, please enable Calendar API at Advanced Google services.
From:
var NotifyContents = '';
var i = 1;
calendarData.forEach(item => {
if (Now <= item.getStartTime()) {
NotifyContents = (item.getTitle() != "") ? ("\n" i ". " item.getTitle() "\n") : ("\n\nNo Title\n");
NotifyContents = (item.getDescription() != "") ? item.getDescription() "\n" : "";
NotifyContents = (item.getStartTime() != "" && item.getEndTime() != "") ? "Time:" item.getStartTime().toLocaleTimeString() "-" item.getEndTime().toLocaleTimeString() "\n" : "";
NotifyContents = (item.getconferencedata() != "") ? ("\n" i ". " item.getconferencedata()) : ("No Conference\n");
i ;
}
}
)
To:
const eventList = Calendar.Events.list(calendar.getId(), { timeMin: Start.toISOString(), timeMax: End.toISOString(), maxResults: 2500 }).items.reduce((o, e) => (o[e.id] = e.conferenceData.entryPoints.map(({ uri }) => uri).join(","), o), {});
var NotifyContents = '';
var i = 1;
calendarData.forEach(item => {
if (Now <= item.getStartTime()) {
NotifyContents = (item.getTitle() != "") ? ("\n" i ". " item.getTitle() "\n") : ("\n\nNo Title\n");
NotifyContents = (item.getDescription() != "") ? item.getDescription() "\n" : "";
NotifyContents = (item.getStartTime() != "" && item.getEndTime() != "") ? "Time:" item.getStartTime().toLocaleTimeString() "-" item.getEndTime().toLocaleTimeString() "\n" : "";
var eventId = item.getId().split("@")[0];
NotifyContents = eventList[eventId] != "" ? ("\n" i ". " eventList[eventId]) : ("No Conference\n");
i ;
}
});
- In this modification, it supposes that
calendar
has already been declaread. Please be careful about this.