สร้างพื้นที่ทำงานที่มีชื่อใน Google Chat

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

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

หากต้องการสร้างพื้นที่ใน Chat ประเภทต่างๆ (รวมถึงข้อความส่วนตัวหรือข้อความกลุ่ม) ให้ใช้วิธี setUp() ในแหล่งข้อมูล Space เพื่อสร้างพื้นที่ทำงานและเพิ่มสมาชิกพร้อมกัน โปรดดูรายละเอียดที่ตั้งค่าพื้นที่ทำงาน

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

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

Node.js

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

Python

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

Java

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

Apps Script

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

สร้างพื้นที่ทำงานที่มีชื่อในฐานะผู้ใช้

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

  • ระบุขอบเขตการให้สิทธิ์ chat.spaces.create หรือ chat.spaces
  • เรียกใช้เมธอด CreateSpace() โดยส่ง space เป็นอินสแตนซ์ของ Space ที่มีช่องต่อไปนี้
    • ตั้งค่าspaceTypeเป็น SPACE
    • displayName ตั้งค่าเป็นชื่อที่ผู้ใช้มองเห็นได้ของพื้นที่ทำงาน
    • ตั้งค่าแอตทริบิวต์อื่นๆ เช่น แอตทริบิวต์ต่อไปนี้ หากต้องการ
      • spaceDetails- คำอธิบายที่ผู้ใช้มองเห็นและชุดหลักเกณฑ์สำหรับ พื้นที่ทำงาน
      • predefinedPermissionSettings - สิทธิ์ที่กำหนดไว้ล่วงหน้าสำหรับพื้นที่ทำงาน เช่น คุณสามารถกำหนดค่าเพื่อให้สมาชิกทั้งหมดหรือเฉพาะผู้จัดการพื้นที่ทำงานโพสต์ข้อความได้

วิธีสร้างพื้นที่ทำงานที่มีชื่อมีดังนี้

Node.js

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

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

// This sample shows how to create a named 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: {
      spaceType: 'SPACE',
      // Replace DISPLAY_NAME here.
      displayName: 'DISPLAY_NAME'
    }
  };

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

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

main().catch(console.error);

Python

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

def create_space_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.CreateSpaceRequest(
        space = {
            "space_type": 'SPACE',
            # Replace DISPLAY_NAME here.
            "display_name": 'DISPLAY_NAME'
        }
    )

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

    # Handle the response
    print(response)

create_space_with_user_cred()

Java

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

// This sample shows how to create space with user credential.
public class CreateSpaceUserCred {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      CreateSpaceRequest.Builder request = CreateSpaceRequest.newBuilder()
        .setSpace(Space.newBuilder()
          .setSpaceType(Space.SpaceType.SPACE)
          // Replace DISPLAY_NAME here.
          .setDisplayName("DISPLAY_NAME"));
      Space response = chatServiceClient.createSpace(request.build());

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

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to create space with user credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.create'
 * referenced in the manifest file (appsscript.json).
 */
function createSpaceUserCred() {
  // Initialize request argument(s)
  const space = {
    spaceType: 'SPACE',
    // TODO(developer): Replace DISPLAY_NAME here
    displayName: 'DISPLAY_NAME'
  };

  // Make the request
  const response = Chat.Spaces.create(space);

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

สร้างพื้นที่ทำงานที่มีชื่อเป็นแอป Chat

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

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

  • ระบุขอบเขตการให้สิทธิ์ chat.app.spaces.create หรือ chat.app.spaces
  • เรียกใช้เมธอด create ในทรัพยากร Space
  • ตั้งค่า spaceType เป็น SPACE
  • ตั้งค่า displayName เป็นชื่อพื้นที่ทำงานที่ผู้ใช้มองเห็น ในตัวอย่างต่อไปนี้ เราตั้งค่า displayName เป็น API-made
  • ระบุรหัสลูกค้าของโดเมน Google Workspace โดยใช้ฟิลด์ customer
  • หรือจะตั้งค่าแอตทริบิวต์อื่นๆ ของพื้นที่ทำงานก็ได้ เช่น spaceDetails (คำอธิบายที่ผู้ใช้มองเห็นและชุดหลักเกณฑ์สำหรับพื้นที่ทำงาน)

สร้างคีย์ API

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

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

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

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

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

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

วิธีสร้างพื้นที่ทำงานที่มีชื่อมีดังนี้

Python

  1. สร้างไฟล์ชื่อ chat_space_create_named_app.py ในไดเรกทอรีการทำงาน
  2. ใส่โค้ดต่อไปนี้ใน chat_space_create_named_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.create"]
    
    def main():
        '''
        Authenticates with Chat API using app authentication,
        then creates a Chat space.
        '''
    
        # 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().create(
    
          # Details about the space to create.
          body = {
    
            # To create a named space, set spaceType to SPACE.
            'spaceType': 'SPACE',
    
            # The user-visible name of the space.
            'displayName': 'API-made',
    
            # The customer ID of the Workspace domain.
            'customer': 'CUSTOMER'
          }
    
          ).execute()
    
        # Prints details about the created space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ค่าต่อไปนี้

    • API_KEY: คีย์ API ที่คุณสร้างขึ้นเพื่อสร้าง ปลายทางบริการสำหรับ Chat API

    • CUSTOMER: รหัสลูกค้าของโดเมนของ พื้นที่ในรูปแบบ customer/{customer} โดยที่ {customer} คือ ID จาก แหล่งข้อมูลลูกค้าของ Admin SDK หากต้องการสร้างพื้นที่ทำงานในองค์กร Google Workspace เดียวกันกับ แอป Chat ให้ใช้ customers/my_customer

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างโดยทำดังนี้

    python3 chat_space_create_named_app.py

เปิดพื้นที่ทำงานใน Google Chat

หากต้องการไปยังพื้นที่ทำงาน ให้ใช้รหัสทรัพยากรของพื้นที่ทำงาน เพื่อสร้าง URL ของพื้นที่ทำงาน คุณดูรหัสทรัพยากรได้จากพื้นที่ทำงาน name ในเนื้อหาการตอบกลับของ Google Chat เช่น หาก nameของพื้นที่ทำงานคือ spaces/1234567 คุณจะไปยังพื้นที่ทำงานได้โดยใช้ URL https://mail.google.com/chat/u/0/#chat/space/1234567

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

  • เมื่อสร้างพื้นที่ทำงานโดยใช้การตรวจสอบสิทธิ์แอป ระบบจะเพิ่มแอป Chat ที่ตรวจสอบสิทธิ์ เป็นสมาชิกของพื้นที่ทำงาน แต่ ไม่เหมือนกับการตรวจสอบสิทธิ์ผู้ใช้ ซึ่งจะไม่เพิ่มเป็นผู้จัดการพื้นที่ทำงาน โดยค่าเริ่มต้น สมาชิกในพื้นที่ทำงาน ทุกคนจะนำแอป Chat ออกได้ หากต้องการอนุญาตให้เฉพาะผู้จัดการพื้นที่ทำงาน นำแอป Chat ออก ให้ตั้งค่า permissionSettings.manageApps เป็น managersAllowed