الحصول على تفاصيل حول رسالة

يوضِّح هذا الدليل كيفية استخدام get() طريقة في مورد Message ضِمن Google Chat API لعرض تفاصيل حول رسالة نصية أو رسالة بطاقة.

في Chat API، يتم تمثيل رسالة Chat بمورد Message. في حين لا يمكن لمستخدمي Chat إرسال سوى الرسائل التي تحتوي على نص، يمكن لتطبيقات Chat استخدام العديد من ميزات المراسلة الأخرى، بما في ذلك عرض واجهات مستخدم ثابتة أو تفاعلية، وجمع المعلومات من المستخدمين، وإرسال الرسائل بشكل خاص. لمزيد من المعلومات حول ميزات المراسلة المتاحة في Chat API، يُرجى الاطّلاع على نظرة عامة على رسائل Google Chat.

المتطلبات الأساسية

Node.js

  • حساب على Google Workspace Business أو Enterprise يتيح الوصول إلى Google Chat

Python

  • حساب على Google Workspace Business أو Enterprise يتيح الوصول إلى Google Chat

جافا

  • حساب على Google Workspace Business أو Enterprise يتيح الوصول إلى Google Chat

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

  • حساب على Google Workspace Business أو Enterprise يتيح الوصول إلى Google Chat

الحصول على رسالة باستخدام مصادقة المستخدم

للحصول على تفاصيل حول رسالة باستخدام مصادقة المستخدم، يُرجى تمرير ما يلي في طلبك:

  • تحديد نطاق التفويض chat.messages.readonly أو chat.messages
  • استدعاء GetMessage() طريقة.
  • ضبط name على اسم مورد الرسالة المطلوب الحصول عليها

يحصل المثال التالي على رسالة باستخدام مصادقة المستخدم:

Node.js

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

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

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

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME',
  };

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

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

await main();

Python

chat/client-libraries/cloud/get_message_user_cred.py
from authentication_utils import create_client_with_user_credentials
import google.oauth2.credentials

from google.apps import chat_v1 as google_chat

SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"]

# This sample shows how to get message with user credential
def get_message_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.GetMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = "spaces/SPACE_NAME/messages/MESSAGE_NAME",
    )

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

    # Handle the response
    print(response)

get_message_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMessageUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMessageRequest;
import com.google.chat.v1.Message;

// This sample shows how to get message with user credential.
public class GetMessageUserCred {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      GetMessageRequest.Builder request = GetMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/members/MESSAGE_NAME");
      Message response = chatServiceClient.getMessage(request.build());

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

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

chat/advanced-service/Main.gs
/**
 * This sample shows how to get message with user credential
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.readonly'
 * referenced in the manifest file (appsscript.json).
 */
function getMessageUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = "spaces/SPACE_NAME/messages/MESSAGE_NAME";

  // Make the request
  const response = Chat.Spaces.Messages.get(name);

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

لتشغيل هذا النموذج، يُرجى استبدال ما يلي:

تعرض Chat API نموذجًا من Message يوضّح الرسالة المحدّدة بالتفصيل.

الحصول على رسالة باستخدام مصادقة التطبيق

للحصول على تفاصيل حول رسالة باستخدام مصادقة التطبيق، يُرجى تمرير ما يلي في طلبك:

  • تحديد نطاق تفويض تستخدم الأمثلة في هذا القسم نطاق chat.bot المتاح بشكل عام ولا يتطلب موافقة المشرف. بدلاً من ذلك، يمكنك التفويض كتطبيق Chat بموافقة المشرف.
  • استدعاء GetMessage() طريقة.
  • ضبط name على اسم مورد الرسالة المطلوب الحصول عليها

يحصل المثال التالي على رسالة باستخدام مصادقة التطبيق:

Node.js

chat/client-libraries/cloud/get-message-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to get message with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME',
  };

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

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

await main();

Python

chat/client-libraries/cloud/get_message_app_cred.py
from authentication_utils import create_client_with_app_credentials
from google.apps import chat_v1 as google_chat

# This sample shows how to get message with app credential
def get_message_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.GetMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME',
    )

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

    # Handle the response
    print(response)

get_message_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMessageAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMessageRequest;
import com.google.chat.v1.Message;

// This sample shows how to get message with app credential.
public class GetMessageAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      GetMessageRequest.Builder request = GetMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/members/MESSAGE_NAME");
      Message response = chatServiceClient.getMessage(request.build());

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

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

chat/advanced-service/Main.gs
/**
 * This sample shows how to get message with app credential
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function getMessageAppCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = "spaces/SPACE_NAME/messages/MESSAGE_NAME";
  const parameters = {};

  // Make the request
  const response = Chat.Spaces.Messages.get(
    name,
    parameters,
    getHeaderWithAppCredentials(),
  );

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

لتشغيل هذا النموذج، يُرجى استبدال ما يلي:

تعرض Chat API نموذجًا من Message يوضّح الرسالة المحدّدة بالتفصيل.

الحصول على رسالة كتطبيق Chat بموافقة المشرف

تتطلب مصادقة التطبيق باستخدام نطاق تفويض chat.app.* موافقة المشرف لمرة واحدة .

للحصول على تفاصيل حول رسالة باستخدام مصادقة التطبيق باستخدام Chat REST API، يُرجى تمرير ما يلي في طلبك:

  • استدعاء GetMessage() طريقة.
  • تحديد نطاق التفويض chat.app.messages.readonly
  • ضبط name على اسم مورد الرسالة المطلوب الحصول عليها

كتابة نص برمجي يستدعي Chat API

إليك كيفية الحصول على تفاصيل حول رسالة باستخدام مصادقة التطبيق وموافقة المشرف وChat REST API:

Python

  1. في دليل العمل، يمكنك إنشاء ملف باسم chat_messages_get_admin_app.py.
  2. يمكنك تضمين الرمز البرمجي التالي في chat_messages_get_admin_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.messages.readonly"]
    
    def main():
        '''
        Authenticates with Chat API using app authentication,
        then gets details about a message.
        '''
    
        # 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().messages().get(
    
            # The message to get details about.
            #
            # Replace SPACE_NAME with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            name='spaces/SPACE_NAME/messages/MESSAGE_NAME',
    
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في الرمز البرمجي، يُرجى استبدال ما يلي:

  4. في دليل العمل، يمكنك إنشاء النموذج وتشغيله:

    python3 chat_messages_get_admin_app.py

تعرض Chat API نموذجًا من Message يوضّح الرسالة المحدّدة بالتفصيل.