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

เอกสารนี้อธิบายวิธีใช้เมธอด search ในทรัพยากร 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

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

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

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

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

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

javascript /** * Searches for messages in Google Chat. */ function searchMessages() { const filter = 'tasks AND is_unread()'; const url = 'https://chat.googleapis.com/v1/spaces/-/messages:search'; const request_payload = { filter: filter }; try { const response = UrlFetchApp.fetch(url, { method: 'post', headers: { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() }, contentType: 'application/json', payload: JSON.stringify(request_payload) }); if (response.results) { 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()