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

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

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

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

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

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

Node.js

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

Python

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

Java

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

Apps Script

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

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

หากต้องการอัปเดตพื้นที่ทำงานที่มีอยู่แล้วใน 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);
}

main().catch(console.error);

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 พร้อมข้อมูลพื้นที่ทำงานที่อัปเดตแล้ว

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

สร้างคีย์ API

หากต้องการเรียกเมธอด API ของรุ่นตัวอย่างสำหรับนักพัฒนาซอฟต์แวร์ คุณต้องใช้เอกสารการค้นพบ API เวอร์ชันตัวอย่างสำหรับนักพัฒนาซอฟต์แวร์ที่ไม่ใช่แบบสาธารณะ หากต้องการตรวจสอบสิทธิ์คำขอ คุณต้องส่งคีย์ API

หากต้องการสร้างคีย์ API ให้เปิดโปรเจ็กต์ Google Cloud ของแอป แล้วทำดังนี้

  1. ในคอนโซล Google Cloud ให้ไปที่เมนู > API และบริการ > ข้อมูลเข้าสู่ระบบ

    ไปที่ข้อมูลเข้าสู่ระบบ

  2. คลิกสร้างข้อมูลเข้าสู่ระบบ > คีย์ API
  3. ระบบจะแสดงคีย์ API ใหม่
    • คลิกคัดลอก เพื่อคัดลอกคีย์ API สำหรับใช้ในโค้ดของแอป คุณยังค้นหาคีย์ API ได้ในส่วน "คีย์ API" ของข้อมูลเข้าสู่ระบบของโปรเจ็กต์
    • คลิกจำกัดคีย์เพื่ออัปเดตการตั้งค่าขั้นสูงและจำกัดการใช้ คีย์ API ดูรายละเอียดเพิ่มเติมได้ที่การใช้ข้อจำกัดของคีย์ API

เขียนสคริปต์ที่เรียกใช้ 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, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY')
    
        # 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. ในโค้ด ให้แทนที่ค่าต่อไปนี้

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

    python3 chat_space_update_app.py

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

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