サードパーティ会議の作成

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

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

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

イベント情報を取得する

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

この関数が呼び出されると、定義した各 onCreateFunction に、カレンダー ID と予定の 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 つまでです。この制限は、対応する conferenceData フィールドの Calendar API イベントで説明されている制限と同じですが、ここで説明するすべての 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;
}