Google Chat スペースを作成してメンバーを追加する

このガイドでは、Google Chat API の Space リソースで setUp() メソッドを使用して、Chat スペースを作成し、メンバーを追加する方法について説明します。

Space リソースは、ユーザーと Chat 用アプリがメッセージの送信、ファイルの共有、共同作業を行える場所を表します。スペースにはいくつかの種類があります。

  • ダイレクト メッセージ(DM)は、2 人のユーザー間、またはユーザーと Chat 用アプリ間の会話です。
  • グループ チャットは、3 人以上のユーザーと Chat 用アプリ間の会話です。
  • 名前付きスペースは、ユーザーがメッセージの送信、ファイルの共有、共同作業を行うための永続的な場所です。

setUp() メソッドを使用すると、次のことができます。

  • 初期メンバーを含む名前付きスペースを作成します。
  • 2 人のユーザー間でダイレクト メッセージ(DM)を作成します。
  • 複数のユーザー間でグループ メッセージを設定します。

スペースを設定する際は、次の点を考慮してください。

  • 呼び出し(認証済み)ユーザーはスペースに自動的に追加されるため、リクエストでユーザーのメンバーシップを指定する必要はありません。
  • ダイレクト メッセージ(DM)を作成するときに、2 人のユーザー間に DM が存在する場合は、その DM が返されます。それ以外の場合は、DM が作成されます。
  • グループ チャットの作成時に、リクエストで指定されたメンバーシップがグループ チャットに正常に追加されなかった場合(権限の問題など)、空のグループ チャット(呼び出しユーザーのみを含む)が作成されることがあります。
  • スレッド形式の返信を使用するスペースを設定したり、Google Workspace 組織外のユーザーを追加したりすることはできません。
  • リクエストで指定された重複するメンバーシップ(呼び出しユーザーを含む)は、リクエスト エラーが発生するのではなく、フィルタリングされます。
  • Google Workspace 管理者が Google Workspace 組織全体に Chat 用アプリをインストールすると、Google Chat はインストールされた Chat 用アプリと組織内の各ユーザーとの間に DM を作成するため、プログラムで DM を設定する必要はありません。代わりに、スペースを一覧表示してすべての DM を返すか、ダイレクト メッセージを検索して特定の DM の詳細を取得します。

前提条件

Node.js

Python

Java

Apps Script

スペースを設定する

スペースを設定するには、リクエストで次の情報を渡します。

  • chat.spaces.create または chat.spaces 認証スコープを指定します。
  • SetUpSpace() メソッドを呼び出します。
  • spaceSpace のインスタンスとして、displayNamespaceType などの必要なフィールドをすべて含めて渡します。
  • membershipsMembership インスタンスの配列として渡します。インスタンスごとに、次の操作を行います。
    • users/{user} を指定して、スペース メンバーとしてユーザーを追加します。ここで、{user} は People API の person{person_id}、または Directory API の user の ID です。たとえば、People API の人物 resourceNamepeople/123456789 の場合、member.name として users/123456789 を含むメンバーシップを含めることで、ユーザーをスペースに追加できます。
    • groups/{group} を指定して、グループをスペースのメンバーとして追加します。ここで、{group} はメンバーシップを作成するグループの ID です。グループの ID は、Cloud Identity API を使用して取得できます。たとえば、Cloud Identity API が名前 groups/123456789 のグループを返す場合、membership.groupMember.namegroups/123456789 に設定します。Google グループは、グループ チャットや DM に追加することはできません。名前付きスペースにのみ追加できます。

通話中のユーザーと別のユーザーとの間に DM を作成するには、リクエストでユーザーのメンバーシップを指定します。

通話中のユーザーと通話中のアプリの間に DM を作成するには、space.singleUserBotDmtrue に設定し、メンバーシップを指定しないでください。このメソッドを使用して、通話アプリとの DM を設定することしかできません。通話アプリをスペースのメンバーとして追加したり、2 人のユーザー間の既存の DM に追加したりするには、メンバーシップを作成するをご覧ください。

次の例では、名前付きスペースを作成し、2 人のユーザー(認証済みユーザーと別のユーザー)のスペース メンバーシップを 1 つ作成します。

Node.js

chat/client-libraries/cloud/set-up-space-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.spaces.create'];

// This sample shows how to set up a named space with one initial member
// with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    space: {
      spaceType: 'SPACE',
      // Replace DISPLAY_NAME here.
      displayName: 'DISPLAY_NAME'
    },
    memberships: [{
      member: {
        // Replace USER_NAME here.
        name: 'users/USER_NAME',
        type: 'HUMAN'
      }
    }]
  };

  // Make the request
  const response = await chatClient.setUpSpace(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/set_up_space_user_cred.py
from authentication_utils import create_client_with_user_credentials
from google.apps import chat_v1 as google_chat

SCOPES = ["https://www.googleapis.com/auth/chat.spaces.create"]

def set_up_space_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.SetUpSpaceRequest(
        space = {
            "space_type": 'SPACE',
            # Replace DISPLAY_NAME here.
            "display_name": 'DISPLAY_NAME'
        },
        memberships = [{
            "member": {
                # Replace USER_NAME here.
                "name": 'users/USER_NAME',
                "type_": 'HUMAN'
            }
        }]
    )

    # Make the request
    response = client.set_up_space(request)

    # Handle the response
    print(response)

set_up_space_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/SetUpSpaceUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.Membership;
import com.google.chat.v1.SetUpSpaceRequest;
import com.google.chat.v1.Space;
import com.google.chat.v1.User;

// This sample shows how to set up a named space with one initial member with
// user credential.
public class SetUpSpaceUserCred {

  private static final String SCOPE =
    "https://www.googleapis.com/auth/chat.spaces.create";

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      SetUpSpaceRequest.Builder request = SetUpSpaceRequest.newBuilder()
        .setSpace(Space.newBuilder()
          .setSpaceType(Space.SpaceType.SPACE)
          // Replace DISPLAY_NAME here.
          .setDisplayName("DISPLAY_NAME"))
        .addAllMemberships(ImmutableList.of(Membership.newBuilder()
          .setMember(User.newBuilder()
            // Replace USER_NAME here.
            .setName("users/USER_NAME")
            .setType(User.Type.HUMAN)).build()));
      Space response = chatServiceClient.setUpSpace(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to set up a named space with one initial member with
 * user credential.
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.create'
 * referenced in the manifest file (appsscript.json).
 */
function setUpSpaceUserCred() {
  // Initialize request argument(s)
  const space = {
    spaceType: 'SPACE',
    // TODO(developer): Replace DISPLAY_NAME here
    displayName: 'DISPLAY_NAME'
  };
  const memberships = [{
    member: {
      // TODO(developer): Replace USER_NAME here
      name: 'users/USER_NAME',
      // User type for the membership
      type: 'HUMAN'
    }
  }];

  // Make the request
  const response = Chat.Spaces.setup({ space: space, memberships: memberships });

  // Handle the response
  console.log(response);
}

サンプルを実行するには、次の値を置き換えます。

  • DISPLAY_NAME: 新しいスペースの表示名。
  • USER_NAME: メンバーシップに含める他のユーザーの ID。

スペースに移動するには、スペースのリソース ID を使用してスペースの URL を作成します。リソース ID は、Google Chat レスポンス本文のスペース name から取得できます。たとえば、スペースの namespaces/1234567 の場合、次の URL を使用してスペースに移動できます: https://mail.google.com/chat/u/0/#chat/space/1234567