แสดงรายการพื้นที่ทำงาน

คู่มือนี้อธิบายวิธีใช้เมธอด list() ในทรัพยากร Space ของ Google Chat API เพื่อแสดงรายการพื้นที่ทำงาน การแสดงรายการพื้นที่ทำงานจะแสดงรายการพื้นที่ทำงานที่แบ่งหน้าและกรองได้

ทรัพยากร Spaceแสดงถึงพื้นที่ที่ผู้คนและแอป Chat สามารถส่งข้อความ แชร์ไฟล์ และทำงานร่วมกันได้ พื้นที่ทำงานมีหลายประเภท ดังนี้

  • ข้อความส่วนตัว (DM) คือการสนทนาระหว่างผู้ใช้ 2 คน หรือระหว่างผู้ใช้กับแอป Chat
  • การแชทเป็นกลุ่มคือการสนทนาระหว่างผู้ใช้ 3 คนขึ้นไปกับแอป Chat
  • พื้นที่ทำงานที่มีชื่อเป็นพื้นที่ถาวรที่ผู้คนส่งข้อความ แชร์ไฟล์ และทำงานร่วมกัน

การแสดงรายการพื้นที่ทำงานด้วย การตรวจสอบสิทธิ์แอป จะแสดงพื้นที่ทำงานที่แอป Chat มีสิทธิ์เข้าถึง การแสดงรายการพื้นที่ทำงานด้วยการตรวจสอบสิทธิ์ผู้ใช้จะแสดงพื้นที่ทำงานที่ผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์มีสิทธิ์เข้าถึง

ข้อกำหนดเบื้องต้น

Node.js

Python

Java

Apps Script

แสดงรายการพื้นที่ทำงานด้วยการตรวจสอบสิทธิ์ผู้ใช้

หากต้องการแสดงรายการพื้นที่ทำงานใน Google Chat ให้ส่งข้อมูลต่อไปนี้ในคำขอ

ตัวอย่างต่อไปนี้แสดงรายการพื้นที่ทำงานที่มีชื่อ (แต่ไม่แสดงแชทเป็นกลุ่มและข้อความส่วนตัวซึ่งถูกกรองออก) ที่ผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์มองเห็น

Node.js

chat/client-libraries/cloud/list-spaces-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

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

// This sample shows how to list spaces with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(
    USER_AUTH_OAUTH_SCOPES,
  );

  // Initialize request argument(s)
  const request = {
    // Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
    filter: 'space_type = "SPACE"',
  };

  // Make the request
  const pageResult = chatClient.listSpacesAsync(request);

  // Handle the response. Iterating over pageResult will yield results
  // and resolve additional pages automatically.
  for await (const response of pageResult) {
    console.log(response);
  }
}

await main();

Python

chat/client-libraries/cloud/list_spaces_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.readonly"]

# This sample shows how to list spaces with user credential
def list_spaces_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.ListSpacesRequest(
        # Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
        filter = 'space_type = "SPACE"',
        # Number of results that will be returned at once
        page_size = 100
    )

    # Make the request
    page_result = client.list_spaces(request)

    # Handle the response. Iterating over page_result will yield results and
    # resolve additional pages automatically.
    for response in page_result:
        print(response)

list_spaces_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/ListSpacesUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.ListSpacesRequest;
import com.google.chat.v1.ListSpacesResponse;
import com.google.chat.v1.Space;

// This sample shows how to list spaces with user credential.
public class ListSpacesUserCred{

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      ListSpacesRequest.Builder request = ListSpacesRequest.newBuilder()
        // Filter spaces by space type (SPACE or GROUP_CHAT or
        // DIRECT_MESSAGE).
        .setFilter("spaceType = \"SPACE\"")
        // Number of results that will be returned at once.
        .setPageSize(10);

      // Iterate over results and resolve additional pages automatically.
      for (Space response :
          chatServiceClient.listSpaces(request.build()).iterateAll()) {
        System.out.println(JsonFormat.printer().print(response));
      }
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to list spaces with user credential
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.readonly'
 * referenced in the manifest file (appsscript.json).
 */
function listSpacesUserCred() {
  // Initialize request argument(s)
  // Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
  const filter = 'space_type = "SPACE"';

  // Iterate through the response pages using page tokens
  let responsePage;
  let pageToken = null;
  do {
    // Request response pages
    responsePage = Chat.Spaces.list({
      filter: filter,
      pageSize: 10,
      pageToken: pageToken,
    });
    // Handle response pages
    if (responsePage.spaces) {
      for (const space of responsePage.spaces) {
        console.log(space);
      }
    }
    // Update the page token to the next one
    pageToken = responsePage.nextPageToken;
  } while (pageToken);
}

Chat API จะแสดงรายการพื้นที่ทำงานที่แบ่งหน้า

แสดงรายการพื้นที่ทำงานด้วยการตรวจสอบสิทธิ์แอป

หากต้องการแสดงรายการพื้นที่ทำงานใน Google Chat ให้ส่งข้อมูลต่อไปนี้ในคำขอ

ตัวอย่างต่อไปนี้แสดงรายการพื้นที่ทำงานที่มีชื่อ (แต่ไม่แสดงแชทเป็นกลุ่มและข้อความส่วนตัว) ที่แอป Chat มองเห็น

Node.js

chat/client-libraries/cloud/list-spaces-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to list spaces with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
    filter: 'space_type = "SPACE"',
  };

  // Make the request
  const pageResult = chatClient.listSpacesAsync(request);

  // Handle the response. Iterating over pageResult will yield results
  // and resolve additional pages automatically.
  for await (const response of pageResult) {
    console.log(response);
  }
}

await main();

Python

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

# This sample shows how to list spaces with app credential
def list_spaces_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.ListSpacesRequest(
        # Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
        filter = 'space_type = "SPACE"',
        # Number of results that will be returned at once
        page_size = 100
    )

    # Make the request
    page_result = client.list_spaces(request)

    # Handle the response. Iterating over page_result will yield results and
    # resolve additional pages automatically.
    for response in page_result:
        print(response)

list_spaces_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/ListSpacesAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.ListSpacesRequest;
import com.google.chat.v1.ListSpacesResponse;
import com.google.chat.v1.Space;

// This sample shows how to list spaces with app credential.
public class ListSpacesAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      ListSpacesRequest.Builder request = ListSpacesRequest.newBuilder()
        // Filter spaces by space type (SPACE or GROUP_CHAT or
        // DIRECT_MESSAGE).
        .setFilter("spaceType = \"SPACE\"")
        // Number of results that will be returned at once.
        .setPageSize(10);

      // Iterate over results and resolve additional pages automatically.
      for (Space response :
          chatServiceClient.listSpaces(request.build()).iterateAll()) {
        System.out.println(JsonFormat.printer().print(response));
      }
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to list spaces with app credential
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function listSpacesAppCred() {
  // Initialize request argument(s)
  // Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
  const filter = 'space_type = "SPACE"';

  // Iterate through the response pages using page tokens
  let responsePage;
  let pageToken = null;
  do {
    // Request response pages
    responsePage = Chat.Spaces.list(
      {
        filter: filter,
        pageSize: 10,
        pageToken: pageToken,
      },
      getHeaderWithAppCredentials(),
    );
    // Handle response pages
    if (responsePage.spaces) {
      for (const space of responsePage.spaces) {
        console.log(space);
      }
    }
    // Update the page token to the next one
    pageToken = responsePage.nextPageToken;
  } while (pageToken);
}

Chat API จะแสดงรายการพื้นที่ทำงานที่แบ่งหน้า

ปรับแต่งการแบ่งหน้าหรือกรองรายการ

หากต้องการแสดงรายการพื้นที่ทำงานใน Google Chat ให้ส่งพารามิเตอร์การค้นหาที่ไม่บังคับต่อไปนี้เพื่อปรับแต่งการแบ่งหน้าหรือกรองพื้นที่ทำงานที่แสดง

  • pageSize: จำนวนพื้นที่ทำงานสูงสุดที่จะแสดง บริการอาจแสดงจำนวนพื้นที่ทำงานน้อยกว่าค่านี้ หากไม่ได้ระบุ ระบบจะแสดงพื้นที่ทำงานไม่เกิน 100 รายการ ค่าสูงสุดคือ 1,000 โดยระบบจะเปลี่ยนค่าที่สูงกว่า 1,000 เป็น 1,000 โดยอัตโนมัติ
  • pageToken: โทเค็นหน้าเว็บที่ได้รับจากการเรียกใช้การแสดงรายการพื้นที่ทำงานครั้งก่อน ระบุโทเค็นนี้เพื่อดึงข้อมูลหน้าถัดไป เมื่อแบ่งหน้า ค่าตัวกรองควรตรงกับการเรียกใช้ที่ให้โทเค็นหน้าเว็บ การส่งค่าอื่นอาจทำให้เกิดผลลัพธ์ที่ไม่คาดคิด
  • filter: ตัวกรองการค้นหา ดูรายละเอียดการค้นหาที่รองรับได้ที่ ListSpacesRequest ข้อมูลอ้างอิง