このガイドでは、通話ユーザーと指定された他のユーザーのリストを含むグループ チャットを見つける方法について説明します。Google Chat API では、グループ チャットは spaceType が GROUP_CHAT に設定されている Space リソースです。グループ チャットを見つけるには、Space リソースの findGroupChats(RPC、REST)メソッドを使用します。
前提条件
Node.js
- Google Chat へのアクセス権を持つ Business または Enterprise の Google Workspace アカウント。
- 環境を設定します。
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成する。
- Chat 用アプリの名前、アイコン、説明を使用して、Google Chat API を有効にして構成します。
- Node.js Cloud クライアント ライブラリをインストールします。
- 認可スコープを選択します。
Python
- Google Chat へのアクセス権を持つ Business または Enterprise の Google Workspace アカウント。
- 環境を設定します。
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成する。
- Chat 用アプリの名前、アイコン、説明を使用して、Google Chat API を有効にして構成します。
- Python Cloud クライアント ライブラリをインストールします。
- 認可スコープを選択します。
Java
- Google Chat へのアクセス権を持つ Business または Enterprise の Google Workspace アカウント。
- 環境を設定します。
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成する。
- Chat 用アプリの名前、アイコン、説明を使用して、Google Chat API を有効にして構成します。
- Java Cloud クライアント ライブラリをインストールします。
- 認可スコープを選択します。
Apps Script
- Google Chat へのアクセス権を持つ Business または Enterprise の Google Workspace アカウント。
- 環境を設定します。
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成する。
- Chat 用アプリの名前、アイコン、説明を使用して、Google Chat API を有効にして構成します。
- スタンドアロンの Apps Script プロジェクトを作成し、高度な Chat サービスを有効にします。
- 認可スコープを選択します。
グループ チャットを検索する
Google Chat でグループ チャットを見つけるには、リクエストで次の情報を渡します。
- 承認スコープ:
chat.memberships.readonlyまたはchat.memberships。 findGroupChats(RPC、REST)メソッドを呼び出し、他のユーザーのリソース名を渡します。
特定のメンバーを含むグループ チャットを見つける方法は次のとおりです。
Node.js
/**
* This sample shows how to find a group chat with specific members.
*
* It relies on the @google-apps/chat npm package.
*/
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
const {ChatServiceClient} = require('@google-apps/chat');
const {auth} = require('google-auth-library');
async function main() {
// Create a client
const chatClient = new ChatServiceClient({
authClient: await auth.getClient({
scopes: ['https://www.googleapis.com/auth/chat.memberships.readonly']
})
});
// The users to find a group chat with.
// Don't include the caller.
const users = [
'users/123456789',
'users/987654321'
];
// Create the request
const request = {
users: users
};
// Call the API
const response = await chatClient.findGroupChats(request);
// Handle the response
if (response.spaces && response.spaces.length > 0) {
console.log('Found group chat:', response.spaces[0].name);
} else {
console.log('No group chat found.');
}
}
main().catch(console.error);
Python
"""
This sample shows how to find a group chat with specific members.
"""
from google.apps import chat_v1
import google.auth
# Read the documentation for more details:
# https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
def find_group_chat():
# Create a client
scopes = ["https://www.googleapis.com/auth/chat.memberships.readonly"]
credentials, _ = google.auth.default(scopes=scopes)
client = chat_v1.ChatServiceClient(credentials=credentials)
# The users to find a group chat with.
# Don't include the caller.
users_list = [
"users/123456789",
"users/987654321"
]
# Create the request
request = chat_v1.FindGroupChatsRequest(
users=users_list
)
# Call the API
response = client.find_group_chats(request)
# Handle the response
if response.spaces:
print(f"Found group chat: {response.spaces[0].name}")
else:
print("No group chat found.")
if __name__ == "__main__":
find_group_chat()
Java
/**
* This sample shows how to find a group chat with specific members.
*/
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.ChatServiceSettings;
import com.google.chat.v1.FindGroupChatsRequest;
import com.google.chat.v1.FindGroupChatsResponse;
import java.util.Arrays;
import java.util.List;
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
public class FindGroupChat {
public static void main(String[] args) throws Exception {
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
.createScoped(Arrays.asList("https://www.googleapis.com/auth/chat.memberships.readonly"));
ChatServiceSettings settings = ChatServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
try (ChatServiceClient chatServiceClient = ChatServiceClient.create(settings)) {
List<String> users = Arrays.asList(
"users/123456789",
"users/987654321"
);
FindGroupChatsRequest request = FindGroupChatsRequest.newBuilder()
.addAllUsers(users)
.build();
FindGroupChatsResponse response = chatServiceClient.findGroupChats(request);
if (!response.getSpacesList().isEmpty()) {
System.out.printf("Found group chat: %s\n", response.getSpacesList().get(0).getName());
} else {
System.out.println("No group chat found.");
}
}
}
}
Apps Script
/**
* This sample shows how to find a group chat with specific members.
*/
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
function findGroupChat() {
// The users to find a group chat with.
// Don't include the caller.
const users = [
'users/123456789',
'users/987654321'
];
try {
// Call the API
// In Apps Script, query parameters are passed as optional arguments
const response = Chat.Spaces.findGroupChats({
users: users
});
if (response.spaces && response.spaces.length > 0) {
console.log('Found group chat: ' + response.spaces[0].name);
} else {
console.log('No group chat found.');
}
} catch (err) {
// Handle error
console.log('Failed to find group chat: ' + err.message);
}
}
このサンプルを実行するには、ユーザー リソース名を有効なユーザー ID に置き換えます。ユーザー ID は People API または Directory API から取得できます。
Chat API は、見つかったスペースのリストを含む FindGroupChatsResponse(RPC、REST)のインスタンスを返します。
詳細情報を含むグループ チャットを探す
デフォルトでは、findGroupChats(RPC、REST)は、name フィールドのみを含む Space オブジェクトを spaces/SPACE_NAME 形式で返します。displayName、spaceType、createTime などの Space の詳細を取得するには、spaceView パラメータを SPACE_VIEW_EXPANDED として指定します。
SPACE_VIEW_EXPANDED を使用するには、追加の認可スコープ(https://www.googleapis.com/auth/chat.spaces または https://www.googleapis.com/auth/chat.spaces.readonly)が必要です。
グループ チャットを見つけて詳細を取得する方法は次のとおりです。
Node.js
/**
* This sample shows how to find a group chat with specific members and return details.
*
* It relies on the @google-apps/chat npm package.
*/
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
const {ChatServiceClient} = require('@google-apps/chat');
const {auth} = require('google-auth-library');
async function main() {
// Create a client
const chatClient = new ChatServiceClient({
authClient: await auth.getClient({
scopes: [
'https://www.googleapis.com/auth/chat.spaces.readonly',
'https://www.googleapis.com/auth/chat.memberships.readonly'
]
})
});
// The users to find a group chat with.
// Don't include the caller.
const users = [
'users/123456789',
'users/987654321'
];
// Create the request
const request = {
users: users,
spaceView: 'SPACE_VIEW_EXPANDED'
};
// Call the API
const response = await chatClient.findGroupChats(request);
// Handle the response
if (response.spaces && response.spaces.length > 0) {
console.log('Found group chat:', response.spaces[0].displayName);
} else {
console.log('No group chat found.');
}
}
main().catch(console.error);
Python
"""
This sample shows how to find a group chat with specific members and return details.
"""
from google.apps import chat_v1
import google.auth
# Read the documentation for more details:
# https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
def find_group_chat_with_details():
# Create a client
scopes = [
"https://www.googleapis.com/auth/chat.memberships.readonly",
"https://www.googleapis.com/auth/chat.spaces.readonly"
]
credentials, _ = google.auth.default(scopes=scopes)
client = chat_v1.ChatServiceClient(credentials=credentials)
# The users to find a group chat with.
# Don't include the caller.
users_list = [
"users/123456789",
"users/987654321"
]
# Create the request
request = chat_v1.FindGroupChatsRequest(
users=users_list,
space_view=chat_v1.SpaceView.SPACE_VIEW_EXPANDED
)
# Call the API
response = client.find_group_chats(request)
# Handle the response
if response.spaces:
print(f"Found group chat: {response.spaces[0].display_name}")
else:
print("No group chat found.")
if __name__ == "__main__":
find_group_chat_with_details()
Java
/**
* This sample shows how to find a group chat with specific members and return details.
*/
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.ChatServiceSettings;
import com.google.chat.v1.FindGroupChatsRequest;
import com.google.chat.v1.FindGroupChatsResponse;
import com.google.chat.v1.SpaceView;
import java.util.Arrays;
import java.util.List;
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
public class FindGroupChatWithDetails {
public static void main(String[] args) throws Exception {
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
.createScoped(Arrays.asList(
"https://www.googleapis.com/auth/chat.memberships.readonly",
"https://www.googleapis.com/auth/chat.spaces.readonly"
));
ChatServiceSettings settings = ChatServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
try (ChatServiceClient chatServiceClient = ChatServiceClient.create(settings)) {
List<String> users = Arrays.asList(
"users/123456789",
"users/987654321"
);
FindGroupChatsRequest request = FindGroupChatsRequest.newBuilder()
.addAllUsers(users)
.setSpaceView(SpaceView.SPACE_VIEW_EXPANDED)
.build();
FindGroupChatsResponse response = chatServiceClient.findGroupChats(request);
if (!response.getSpacesList().isEmpty()) {
System.out.printf("Found group chat: %s\n", response.getSpacesList().get(0).getDisplayName());
} else {
System.out.println("No group chat found.");
}
}
}
}
Apps Script
/**
* This sample shows how to find a group chat with specific members and return details.
*/
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.FindGroupChats
function findGroupChatWithDetails() {
// The users to find a group chat with.
// Don't include the caller.
const users = [
'users/123456789',
'users/987654321'
];
try {
// Call the API
// In Apps Script, query parameters are passed as optional arguments
const response = Chat.Spaces.findGroupChats({
users: users,
spaceView: 'SPACE_VIEW_EXPANDED'
});
if (response.spaces && response.spaces.length > 0) {
console.log('Found group chat: ' + response.spaces[0].displayName);
} else {
console.log('No group chat found.');
}
} catch (err) {
// Handle error
console.log('Failed to find group chat: ' + err.message);
}
}
このサンプルを実行するには、ユーザー リソース名を有効なユーザー ID に置き換えます。ユーザー ID は People API または Directory API から取得できます。
Chat API は、追加の spaceView 詳細を含む、見つかったスペースのリストを含む FindGroupChatsResponse(RPC、REST)のインスタンスを返します。