เชิญหรือเพิ่มผู้ใช้, แอป Google Group หรือ Google Chat ไปยังพื้นที่ทำงาน

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

หากคุณเป็นผู้ดูแลระบบ Google Workspace คุณสามารถเพิ่มผู้ใช้ Google Groups หรือแอป Chat ลงในพื้นที่ใดก็ได้ในองค์กร Google Workspace

Membershipทรัพยากร แสดงว่าผู้ใช้ที่เป็นมนุษย์หรือแอป Google Chat ได้รับเชิญให้เข้าร่วม เป็นส่วนหนึ่ง หรือไม่ได้อยู่ในพื้นที่ทำงาน

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

Node.js

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

Python

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

Java

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

Apps Script

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

เชิญหรือเพิ่มผู้ใช้ไปยังพื้นที่ทำงานในฐานะผู้ใช้

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

  • ระบุchat.membershipsขอบเขตการให้สิทธิ์
  • เรียกใช้เมธอด CreateMembership()
  • ส่ง parent เป็นชื่อทรัพยากรของพื้นที่ทำงานที่จะสร้างการเป็นสมาชิก
  • ส่ง membership เป็นอินสแตนซ์ของ Membership โดยตั้งค่าฟิลด์ member ด้วยค่าต่อไปนี้
    • ตั้งค่าฟิลด์ type เป็น HUMAN
    • ฟิลด์ name ตั้งค่าเป็น users/{user} โดยที่ {user} คือบุคคลที่คุณต้องการเพิ่มลงในพื้นที่ทำงาน หากต้องการระบุผู้ใช้ Chat ให้แทนที่ {user} ด้วยรายการใดก็ได้ต่อไปนี้
      • รหัสสำหรับ บุคคล ใน People API เช่น หาก People API person resourceName คือ people/123456789 ให้ใช้ค่า users/123456789
      • รหัสของ ผู้ใช้ ใน Directory API
      • อีเมลของผู้ใช้ เช่น users/222larabrown@gmail.com หรือ users/larabrown@cymbalgroup.com หากผู้ใช้ใช้บัญชี Google หรือ อยู่ในองค์กร Google Workspace อื่น คุณต้อง ใช้อีเมลของผู้ใช้

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

Node.js

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

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

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

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here.
    parent: 'spaces/SPACE_NAME',
    membership: {
      member: {
        // Replace USER_NAME here
        name: 'users/USER_NAME',
        // User type for the membership
        type: 'HUMAN',
      },
    },
  };

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

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

await main();

Python

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

# This sample shows how to create membership with user credential for a human
# user
def create_membership_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.CreateMembershipRequest(
        # Replace SPACE_NAME here
        parent = "spaces/SPACE_NAME",
        membership = {
            "member": {
                # Replace USER_NAME here
                "name": "users/USER_NAME",
                # user type for the membership
                "type_": "HUMAN"
            }
        }
    )

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

    # Handle the response
    print(response)

create_membership_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/CreateMembershipUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.CreateMembershipRequest;
import com.google.chat.v1.Membership;
import com.google.chat.v1.SpaceName;
import com.google.chat.v1.User;

// This sample shows how to create membership with user credential for a human
// user.
public class CreateMembershipUserCred {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      CreateMembershipRequest.Builder request = CreateMembershipRequest.newBuilder()
        // replace SPACE_NAME here
        .setParent("spaces/SPACE_NAME")
        .setMembership(Membership.newBuilder()
          .setMember(User.newBuilder()
            // replace USER_NAME here
            .setName("users/USER_NAME")
            // user type for the membership
            .setType(User.Type.HUMAN)));
      Membership response = chatServiceClient.createMembership(request.build());

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

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to create membership with user credential for a human user
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.memberships'
 * referenced in the manifest file (appsscript.json).
 */
function createMembershipUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here.
  const parent = "spaces/SPACE_NAME";
  const membership = {
    member: {
      // TODO(developer): Replace USER_NAME here
      name: "users/USER_NAME",
      // User type for the membership
      type: "HUMAN",
    },
  };

  // Make the request
  const response = Chat.Spaces.Members.create(membership, parent);

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

หากต้องการเรียกใช้ตัวอย่าง ให้แทนที่ค่าต่อไปนี้

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

Chat API จะแสดงอินสแตนซ์ของ Membership ซึ่งแสดงรายละเอียดการเป็นสมาชิกของผู้ใช้ที่สร้างขึ้น

เชิญหรือเพิ่ม Google Group ในพื้นที่ทำงาน

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

  • ระบุchat.membershipsขอบเขตการให้สิทธิ์
  • เรียกใช้เมธอด CreateMembership()
  • ส่ง parent เป็นชื่อทรัพยากรของพื้นที่ทำงานที่จะสร้างการเป็นสมาชิก
  • ส่ง membership เป็นอินสแตนซ์ของ Membership โดยมีฟิลด์ name ของ groupMember ตั้งค่าเป็น groups/{group} โดยที่ {group} คือรหัสกลุ่มที่ต้องการสร้างการเป็นสมาชิก คุณเรียกข้อมูลรหัสของกลุ่มได้โดยใช้ Cloud Identity API

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

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

Node.js

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

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

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

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here.
    parent: 'spaces/SPACE_NAME',
    membership: {
      groupMember: {
        // Replace GROUP_NAME here
        name: 'groups/GROUP_NAME',
      },
    },
  };

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

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

await main();

Python

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

# This sample shows how to create membership with user credential for a group
def create_membership_with_user_cred_for_group():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.CreateMembershipRequest(
        # Replace SPACE_NAME here
        parent = "spaces/SPACE_NAME",
        membership = {
            "groupMember": {
                # Replace GROUP_NAME here
                "name": "groups/GROUP_NAME"
            }
        }
    )

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

    # Handle the response
    print(response)

create_membership_with_user_cred_for_group()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/CreateMembershipUserCredForGroup.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.CreateMembershipRequest;
import com.google.chat.v1.Membership;
import com.google.chat.v1.SpaceName;
import com.google.chat.v1.Group;

// This sample shows how to create membership with user credential for a group.
public class CreateMembershipUserCredForGroup {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      CreateMembershipRequest.Builder request = CreateMembershipRequest.newBuilder()
        // replace SPACE_NAME here
        .setParent("spaces/SPACE_NAME")
        .setMembership(Membership.newBuilder()
          .setGroupMember(Group.newBuilder()
            // replace GROUP_NAME here
            .setName("groups/GROUP_NAME")));
      Membership response = chatServiceClient.createMembership(request.build());

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

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to create membership with user credential for a group
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.memberships'
 * referenced in the manifest file (appsscript.json).
 */
function createMembershipUserCredForGroup() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here.
  const parent = "spaces/SPACE_NAME";
  const membership = {
    groupMember: {
      // TODO(developer): Replace GROUP_NAME here
      name: "groups/GROUP_NAME",
    },
  };

  // Make the request
  const response = Chat.Spaces.Members.create(membership, parent);

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

หากต้องการเรียกใช้ตัวอย่าง ให้แทนที่ค่าต่อไปนี้

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

Chat API จะแสดงอินสแตนซ์ของ Membership ซึ่งแสดงรายละเอียดการเป็นสมาชิกของผู้ใช้ที่สร้างขึ้น

เพิ่มแอป Chat ลงในพื้นที่ทำงาน

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

  • ระบุchat.memberships.appขอบเขตการให้สิทธิ์
  • เรียกใช้เมธอด CreateMembership()
  • ส่ง parent เป็นชื่อทรัพยากรของพื้นที่ทำงานที่จะสร้างการเป็นสมาชิก
  • ส่ง membership เป็นอินสแตนซ์ของ Membership โดยตั้งค่าฟิลด์ member ด้วยค่าต่อไปนี้
    • ตั้งค่าฟิลด์ type เป็น BOT
    • ฟิลด์ name ตั้งค่าเป็น users/app ซึ่งเป็นชื่อแทนที่แสดงแอป ที่เรียกใช้ Chat API

ตัวอย่างต่อไปนี้จะเพิ่มแอป Chat ลงในพื้นที่ทำงาน

Node.js

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

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

// This sample shows how to create an app membership.
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(
    USER_AUTH_OAUTH_SCOPES,
  );

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here.
    parent: 'spaces/SPACE_NAME',
    membership: {
      member: {
        // Member name for app membership, do not change this
        name: 'users/app',
        // User type for the membership
        type: 'BOT',
      },
    },
  };

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

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

await main();

Python

chat/client-libraries/cloud/create_membership_user_cred_for_app.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.memberships.app"]

# This sample shows how to create membership with app credential for an app
def create_membership_with_user_cred_for_app():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.CreateMembershipRequest(
        # Replace SPACE_NAME here
        parent = "spaces/SPACE_NAME",
        membership = {
            "member": {
                # member name for app membership, do not change this.
                "name": "users/app",
                # user type for the membership
                "type_": "BOT"
            }
        }
    )

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

    # Handle the response
    print(response)

create_membership_with_user_cred_for_app()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/CreateMembershipUserCredForApp.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.CreateMembershipRequest;
import com.google.chat.v1.Membership;
import com.google.chat.v1.SpaceName;
import com.google.chat.v1.User;

// This sample shows how to create membership with user credential for the
// calling app.
public class CreateMembershipUserCredForApp {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      CreateMembershipRequest.Builder request = CreateMembershipRequest.newBuilder()
        // replace SPACE_NAME here
        .setParent("spaces/SPACE_NAME")
        .setMembership(Membership.newBuilder()
          .setMember(User.newBuilder()
            // member name for app membership, do not change this.
            .setName("users/app")
            // user type for the membership
            .setType(User.Type.BOT)));
      Membership response = chatServiceClient.createMembership(request.build());

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

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to create membership with app credential for an app
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.memberships.app'
 * referenced in the manifest file (appsscript.json).
 */
function createMembershipUserCredForApp() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here.
  const parent = "spaces/SPACE_NAME";
  const membership = {
    member: {
      // Member name for app membership, do not change this
      name: "users/app",
      // User type for the membership
      type: "BOT",
    },
  };

  // Make the request
  const response = Chat.Spaces.Members.create(membership, parent);

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

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

Chat API จะแสดงอินสแตนซ์ของ Membership ซึ่งแสดงรายละเอียดการเป็นสมาชิกของผู้ใช้ที่สร้างขึ้น

เชิญหรือเพิ่มผู้ใช้ไปยังพื้นที่ทำงานเป็นแอป Chat

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

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

  • ระบุchat.app.membershipsขอบเขตการให้สิทธิ์
  • เรียกใช้create เมธอด ใน membership ทรัพยากร
  • ตั้งค่า parent เป็นชื่อทรัพยากรของพื้นที่ทำงานที่จะสร้างการเป็นสมาชิก
  • ตั้งค่า member เป็น users/{user} โดยที่ {user} คือบุคคลที่คุณต้องการ สร้างการเป็นสมาชิกให้ และเป็นอย่างใดอย่างหนึ่งต่อไปนี้
    • รหัสสำหรับ บุคคล ใน People API เช่น หาก People API person resourceName คือ people/123456789 ให้ตั้งค่า membership.member.name เป็น users/123456789
    • รหัสของ ผู้ใช้ ใน Directory API
    • อีเมลของผู้ใช้ เช่น users/222larabrown@gmail.com หรือ users/larabrown@cymbalgroup.com หากผู้ใช้ใช้บัญชี Google หรือ เป็นขององค์กร Google Workspace อื่น คุณต้องใช้อีเมลของบุคคลนั้น

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

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

Python

  1. สร้างไฟล์ชื่อ chat_membership_app_create.py ในไดเรกทอรีการทำงาน
  2. ใส่โค้ดต่อไปนี้ใน chat_membership_app_create.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.memberships"]
    
    def main():
        '''
        Authenticates with Chat API using app authentication,
        then adds a user to a Chat space by creating a membership.
        '''
    
        # 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().members().create(
    
            # The space in which to create a membership.
            parent = 'spaces/SPACE',
    
            # Specify which user the membership is for.
            body = {
              'member': {
                'name':'users/USER',
                'type': 'HUMAN'
              }
            }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ค่าต่อไปนี้

    • SPACE: ชื่อพื้นที่ ซึ่งคุณรับได้จากspaces.listเมธอด ใน Chat API หรือจาก URL ของพื้นที่

    • USER: รหัสผู้ใช้

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

    python3 chat_membership_app_create.py

เพิ่มผู้ใช้หรือ Google Groups ลงในพื้นที่ทำงานในฐานะผู้ดูแลระบบ Google Workspace

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

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

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

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