إنشاء مساحة مُسمّاة في 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.

جافا

  • حساب Google Workspace للعمل أو للمؤسسة مع إمكانية الوصول إلى Google Chat.

برمجة التطبيقات

  • حساب 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);
}

await main();

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()

جافا

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));
    }
  }
}

برمجة التطبيقات

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 (وصف يظهر للمستخدم ومجموعة من الإرشادات للمساحة).

كتابة نص برمجي يستدعي 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)
    
        # 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. في الرمز البرمجي، استبدِل ما يلي:

    • 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.