本指南介绍了如何查找包含调用用户和指定其他用户列表的群组聊天。在 Google Chat API 中,群聊是 Space 资源,其 spaceType 设置为 GROUP_CHAT。如需查找群聊,请对 Space 资源使用 findGroupChats (RPC、REST) 方法。
前提条件
Node.js
- 拥有可访问 Google Chat 的 Google Workspace 商务版或企业版账号。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为您的 Chat 应用指定名称、图标和说明。
- 安装 Node.js Cloud 客户端库。
- 选择授权范围。
Python
- 拥有可访问 Google Chat 的 Google Workspace 商务版或企业版账号。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为您的 Chat 应用指定名称、图标和说明。
- 安装 Python Cloud 客户端库。
- 选择授权范围。
Java
- 拥有可访问 Google Chat 的 Google Workspace 商务版或企业版账号。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为您的 Chat 应用指定名称、图标和说明。
- 安装 Java Cloud 客户端库。
- 选择授权范围。
Apps 脚本
- 拥有可访问 Google Chat 的 Google Workspace 商务版或企业版账号。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为您的 Chat 应用指定名称、图标和说明。
- 创建独立的 Apps 脚本项目,然后启用高级聊天服务。
- 选择授权范围。
查找群聊
如需在 Google Chat 中查找群聊,请在请求中传递以下内容:
如需查找包含特定成员的群聊,请执行以下操作:
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 user = [
'users/123456789',
'users/987654321'
];
// Create the request
const request = {
user: user
};
// 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.
user_list = [
"users/123456789",
"users/987654321"
]
# Create the request
request = chat_v1.FindGroupChatsRequest(
user=user_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()
.addAllUser(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 脚本
/**
* 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({
user: 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。 您可以从 People API 或 Directory API 获取用户 ID。
Chat API 会返回 FindGroupChatsResponse 的实例(RPC、REST),其中包含找到的会议室列表。
查找包含详细信息的群聊
默认情况下,findGroupChats(RPC、REST)返回仅包含 name 字段的 Space 对象,格式为 spaces/SPACE_NAME。如需获取有关空间的更多详细信息,例如 displayName、spaceType 或 createTime,请将 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 user = [
'users/123456789',
'users/987654321'
];
// Create the request
const request = {
user: user,
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.
user_list = [
"users/123456789",
"users/987654321"
]
# Create the request
request = chat_v1.FindGroupChatsRequest(
user=user_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()
.addAllUser(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 脚本
/**
* 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({
user: 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。 您可以从 People API 或 Directory API 获取用户 ID。
Chat API 会返回 FindGroupChatsResponse 的实例(RPC、REST),其中包含找到的会议室列表以及其他 spaceView 详细信息。