คู่มือนี้อธิบายวิธีค้นหากลุ่มแชทที่มีผู้ใช้ที่โทร
และรายชื่อผู้ใช้รายอื่นๆ ที่ระบุ ใน Google Chat API แชทกลุ่มคือ
Space แหล่งข้อมูลที่มีspaceTypeตั้งค่าเป็นGROUP_CHAT หากต้องการค้นหากลุ่มแชท ให้ใช้วิธี findGroupChats
(RPC,
REST) ใน
แหล่งข้อมูล Space
ข้อกำหนดเบื้องต้น
Node.js
- บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมของคุณ ดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API พร้อมชื่อ ไอคอน และคำอธิบายสำหรับแอป Chat
- ติดตั้ง Node.js Cloud Client Library
- เลือกขอบเขตการให้สิทธิ์
Python
- บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมของคุณ ดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API พร้อมชื่อ ไอคอน และคำอธิบายสำหรับแอป Chat
- ติดตั้ง Python Cloud Client Library
- เลือกขอบเขตการให้สิทธิ์
Java
- บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมของคุณ ดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API พร้อมชื่อ ไอคอน และคำอธิบายสำหรับแอป Chat
- ติดตั้ง Java Cloud Client Library
- เลือกขอบเขตการให้สิทธิ์
Apps Script
- บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมของคุณ ดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API พร้อมชื่อ ไอคอน และคำอธิบายสำหรับแอป Chat
- สร้างโปรเจ็กต์ 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);
}
}
หากต้องการเรียกใช้ตัวอย่างนี้ ให้แทนที่ชื่อทรัพยากรผู้ใช้ด้วยรหัสผู้ใช้ที่ถูกต้อง คุณขอรับรหัสผู้ใช้ได้จาก People API หรือ Directory API
Chat API จะแสดงอินสแตนซ์ของ
FindGroupChatsResponse (RPC, REST)
ซึ่งมีรายการพื้นที่ทำงานที่พบ
ค้นหาแชทกลุ่มพร้อมรายละเอียด
โดยค่าเริ่มต้น findGroupChats
(RPC,
REST) จะแสดงออบเจ็กต์ Space ที่มีเฉพาะฟิลด์ name ในรูปแบบ 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 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);
}
}
หากต้องการเรียกใช้ตัวอย่างนี้ ให้แทนที่ชื่อทรัพยากรผู้ใช้ด้วยรหัสผู้ใช้ที่ถูกต้อง คุณขอรับรหัสผู้ใช้ได้จาก People API หรือ Directory API
Chat API จะแสดงอินสแตนซ์ของ
FindGroupChatsResponse (RPC, REST)
ซึ่งมีรายการพื้นที่ทำงานที่พบ รวมถึงspaceViewรายละเอียดเพิ่มเติม
หัวข้อที่เกี่ยวข้อง
- สร้างพื้นที่ทำงาน
- ตั้งค่าพื้นที่ทำงาน
- แสดงพื้นที่ทำงาน
- อัปเดตพื้นที่ทำงาน
- ลบพื้นที่ทำงาน
- ค้นหาข้อความส่วนตัว