腳本專案資訊清單中定義的每個會議解決方案都有相關聯的 onCreateFunction。每當使用者嘗試為活動選取該會議解決方案時,Google Workspace 外掛程式就會呼叫這個函式來建立會議。
實作外掛程式資訊清單中說明的每個 onCreateFunction。這些函式必須執行下列操作:
- 擷取第三方會議系統建立會議所需的任何 Google 日曆活動資訊,例如活動 ID 或與會者清單。
- 連線至第三方會議服務,然後使用日曆活動資訊建立新的會議。
- 如果會議建立要求失敗,請使用錯誤資訊建構並傳回包含
ConferenceError的ConferenceData物件。如否,請完成後續步驟。 - 初始化會議同步。
- 使用第三方會議服務傳回的資訊,建構並傳回新的
ConferenceData物件。
擷取活動資訊
如要建立第三方會議,必須提供相應的 Google 日曆活動資訊。不同第三方會議系統要求的確切活動資訊有所不同,但通常包括活動開始時間、結束時間、摘要、出席者清單和 ID。
呼叫時,您定義的每個 onCreateFunction 都會傳遞包含日曆和活動 ID 的引數。使用這些 ID,透過日曆進階服務擷取完整活動資訊。
Google 日曆可能會在活動建立前,就將會議詳細資料加入活動中。在這種情況下,日曆會傳遞有效的 eventId,但後續對 Calendar.Events.get 的呼叫可能會導致錯誤,指出活動不存在。onCreateFunction在這種情況下,請使用預留位置資料建立第三方會議,下次活動同步時,系統會替換這些資料。
建立第三方會議
onCreateFunction擷取必要的事件資料後,必須連線至第三方會議系統,建立會議。通常是透過第三方會議系統支援的 API 要求來完成。請參閱第三方會議解決方案的說明文件,瞭解可使用哪些 API 要求建立會議。
在 Google Apps Script 中,處理外部 API 要求的簡單方法是使用 Apps Script 適用的 OAuth2 或 Apps Script 適用的 OAuth1 開放原始碼程式庫。您也可以使用 UrlFetch 服務連線至外部 API,但這需要您明確處理授權詳細資料。
要求建立會議後,您可能需要提出其他要求,才能擷取新的會議詳細資料。
初始化會議同步
外掛程式在第三方系統上成功建立會議後,您應執行幾個步驟來啟用同步,這樣日曆活動的變更就會反映在會議中。
如要瞭解如何在建立會議後設定同步功能,請參閱「同步處理日曆變更」。
建構會議資料回應
onCreateFunction 必須使用第三方服務傳回的會議資訊,建構並傳回 ConferenceData 物件;「會議資料」一節說明這個物件的內容。Google 日曆會使用這項資訊,在會議開始時將使用者導向會議。
建構 ConferenceData 物件時,請注意欄位長度、進入點 URI 的格式,以及允許的進入點組合。舉例來說,單一 ConferenceData 最多只能有一個 VIDEO 進入點。這些限制與日曆 API 事件中相應 conferenceData 欄位的限制相同,但並非所有 API 事件欄位都適用於 Apps Script。
處理錯誤
建立會議時可能會發生錯誤。在某些情況下,第三方會議系統傳回錯誤,因此無法完成會議建立程序。在這些情況下,外掛程式應建構並傳回包含 ConferenceError 詳細資料的 ConferenceData 物件,藉此處理錯誤狀況,讓 Google 日曆採取適當行動。
建構 ConferenceData 物件來回報錯誤時,除了 ConferenceError 物件之外,不需要納入任何 ConferenceData 元件。ConferenceErrors 可以包含 ConferenceErrorType、錯誤訊息,以及供使用者登入第三方會議系統的網址 (適用於驗證問題)。
如果會議建立嘗試失敗,外掛程式就不需要嘗試設定會議同步。
範例
以下範例示範如何導入 onCreateFunction。函式名稱可以是任何名稱,您只需要在外掛程式專案資訊清單中定義即可。
create3rdPartyConference 函式會與第三方系統聯絡,以建立會議,而 getAuthenticationUrl 函式則會建立第三方系統驗證網址。這些功能並未在此完整實作。
這裡未顯示 initializeSyncing 函式,該函式會處理同步作業所需的前置作業。詳情請參閱「同步處理日曆變更」。
/**
* Creates a conference, then builds and returns a ConferenceData object
* with the corresponding conference information. This method is called
* when a user selects a conference solution defined by the add-on that
* uses this function as its 'onCreateFunction' in the add-on manifest.
*
* @param {Object} arg The default argument passed to a 'onCreateFunction';
* it carries information about the Google Calendar event.
* @return {ConferenceData}
*/
function createConference(arg) {
const eventData = arg.eventData;
const calendarId = eventData.calendarId;
const eventId = eventData.eventId;
// Retrieve the Calendar event information using the Calendar
// Advanced service.
var calendarEvent;
try {
calendarEvent = Calendar.Events.get(calendarId, eventId);
} catch (err) {
// The calendar event does not exist just yet; just proceed with the
// given event ID and allow the event details to sync later.
console.log(err);
calendarEvent = {
id: eventId,
};
}
// Create a conference on the third-party service and return the
// conference data or errors in a custom JSON object.
var conferenceInfo = create3rdPartyConference(calendarEvent);
// Build and return a ConferenceData object, either with conference or
// error information.
var dataBuilder = ConferenceDataService.newConferenceDataBuilder();
if (!conferenceInfo.error) {
// No error, so build the ConferenceData object from the
// returned conference info.
var phoneEntryPoint = ConferenceDataService.newEntryPoint()
.setEntryPointType(ConferenceDataService.EntryPointType.PHONE)
.setUri('tel:+' + conferenceInfo.phoneNumber)
.setPin(conferenceInfo.phonePin);
var adminEmailParameter = ConferenceDataService.newConferenceParameter()
.setKey('adminEmail')
.setValue(conferenceInfo.adminEmail);
dataBuilder.setConferenceId(conferenceInfo.id)
.addEntryPoint(phoneEntryPoint)
.addConferenceParameter(adminEmailParameter)
.setNotes(conferenceInfo.conferenceLegalNotice);
if (conferenceInfo.videoUri) {
var videoEntryPoint = ConferenceDataService.newEntryPoint()
.setEntryPointType(ConferenceDataService.EntryPointType.VIDEO)
.setUri(conferenceInfo.videoUri)
.setPasscode(conferenceInfo.videoPasscode);
dataBuilder.addEntryPoint(videoEntryPoint);
}
// Since the conference creation request succeeded, make sure that
// syncing has been enabled.
initializeSyncing(calendarId, eventId, conferenceInfo.id);
} else if (conferenceInfo.error === 'AUTH') {
// Authenentication error. Implement a function to build the correct
// authenication URL for the third-party conferencing system.
var authenticationUrl = getAuthenticationUrl();
var error = ConferenceDataService.newConferenceError()
.setConferenceErrorType(
ConferenceDataService.ConferenceErrorType.AUTHENTICATION)
.setAuthenticationUrl(authenticationUrl);
dataBuilder.setError(error);
} else {
// Other error type;
var error = ConferenceDataService.newConferenceError()
.setConferenceErrorType(
ConferenceDataService.ConferenceErrorType.TEMPORARY);
dataBuilder.setError(error);
}
// Don't forget to build the ConferenceData object.
return dataBuilder.build();
}
/**
* Contact the third-party conferencing system to create a conference there,
* using the provided calendar event information. Collects and retuns the
* conference data returned by the third-party system in a custom JSON object
* with the following fields:
*
* data.adminEmail - the conference administrator's email
* data.conferenceLegalNotice - the conference legal notice text
* data.error - Only present if there was an error during
* conference creation. Equal to 'AUTH' if the add-on user needs to
* authorize on the third-party system.
* data.id - the conference ID
* data.phoneNumber - the conference phone entry point phone number
* data.phonePin - the conference phone entry point PIN
* data.videoPasscode - the conference video entry point passcode
* data.videoUri - the conference video entry point URI
*
* The above fields are specific to this example; which conference information
* your add-on needs is dependent on the third-party conferencing system
* requirements.
*
* @param {Object} calendarEvent A Calendar Event resource object returned by
* the Google Calendar API.
* @return {Object}
*/
function create3rdPartyConference(calendarEvent) {
var data = {};
// Implementation details dependent on the third-party system API.
// Typically one or more API calls are made to create the conference and
// acquire its relevant data, which is then put in to the returned JSON
// object.
return data;
}
/**
* Return the URL used to authenticate the user with the third-party
* conferencing system.
*
* @return {String}
*/
function getAuthenticationUrl() {
var url;
// Implementation details dependent on the third-party system.
return url;
}