ค้นหาข้อความ

เอกสารนี้อธิบายวิธีใช้เมธอด search ในทรัพยากร Message ของ Google Chat API เพื่อค้นหาข้อความที่ผู้ใช้ที่ตรวจสอบสิทธิ์แล้วมีสิทธิ์เข้าถึง

เมื่อใช้การตรวจสอบสิทธิ์ผู้ใช้ คุณจะค้นหาข้อความในการสนทนาทั้งหมดที่ผู้ใช้เข้าร่วมหรือภายในการสนทนาที่เฉพาะเจาะจงได้ เช่น คุณสามารถค้นหาข้อความที่มีคีย์เวิร์ดที่เฉพาะเจาะจง พูดถึงผู้ใช้ ยังไม่ได้อ่าน หรือมีไฟล์แนบ

ใน Chat API ข้อความแชทจะแสดงด้วยทรัพยากร Message แม้ว่าผู้ใช้ Chat จะส่งได้เฉพาะข้อความที่มีข้อความ แต่แอปใน Chat สามารถใช้ฟีเจอร์การรับส่งข้อความอื่นๆ ได้อีกมากมาย เช่น การแสดงอินเทอร์เฟซผู้ใช้แบบคงที่หรือแบบอินเทอร์แอกทีฟ การรวบรวมข้อมูลจากผู้ใช้ และการส่งข้อความแบบส่วนตัว ดูข้อมูลเพิ่มเติมเกี่ยวกับฟีเจอร์ข้อความแชทที่ใช้ได้กับ Chat API ได้ที่ภาพรวมข้อความ Google Chat

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

Node.js

  • บัญชี Google Workspace แบบ Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

Python

  • บัญชี Google Workspace แบบ Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

Java

  • บัญชี Google Workspace แบบ Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

Apps Script

  • บัญชี Google Workspace แบบ Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

ค้นหาข้อความ

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

  • ระบุขอบเขตการให้สิทธิ์ chat.messages.readonly หรือ chat.messages

  • เรียกใช้เมธอด SearchMessages

  • ตั้งค่า parent เป็น spaces/- เพื่อค้นหาภายในพื้นที่ทำงานทั้งหมดที่ผู้ใช้เป็นสมาชิก การใช้ค่าอื่นๆ จะทำให้เกิดข้อผิดพลาด

  • ในช่อง filter ให้ระบุสตริงคำค้นหา คำค้นหาอาจรวมถึงคีย์เวิร์ดและตัวกรอง

ตัวอย่างโค้ดต่อไปนี้จะค้นหาข้อความที่ยังไม่อ่านซึ่งมีคีย์เวิร์ด "tasks"

Node.js

/**
 * Searches for messages in Google Chat.
 * @param {string} filter The search query.
 */
async function searchMessages(filter) {
  const {ChatServiceClient} = require('@google-apps/chat').v1;

  // Instantiates a client
  const chatClient = new ChatServiceClient();

  // See https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/authentication-utils.js
  // for an example of how to authenticate the request.

  // Construct request
  const request = {
    // Parent must be "spaces/-" to search across all spaces.
    parent: 'spaces/-',
    filter: filter,
  };

  // Run request
  const iterable = await chatClient.searchMessagesAsync(request);
  for await (const response of iterable) {
    console.log(response);
  }
}

searchMessages('tasks AND is_unread()');

Python

from google.apps import chat_v1

def search_messages(filter_str: str):
    """
    Searches for messages in Google Chat.
    Args:
        filter_str: The search query.
    """
    # Create a client
    client = chat_v1.ChatServiceClient()

    # See https://github.com/googleworkspace/python-samples/blob/main/chat/client-libraries/cloud/authentication_utils.py
    # for an example of how to authenticate the request.

    # Initialize request argument
    request = chat_v1.SearchMessagesRequest(
        # Parent must be "spaces/-" to search across all spaces.
        parent="spaces/-",
        filter=filter_str
    )

    # Make the request
    page_result = client.search_messages(request=request)

    # Handle the response
    for response in page_result:
        print(response)

search_messages('tasks AND is_unread()')

Java

import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.SearchMessageResult;
import com.google.chat.v1.SearchMessagesRequest;

public class SearchMessages {
  public static void main(String[] args) throws Exception {
    searchMessages("tasks AND is_unread()");
  }

  /**
   * Searches for messages in Google Chat.
   *
   * @param filter The search query.
   */
  public static void searchMessages(String filter) throws Exception {
    // See https://github.com/googleworkspace/java-samples/blob/main/chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/AuthenticationUtils.java
    // for an example of how to authenticate the request.
    try (ChatServiceClient chatServiceClient = ChatServiceClient.create()) {
      SearchMessagesRequest request =
          SearchMessagesRequest.newBuilder()
              .setParent("spaces/-")
              .setFilter(filter)
              .build();
      for (SearchMessageResult result : chatServiceClient.searchMessages(request).iterateAll()) {
        System.out.println(result.getMessage().getText());
      }
    }
  }
}

Apps Script

/**
 * Searches for messages in Google Chat.
 */
function searchMessages() {
  const parent = 'spaces/-';
  const request = {
    filter: 'tasks AND is_unread()',
  };
  try {
    const response = Chat.Spaces.Messages.search(request, parent);
    if (response.results && response.results.length > 0) {
      for (const result of response.results) {
        console.log('Message text: %s', result.message.text);
      }
    } else {
      console.log('No messages found.');
    }
  } catch (err) {
    console.log('Failed to search messages with error: %s', err.message);
  }
}

ใช้ตัวกรองและโอเปอเรเตอร์การค้นหา

คุณสามารถปรับแต่งผลการค้นหาได้โดยใช้คีย์เวิร์ด ช่อง และฟังก์ชันในช่อง filter ดูข้อมูลเพิ่มเติมได้ที่ SearchMessagesRequest

หากต้องการค้นหาข้อความที่มีข้อความที่เฉพาะเจาะจง ให้ป้อนคีย์เวิร์ด เช่น หากต้องการค้นหารายงานที่รอดำเนินการ ให้ใช้ pending reports

ค้นหาตามช่อง

คุณสามารถกรองผลลัพธ์ตามช่องข้อความหรือพื้นที่ทำงานที่เฉพาะเจาะจงได้ เช่น

  • create_time: กรองตามเวลาที่สร้างข้อความ ตัวอย่าง: create_time > "2023-01-01T00:00:00Z"
  • sender.name: กรองตามชื่อทรัพยากรของผู้ส่ง ตัวอย่าง: sender.name = "users/1234567890"
  • space.name: จำกัดการค้นหาเฉพาะพื้นที่ทำงานที่ระบุ ตัวอย่าง: space.name = "spaces/ABCDEFGH"
  • space.display_name: กรองพื้นที่ทำงานตามชื่อที่แสดงที่ตรงกันบางส่วน ผลการค้นหาจะจำกัดอยู่ที่พื้นที่ทำงานที่ตรงกัน 5 อันดับแรก ตัวอย่าง: space.display_name:Project
  • attachment: ตรวจสอบว่ามีไฟล์แนบหรือไม่ ตัวอย่าง: attachment:*
  • annotations.user_mentions.user.name: กรองตามการพูดถึง ตัวอย่าง: annotations.user_mentions.user.name:"users/me"

ค้นหาโดยใช้ฟังก์ชัน

การกรองขั้นสูงพร้อมใช้งานผ่านฟังก์ชันต่อไปนี้

  • has_link: แสดงผลข้อความที่มีไฮเปอร์ลิงก์อย่างน้อย 1 รายการ
  • is_unread: แสดงข้อความที่ผู้ใช้ยังไม่ได้อ่าน

ในช่องต่างๆ ระบบรองรับเฉพาะโอเปอเรเตอร์ AND เท่านั้น ตัวอย่างเช่น sender.name = "users/me" AND is_unread()