サードパーティの会議を作成する

スクリプト プロジェクトのマニフェストで定義した各会議ソリューションには、onCreateFunction が関連付けられています。アドオンは、ユーザーが会議ソリューションを選択しようとするたびに、この関数を呼び出して会議を作成します。

アドオン マニフェストに記述されている各 onCreateFunction を実装する必要があります。一般に、これらの関数は次の処理を行う必要があります。

  1. サードパーティの会議システムで会議の作成に必要となる可能性がある、Google カレンダーの予定情報(イベント ID や参加者のリストなど)を取得する。
  2. サードパーティの会議サービスに接続し、Google カレンダーの予定情報を使用して新しい会議を作成します。
  3. なんらかの理由で会議作成リクエストが失敗した場合は、エラー情報を使用して ConferenceError を含む ConferenceData オブジェクトをビルドして返します。それ以外の場合は、次の手順を完了します。
    1. 会議の同期を初期化します。
    2. サードパーティの会議サービスから返された情報を使用して、新しい ConferenceData オブジェクトをビルドして返します。

イベント情報の取得

サードパーティの会議を作成するには、対応する Google カレンダーの予定に関する特定の情報が必要です。必要となる正確なイベント情報は、サードパーティの会議システムによって異なりますが、多くの場合、イベントの開始時間、終了時間、概要、参加者リスト、ID などです。

定義した各 onCreateFunction が呼び出されると、カレンダーとイベント ID を含む引数が渡されます。これらの ID を使用して、Google カレンダーの高度なサービスで完全なイベント情報を取得できます。

Google カレンダーでは、会議の詳細を事前に予定に追加しておくことができます。このような場合、Google カレンダーは onCreateFunction に有効な eventId を渡しますが、その後に Calendar.Events.get() を呼び出すと、予定が存在しないというエラー レスポンスが返されることがあります。このような場合は、プレースホルダ データを使用してサードパーティの会議を作成することをおすすめします。このデータは、次回のイベントが同期されたときに置き換えられます。

サードパーティの会議を作成する

onCreateFunction は、必要なイベントデータを取得したら、サードパーティの会議システムに接続して会議を作成する必要があります。通常、これを行うには、サードパーティの会議システムでサポートされている API リクエストを行います。サードパーティの会議ソリューションのドキュメントを参照して、会議の作成に使用できる API リクエストを確認してください。

Apps Script で外部 API リクエストを行う最も簡単な方法は、OAuth2 for Apps Script または OAuth1 for Apps Script オープンソース ライブラリを使用することです。UrlFetch サービスを使用して外部 API に接続することもできますが、その場合は認証の詳細を明示的に処理する必要があります。

会議の作成をリクエストした後、新しい会議の詳細を取得するために追加のリクエストが必要になることがあります。

会議の同期を初期化する

アドオンがサードパーティ システム上で会議を正常に作成したら、いくつかの手順を踏んで同期を有効にして、Google カレンダーの予定への変更が会議に反映されるようにする必要があります。

会議作成後に同期を設定する方法の詳細については、カレンダーの変更の同期をご覧ください。

会議データ応答の作成

onCreateFunction は、サードパーティ サービスから返された会議情報を使用して、ConferenceData オブジェクトをビルドして返す必要があります。このオブジェクトの内容は、会議データ セクションに記述されます。Google カレンダーは、この情報に基づいて会議が開始されると、その会議にユーザーを誘導します。

ConferenceData オブジェクトを作成する場合は、フィールド長、エントリ ポイント URI の形式、エントリ ポイントの許可される組み合わせに制限があることに注意してください。たとえば、1 つの ConferenceData に含めることができる VIDEO エントリ ポイントは 1 つだけです。これらの制限は、Calendar API イベントで説明されている、対応する conferenceData フィールドの制限と同じですが、ここに記載されているすべての API イベント フィールドが Apps Script で利用できるわけではありません。

エラー処理

サードパーティの会議システムから返されるエラーにより、会議の作成を完了できないことがあります。このような場合、Google カレンダーが対応できるように、アドオンは ConferenceError の詳細を含む ConferenceData オブジェクトを作成して返すことにより、エラー状態を確実に処理する必要があります。

ConferenceData オブジェクトを作成してエラーを報告する場合、ConferenceError オブジェクト以外の ConferenceData コンポーネントを含める必要はありません。ConferenceErrors には ConferenceErrorType とエラー メッセージを含めることができます。認証の場合は、ユーザーがサードパーティの会議システムにログインできる URL が発行されます。

以下に onCreateFunction の例を示します(関数の名前には任意の名前を付けることができます。アドオン プロジェクトのマニフェストで定義するだけで済みます)。

関数 create3rdPartyConference() がサードパーティ システムにアクセスして会議を作成し、getAuthenticationUrl() 関数がサードパーティのシステム認証 URL を作成します。これらは、サードパーティのシステムの詳細に依存するため、ここでは完全には実装されていません。

ここでは関数 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;
}