อัปเดตพื้นที่ทํางาน

คู่มือนี้อธิบายวิธีใช้ patch() เมธอดในทรัพยากร Space ของ Google Chat API เพื่ออัปเดตพื้นที่ทำงาน อัปเดตพื้นที่ทำงานเพื่อเปลี่ยนแอตทริบิวต์เกี่ยวกับพื้นที่ทำงาน เช่น ชื่อที่แสดงที่ผู้ใช้มองเห็น คำอธิบาย และหลักเกณฑ์

หากคุณเป็นผู้ดูแลระบบ Google Workspace คุณสามารถเรียกเมธอด patch() เพื่ออัปเดตพื้นที่ทำงานที่มีอยู่ในองค์กร Google Workspace ได้

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

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

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

Node.js

Python

Java

Apps Script

อัปเดตพื้นที่ทำงานในฐานะผู้ใช้

หากต้องการอัปเดตพื้นที่ทำงานที่มีอยู่ใน Google Chat ด้วย การตรวจสอบสิทธิ์ผู้ใช้ ให้ส่ง ข้อมูลต่อไปนี้ในคำขอ

  • ระบุขอบเขตการให้สิทธิ์ chat.spaces
  • เรียกเมธอด UpdateSpace() ในคำขอ ให้ระบุช่อง name ของพื้นที่ทำงาน ช่อง updateMask ที่มีช่องอย่างน้อย 1 ช่องที่จะอัปเดต และ body ที่มีข้อมูลพื้นที่ทำงานที่อัปเดต

คุณสามารถอัปเดตข้อมูลต่างๆ เช่น ชื่อที่แสดง ประเภทพื้นที่ทำงาน สถานะประวัติ และอื่นๆ ดูช่องทั้งหมดที่อัปเดตได้ใน เอกสารอ้างอิง

วิธีอัปเดตช่อง displayName ของพื้นที่ทำงานที่มีอยู่

Node.js

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

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

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

  // Initialize request argument(s)
  const request = {
    space: {
      // Replace SPACE_NAME here
      name: 'spaces/SPACE_NAME',
      displayName: 'New space display name',
    },
    // The field paths to update. Separate multiple values with commas or use `*`
    // to update all field paths.
    updateMask: {
      // The field paths to update.
      paths: ['display_name'],
    },
  };

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

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

await main();

Python

chat/client-libraries/cloud/update_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"]

# This sample shows how to update a space with user credential
def update_space_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.UpdateSpaceRequest(
        space = {
            # Replace SPACE_NAME here
            'name': 'spaces/SPACE_NAME',
            'display_name': 'New space display name'
        },
        # The field paths to update. Separate multiple values with commas.
        update_mask = 'displayName'
    )

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

    # Handle the response
    print(response)

update_space_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/UpdateSpaceUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.UpdateSpaceRequest;
import com.google.chat.v1.Space;
import com.google.protobuf.FieldMask;

// This sample shows how to update space with user credential.
public class UpdateSpaceUserCred {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      UpdateSpaceRequest.Builder request = UpdateSpaceRequest.newBuilder()
        .setSpace(Space.newBuilder()
          // Replace SPACE_NAME here.
          .setName("spaces/SPACE_NAME")
          .setDisplayName("New space display name"))
        .setUpdateMask(FieldMask.newBuilder()
          // The field paths to update.
          .addPaths("display_name"));
      Space response = chatServiceClient.updateSpace(request.build());

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

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to update a space with user credential
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces'
 * referenced in the manifest file (appsscript.json).
 */
function updateSpaceUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here
  const name = "spaces/SPACE_NAME";
  const space = {
    displayName: "New space display name",
  };
  // The field paths to update. Separate multiple values with commas or use
  // `*` to update all field paths.
  const updateMask = "displayName";

  // Make the request
  const response = Chat.Spaces.patch(space, name, {
    updateMask: updateMask,
  });

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

หากต้องการเรียกใช้ตัวอย่างนี้ ให้แทนที่ SPACE_NAME ด้วยรหัสจาก ช่อง name ของพื้นที่ทำงาน คุณสามารถรับรหัสได้โดยการเรียก ListSpaces() เมธอด หรือจาก URL ของพื้นที่ทำงาน

Google Chat API จะแสดงผลอินสแตนซ์ของ Space ที่แสดงการ อัปเดต

อัปเดตพื้นที่ทำงานในฐานะผู้ดูแลระบบ Google Workspace

หากคุณเป็นผู้ดูแลระบบ Google Workspace คุณสามารถเรียกเมธอด UpdateSpace() เพื่ออัปเดตพื้นที่ทำงานใดก็ได้ในองค์กร Google Workspace

หากต้องการเรียกเมธอดนี้ในฐานะผู้ดูแลระบบ Google Workspace ให้ทำดังนี้

ดูข้อมูลเพิ่มเติมและตัวอย่างได้ที่ จัดการพื้นที่ทำงานใน Google Chat ในฐานะผู้ดูแลระบบ Google Workspace

อัปเดตพื้นที่ทำงานในฐานะแอปใน Chat

การตรวจสอบสิทธิ์แอปต้องได้รับการอนุมัติจากผู้ดูแลระบบเพียงครั้งเดียว

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

  • ระบุขอบเขตการให้สิทธิ์ chat.app.spaces เมื่อใช้การตรวจสอบสิทธิ์แอป คุณจะอัปเดตได้เฉพาะพื้นที่ทำงานที่สร้างโดยแอปใน Chat เท่านั้น
  • เรียกเมธอด patch ในทรัพยากร Space ในคำขอ ให้ระบุช่อง name ของพื้นที่ทำงาน ช่อง updateMask ที่มีช่องอย่างน้อย 1 ช่องที่จะอัปเดต และ body ที่มีข้อมูลพื้นที่ทำงานที่อัปเดต

คุณสามารถอัปเดตข้อมูลต่างๆ เช่น ชื่อที่แสดง ประเภทพื้นที่ทำงาน สถานะประวัติ การตั้งค่าสิทธิ์ และอื่นๆ ดูช่องทั้งหมดที่อัปเดตได้ใน เอกสารอ้างอิง

เขียนสคริปต์ที่เรียก Chat API

วิธีอัปเดตช่อง spaceDetails ของพื้นที่ทำงานที่มีอยู่

Python

  1. สร้างไฟล์ชื่อ chat_space_update_app.py ในไดเรกทอรีงาน
  2. ใส่โค้ดต่อไปนี้ใน chat_space_update_app.py

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.app.spaces"]
    
    def main():
        '''
        Authenticates with Chat API using app authentication,
        then updates the specified space description and guidelines.
        '''
    
        # Specify service account details.
        creds = (
            service_account.Credentials.from_service_account_file('credentials.json')
            .with_scopes(SCOPES)
        )
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().patch(
    
          # The space to update, and the updated space details.
          #
          # Replace {space} with a space name.
          # Obtain the space name from the spaces resource of Chat API,
          # or from a space's URL.
          name='spaces/SPACE',
          updateMask='spaceDetails',
          body={
    
            'spaceDetails': {
              'description': 'This description was updated with Chat API!',
              'guidelines': 'These guidelines were updated with Chat API!'
            }
    
          }
    
        ).execute()
    
        # Prints details about the updated space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. แทนที่ข้อมูลต่อไปนี้ในโค้ด

    • SPACE ด้วยชื่อพื้นที่ทำงาน ซึ่ง คุณรับได้จาก spaces.list เมธอด ใน Chat API หรือจาก URL ของพื้นที่ทำงาน
  4. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีงานโดยทำดังนี้

    python3 chat_space_update_app.py

Google Chat API จะแสดงผลอินสแตนซ์ของ Space ทรัพยากร ที่แสดง การอัปเดต

ข้อจำกัดและข้อควรพิจารณา