[英]How do I invite someone to a meeting using google apps calendar when I am not subscribed to it
[英]How to find a time for a meeting with several participants using Google Calendar API and Apps Script?
我正在使用 Dialogflow API、Apps 腳本、日歷 API 進行聊天機器人項目。 聊天機器人應該能夠直接從聊天中組織兩個或更多參與者之間的會議。 例如,用戶說“明天下午 5 點與 john.smith@mail.com 組織一次會議”,聊天機器人應該 go 到我和約翰的日歷,檢查此時間范圍的可用性並預訂會議。 到目前為止,一切都很好。 我已經有了這個階段的解決方案(檢查下面的代碼片段)。 我的問題是,如果用戶在這個時間范圍內很忙,我怎樣才能獲得所有參與者都空閑的建議時間。 我正在使用空閑/忙碌呼叫,但它僅在用戶忙碌時返回。
//the function search in spreadshet containing all timezones and selects the corresponding offset
function setCorrectTimeZone(rawTime,rawDate){
var spreadSheet = SpreadsheetApp.openById("10tJCi5bRHs3Cl8Gvw8NGMeHhfRBDnvPD338peH2MWyg"); //the timezones sheed ID stored in CEE Google Assistant shared drive
var sheet = spreadSheet.getSheetByName("timezones");
var timezoneRange = sheet.getRange(2, 1, 513, 2).getValues();
//getting the user's timezone
var userTimeZone = CalendarApp.getDefaultCalendar().getTimeZone();
Logger.log("User time zone: " + userTimeZone);
var userTimeZoneOffset = "";
var correctDateTimeAndZone = "";
//iterating over the timezones from the sheet and comparing with user's to find the correct offset
for(var i = 0; i<timezoneRange.length; i++){
if(timezoneRange[i][1] == userTimeZone){
userTimeZoneOffset = timezoneRange[i][0];
}
}
//taking the date only
var date = rawDate.split('T')[0];
//taking the time only
var timeNoTimeZone = rawTime.split('+')[0].split('T')[1];
//concatenating the date, time and the correct timezone together
correctDateTimeAndZone = date + 'T' + timeNoTimeZone + userTimeZoneOffset;
return correctDateTimeAndZone;
}
function organizeMeeting(dialogflowRawResponse, email) {
var guestEmail = dialogflowRawResponse.queryResult.parameters.email; //the list of all guests
var rawDate = dialogflowRawResponse.queryResult.parameters.date;
var rawTime = dialogflowRawResponse.queryResult.parameters.time;
var eventTitle = dialogflowRawResponse.queryResult.parameters.meetingName;
var hasAllParams = dialogflowRawResponse.queryResult.hasOwnProperty('allRequiredParamsPresent'); //checker for all parameters
var correctedTimezone = setCorrectTimeZone(rawTime,rawDate);
Logger.log("Has all required parameters? " + hasAllParams);
//check if all parameters are passed
while(hasAllParams == false){
Logger.log("Parameters are missing");
Logger.log(dialogflowRawResponse.queryResult.fulfillmentText);
return { text: dialogflowRawResponse.queryResult.fulfillmentText };
}
Logger.log("Guests email list detected: " + JSON.stringify(guestEmail) + "\nDate-time detected: " + rawTime + "\nCorrect date-time timezone: " + correctedTimezone +"\nTitle detected: " + eventTitle);
//setting the date-time for the start and the end of the event
var dateTimeStart = new Date(correctedTimezone);
var dateTimeEnd = new Date(correctedTimezone);
dateTimeEnd.setHours(dateTimeEnd.getHours() + 1);
dateTimeStart = dateTimeStart.toISOString();
dateTimeEnd = dateTimeEnd.toISOString();
Logger.log("ISO dateTimeStart: " + dateTimeStart);
Logger.log("ISO dateTimeEnd: " + dateTimeEnd);
var participants = [{"id": email}]; //array of objects. Each object is a particpant for the event
for(var i = 0; i < guestEmail.length; i++){
participants.push({"id": guestEmail[i]}); //filling the participant array
}
//preparing the body for the Calendar API free-busy request
var requestBody = {
"timeMin": dateTimeStart,
"timeMax": dateTimeEnd,
"items": participants
}
//Calendar freebusy request to check if the slot is available for all particiaptns
var response = Calendar.Freebusy.query(requestBody);
for(var i = 0; i < participants.length; i++){
var calendarId = participants[i].id;
if(response.calendars[calendarId]['busy'].length != 0){
Logger.log(calendarId + " is busy at this time");
return { text: calendarId + " is busy at this time" };
break;
}
}
//guest array of objects for each participant
var guestsArr = [{"email":email}];
for(var i = 0; i < guestEmail.length; i++){
guestsArr.push({"email": guestEmail[i]});
}
//preparing the event details for the Calendar API call
var event = {
"summary": eventTitle,
"end": {
"dateTime": dateTimeEnd
},
"start": {
"dateTime": dateTimeStart
},
"attendees": guestsArr
}
//preapring the event options for the Calendar API call
var eventOptions = {
"sendNotifications": true,
"sendUpdates": "all"
}
//Calendar API call
var calendarEventRequest = Calendar.Events.insert(event, "primary",eventOptions);
//logs the Calendar API response to the logs
Logger.log(JSON.stringify(calendarEventRequest));
return { text: "Done! Check you calendar." };
}
上面的代碼從 Dialogflow API 中獲取參數 - 日期、時間、會議標題和參與者,並使用此信息進行忙/閑通話,然后最終調用日歷 API。 它還使用電子表格數據庫根據用戶位置查找正確的用戶時區。
如果有人已經完成了此類功能以獲得可用的時間段,我們將不勝感激。
如果每個人都有空,您可以檢查每個一小時的時間段,如果他們都有空,然后在日歷中發送邀請。
示例代碼:
var dateTimeStart = new Date(correctedTimezone);
var dateTimeEnd = new Date(correctedTimezone);
do {
dateTimeEnd.setHours(dateTimeStart.getHours() + 1);
dateTimeStart = dateTimeStart.toISOString();
dateTimeEnd = dateTimeEnd.toISOString();
Logger.log("ISO dateTimeStart: " + dateTimeStart);
Logger.log("ISO dateTimeEnd: " + dateTimeEnd);
var participants = [{"id": email}]; //array of objects. Each object is a particpant for the event
for(var i = 0; i < guestEmail.length; i++){
participants.push({"id": guestEmail[i]}); //filling the participant array
}
//preparing the body for the Calendar API free-busy request
var requestBody = {
"timeMin": dateTimeStart,
"timeMax": dateTimeEnd,
"items": participants
}
//Calendar freebusy request to check if the slot is available for all particiaptns
var response = Calendar.Freebusy.query(requestBody);
for(var i = 0; i < participants.length; i++){
var calendarId = participants[i].id;
if(response.calendars[calendarId]['busy'].length != 0){
dateTimeStart.setHours(dateTimeStart.getHours() + 1);
Logger.log(calendarId + " is busy at this time");
//return { text: calendarId + " is busy at this time" };
break;
}
}
}
while (response.calendars[calendarId]['busy'].length != 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.