ユーザー、Google グループ、Google Chat アプリをスペースに招待または追加する

このガイドでは、membership リソースで create メソッドを使用する方法について説明します ユーザー、Google グループ、または Google Meet ビデオ会議の Chat アプリからスペースへの できます。メンバーシップの作成時に、指定したメンバーが 自動承諾ポリシーがオフになっていると、そのユーザーは招待され、スペースへの参加を承諾する必要があります 招待を承諾する必要があります。それ以外の場合、メンバーシップを作成すると、そのメンバーは 指定したスペースに直接移動できます。

Membership リソース 人間のユーザーまたは Google Chat アプリのどちらが招待されているかを表します。 その場から離れることがなくなります。

前提条件

Python

  • Python 3.6 以降
  • pip パッケージ管理ツール
  • 最新の Python 用 Google クライアント ライブラリ。インストールまたは更新する手順は次のとおりです。 コマンドライン インターフェースで次のコマンドを実行します。

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • Google Chat API が有効で構成されている Google Cloud プロジェクト。手順については、以下をご覧ください。 Google Chat アプリを作成します
  • Chat 用アプリに認可が構成されました。作成中 メンバーには ユーザー認証 認可スコープは chat.memberships または chat.memberships.app です。

Node.js

  • Node.js とnpm
  • 最新の Node.js 用 Google クライアント ライブラリ。これらをインストールするには、次のコマンドを実行します。 コマンドライン インターフェースで次のコマンドを実行します。

    npm install @google-cloud/local-auth @googleapis/chat
    
  • Google Chat API が有効で構成されている Google Cloud プロジェクト。手順については、以下をご覧ください。 Google Chat アプリを作成します
  • Chat 用アプリに認可が構成されました。作成中 メンバーには ユーザー認証 認可スコープは chat.memberships または chat.memberships.app です。

スペースにユーザーを招待または追加する

スペースにユーザーを招待または追加するには、 request:

  • chat.memberships 認可スコープを指定します。
  • 呼び出し create メソッド 日付 membership リソース
  • parent を、メンバーシップを作成するスペースのリソース名に設定します。
  • memberusers/{user}{user} は共有先のユーザー)に設定します。 次のどちらかです:
    • 関数の ID People API です。たとえば、People API が resourceNamepeople/123456789 の場合は、membership.member.name を設定します。 users/123456789 まで。
    • 関数の ID ユーザー ディレクトリ API で管理できます。
    • ユーザーのメールアドレス。たとえば、users/222larabrown@gmail.com のようにします。 users/larabrown@cymbalgroup.com。お客様が Google アカウントまたは 別の Google Workspace 組織に所属している場合は、その組織の 入力します。

次の例では、スペースにユーザーを追加します。

Python

  1. 作業ディレクトリに、先ほど作成した chat_membership_user_create.py
  2. chat_membership_user_create.py に次のコードを含めます。

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.memberships"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then adds a user to a Chat space by creating a membership.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                    'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().members().create(
    
            # The space in which to create a membership.
            parent = 'spaces/SPACE',
    
            # Specify which user the membership is for.
            body = {
              'member': {
                'name':'users/USER',
                'type': 'HUMAN'
              }
            }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードの次のように置き換えます。

    • SPACE: スペース名。 こちらの spaces.list メソッド スペースの URL から取得できます。

    • USER: ユーザー ID。

  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_membership_user_create.py
    

Node.js

  1. 作業ディレクトリに、add-user-to-space.js という名前のファイルを作成します。
  2. add-user-to-space.js に次のコードを含めます。

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Adds the user to the Chat space.
    * @return {!Promise<!Object>}
    */
    async function addUserToSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.members.create({
        parent: 'spaces/SPACE',
        requestBody: {member: {name: 'users/USER', type: 'HUMAN'}}
      });
    }
    
    addUserToSpace().then(console.log);
    
  3. コードの次のように置き換えます。

    • SPACE: スペース名。 spaces.list メソッド スペースの URL から取得できます。

    • USER: ユーザー ID。

  4. 作業ディレクトリでサンプルを実行します。

    node add-user-to-space.js
    

Chat API は、メッセージに対して membership 作成されたユーザー メンバーシップの詳細が記載されます。

Google グループをスペースに招待または追加する

スペースに Google グループを招待または追加するには、 request:

  • chat.memberships 認可スコープを指定します。
  • 呼び出し create メソッド 日付 membership リソース
  • parent を、メンバーシップを作成するスペースのリソース名に設定します。
  • groupMembergroups/{group} に設定します。{group} は、設定するグループ ID です。 選択します。グループの ID は、 Cloud Identity API。 たとえば、Cloud Identity API groups/123456789 という名前のグループを返してから、 membership.groupMember.name から groups/123456789 に変更。

Google グループは、グループ チャットやダイレクト メッセージには追加できません。 管理することもできます。次の例では、名前付きスペースにグループを追加します。

Python

  1. 作業ディレクトリに、先ほど作成した chat_membership_group_create.py
  2. chat_membership_group_create.py に次のコードを含めます。

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.memberships"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then adds a group to a Chat space by creating a membership.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                    'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().members().create(
    
            # The named space in which to create a membership.
            parent = 'spaces/SPACE',
    
            # Specify which group the membership is for.
            body = {
              'groupMember': {
                'name':'groups/GROUP',
              }
            }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードの次のように置き換えます。

    • SPACE: スペース名。 こちらの spaces.list メソッド スペースの URL から取得できます。

    • GROUP: グループ ID。

  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_membership_group_create.py
    

Node.js

  1. 作業ディレクトリに、add-group-to-space.js という名前のファイルを作成します。
  2. add-group-to-space.js に次のコードを含めます。

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Adds the group to the Chat space.
    * @return {!Promise<!Object>}
    */
    async function addUserToSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.members.create({
        parent: 'spaces/SPACE',
        requestBody: {groupMember: {name: 'groups/GROUP'}}
      });
    }
    
    addUserToSpace().then(console.log);
    
  3. コードの次のように置き換えます。

    • SPACE: スペース名。 spaces.list メソッド スペースの URL から取得できます。

    • GROUP: グループ ID。

  4. 作業ディレクトリでサンプルを実行します。

    node add-group-to-space.js
    

Chat API は、メッセージに対して membership 作成されたグループ メンバーシップの詳細が記載されます。

スペースに Chat 用アプリを追加する

Chat 用アプリで、別のアプリをメンバーとしてスペースに追加することはできません。 選択します。スペースに Chat 用アプリを追加するには 2 人のユーザー間のダイレクト メッセージの場合は、リクエストに次の内容を渡します。

  • chat.memberships.app 認可スコープを指定します。
  • 呼び出し create メソッド membership リソースに対する権限。
  • parent を、メンバーシップを作成するスペースのリソース名に設定します。
  • memberusers/app に設定します。呼び出すアプリを表すエイリアス Chat API。

次の例では、スペースに Chat 用アプリを追加します。

Python

  1. 作業ディレクトリに、先ほど作成した chat_membership_app_create.py
  2. chat_membership_app_create.py に次のコードを含めます。

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.memberships.app"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then adds the Chat app to a Chat space.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                    'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().members().create(
    
            # The space in which to create a membership.
            parent = 'spaces/SPACE',
    
            # Set the Chat app as the entity that gets added to the space.
            # 'app' is an alias for the Chat app calling the API.
            body = {
                'member': {
                  'name':'users/app',
                  'type': 'BOT'
                }
            }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードで SPACE をスペース名に置き換えます。スペースには こちらの spaces.list メソッド スペースの URL から取得できます。

  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_membership_app_create.py
    

Node.js

  1. 作業ディレクトリに、add-app-to-space.js という名前のファイルを作成します。
  2. add-app-to-space.js に次のコードを含めます。

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Adds the app to the Chat space.
    * @return {!Promise<!Object>}
    */
    async function addAppToSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships.app',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.members.create({
        parent: 'spaces/SPACE',
        requestBody: {member: {name: 'users/app', type: 'BOT'}}
      });
    }
    
    addAppToSpace().then(console.log);
    
  3. コードで SPACE をスペース名に置き換えます。スペースには こちらの spaces.list メソッド スペースの URL から取得できます。

  4. 作業ディレクトリでサンプルを実行します。

    node add-app-to-space.js
    

Chat API は、メッセージに対して membership 作成されたアプリ メンバーシップの詳細を確認できます。