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

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

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

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

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

Node.js

Python

Java

Apps Script

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

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

  • ระบุขอบเขตการให้สิทธิ์ 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

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