ดูรายละเอียดเกี่ยวกับข้อความ

คู่มือนี้จะอธิบายวิธีใช้เมธอด get() ในทรัพยากร Message ของ Google Chat API เพื่อแสดงรายละเอียดเกี่ยวกับข้อความที่เป็นข้อความหรือการ์ด

ใน Chat API ข้อความ Chat จะแสดงด้วยMessageทรัพยากร แม้ว่าผู้ใช้ Chat จะส่งได้เฉพาะข้อความที่มีข้อความ แต่ แอปใน Chat สามารถใช้ฟีเจอร์การรับส่งข้อความอื่นๆ ได้อีกมากมาย ซึ่งรวมถึง การแสดงอินเทอร์เฟซผู้ใช้แบบคงที่หรือแบบอินเทอร์แอกทีฟ การรวบรวมข้อมูลจาก ผู้ใช้ และการส่งข้อความแบบส่วนตัว ดูข้อมูลเพิ่มเติมเกี่ยวกับฟีเจอร์การรับส่งข้อความที่ใช้ได้กับ Chat API ได้ที่ภาพรวมของข้อความ 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.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);
}

main().catch(console.error);

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

Apps Script

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

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

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

Chat API จะแสดงอินสแตนซ์ของ Message ซึ่งมีรายละเอียดของข้อความที่ระบุ

รับข้อความที่มีการตรวจสอบสิทธิ์แอป

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

ตัวอย่างต่อไปนี้จะรับข้อความที่มีการตรวจสอบสิทธิ์แอป

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

main().catch(console.error);

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

Apps Script

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

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

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

Chat API จะแสดงอินสแตนซ์ของ Message ซึ่งมีรายละเอียดของข้อความที่ระบุ

รับข้อความในฐานะแอป Chat ที่ได้รับการอนุมัติจากผู้ดูแลระบบ

การตรวจสอบสิทธิ์แอปที่มีchat.app.*ขอบเขตการให้สิทธิ์ต้องมีการอนุมัติจากผู้ดูแลระบบแบบครั้งเดียว

หากต้องการดูรายละเอียดเกี่ยวกับข้อความที่มีการตรวจสอบสิทธิ์แอปโดยใช้ Chat REST API ให้ส่งข้อมูลต่อไปนี้ในคำขอ

  • เรียกใช้เมธอด GetMessage()
  • ระบุchat.app.messages.readonlyขอบเขตการให้สิทธิ์
  • ตั้งค่า name เป็นชื่อทรัพยากรของข้อความที่จะรับ

สร้างคีย์ API

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

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

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

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

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

เขียนสคริปต์ที่เรียกใช้ 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, 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().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. ในโค้ด ให้แทนที่ค่าต่อไปนี้

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

    python3 chat_messages_get_admin_app.py

Chat API จะแสดงอินสแตนซ์ของ Message ซึ่งมีรายละเอียดของข้อความที่ระบุ