บริการ Chat ขั้นสูง

บริการแชทขั้นสูงช่วยให้คุณใช้ Google Chat API ใน Apps Script ได้ API นี้ช่วยให้สคริปต์ค้นหา สร้าง และแก้ไข พื้นที่ทำงาน Chat เพิ่มหรือนำสมาชิกออกจากพื้นที่ทำงาน และอ่านหรือโพสต์ ข้อความที่มีข้อความ การ์ด ไฟล์แนบ และรีแอ็กชันได้

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

ข้อมูลอ้างอิง

ดูข้อมูลเพิ่มเติมเกี่ยวกับบริการนี้ได้ที่เอกสารอ้างอิง Chat API เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script บริการ Chat จะใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ

โค้ดตัวอย่าง

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

โพสต์ข้อความพร้อมข้อมูลเข้าสู่ระบบของผู้ใช้

ตัวอย่างต่อไปนี้แสดงวิธีโพสต์ข้อความไปยัง พื้นที่ทำงานใน Chat ในนามของผู้ใช้

  1. เพิ่มขอบเขตการให้สิทธิ์ chat.messages.create ลงในไฟล์ appsscript.json ของโปรเจ็กต์ Apps Script โดยทำดังนี้

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.messages.create"
    ]
    
  2. เพิ่มฟังก์ชันเช่นฟังก์ชันนี้ลงในโค้ดของโปรเจ็กต์ Apps Script

    advanced/chat.gs
    /**
     * Posts a new message to the specified space on behalf of the user.
     * @param {string} spaceName The resource name of the space.
     */
    function postMessageWithUserCredentials(spaceName) {
      try {
        const message = {'text': 'Hello world!'};
        Chat.Spaces.Messages.create(message, spaceName);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create message with error %s', err.message);
      }
    }

โพสต์ข้อความพร้อมข้อมูลเข้าสู่ระบบของแอป

ตัวอย่างต่อไปนี้แสดงวิธีโพสต์ข้อความไปยัง พื้นที่ทำงานของ Chat ในนามของแอป การใช้บริการ Chat ขั้นสูงกับบัญชีบริการไม่จำเป็นต้อง ระบุขอบเขตการให้สิทธิ์ใน appsscript.json โปรดดูรายละเอียดเกี่ยวกับการตรวจสอบสิทธิ์ด้วยบัญชีบริการที่หัวข้อตรวจสอบสิทธิ์ในฐานะแอป Google Chat

advanced/chat.gs
/**
 * Posts a new message to the specified space on behalf of the app.
 * @param {string} spaceName The resource name of the space.
 */
function postMessageWithAppCredentials(spaceName) {
  try {
    // See https://developers.google.com/chat/api/guides/auth/service-accounts
    // for details on how to obtain a service account OAuth token.
    const appToken = getToken_();
    const message = {'text': 'Hello world!'};
    Chat.Spaces.Messages.create(
        message,
        spaceName,
        {},
        // Authenticate with the service account token.
        {'Authorization': 'Bearer ' + appToken});
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to create message with error %s', err.message);
  }
}

รับพื้นที่ทำงาน

ตัวอย่างต่อไปนี้แสดงวิธีรับข้อมูลเกี่ยวกับพื้นที่แชท

  1. เพิ่มขอบเขตการให้สิทธิ์ chat.spaces.readonly ลงในไฟล์ appsscript.json ของโปรเจ็กต์ Apps Script โดยทำดังนี้

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.spaces.readonly"
    ]
    
  2. เพิ่มฟังก์ชันเช่นฟังก์ชันนี้ลงในโค้ดของโปรเจ็กต์ Apps Script

    advanced/chat.gs
    /**
     * Gets information about a Chat space.
     * @param {string} spaceName The resource name of the space.
     */
    function getSpace(spaceName) {
      try {
        const space = Chat.Spaces.get(spaceName);
        console.log('Space display name: %s', space.displayName);
        console.log('Space type: %s', space.spaceType);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to get space with error %s', err.message);
      }
    }

สร้างพื้นที่ทำงาน

ตัวอย่างต่อไปนี้แสดงวิธีสร้างพื้นที่ใน Chat

  1. เพิ่มขอบเขตการให้สิทธิ์ chat.spaces.create ลงในไฟล์ appsscript.json ของโปรเจ็กต์ Apps Script โดยทำดังนี้

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.spaces.create"
    ]
    
  2. เพิ่มฟังก์ชันเช่นฟังก์ชันนี้ลงในโค้ดของโปรเจ็กต์ Apps Script

    advanced/chat.gs
    /**
     * Creates a new Chat space.
     */
    function createSpace() {
      try {
        const space = {'displayName': 'New Space', 'spaceType': 'SPACE'};
        Chat.Spaces.create(space);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create space with error %s', err.message);
      }
    }

แสดงรายการการเป็นสมาชิก

ตัวอย่างต่อไปนี้แสดงวิธีแสดงรายชื่อสมาชิกทั้งหมดในพื้นที่ทำงานของ Chat

  1. เพิ่มขอบเขตการให้สิทธิ์ chat.memberships.readonly ลงในไฟล์ appsscript.json ของโปรเจ็กต์ Apps Script โดยทำดังนี้

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships.readonly"
    ]
    
  2. เพิ่มฟังก์ชันเช่นฟังก์ชันนี้ลงในโค้ดของโปรเจ็กต์ Apps Script

    advanced/chat.gs
    /**
     * Lists all the members of a Chat space.
     * @param {string} spaceName The resource name of the space.
     */
    function listMemberships(spaceName) {
      let response;
      let pageToken = null;
      try {
        do {
          response = Chat.Spaces.Members.list(spaceName, {
            pageSize: 10,
            pageToken: pageToken
          });
          if (!response.memberships || response.memberships.length === 0) {
            pageToken = response.nextPageToken;
            continue;
          }
          response.memberships.forEach((membership) => console.log(
              'Member resource name: %s (type: %s)',
              membership.name,
              membership.member.type));
          pageToken = response.nextPageToken;
        } while (pageToken);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed with error %s', err.message);
      }
    }

แก้ปัญหา

หากคุณพบ Error 400: invalid_scope พร้อมข้อความแสดงข้อผิดพลาด Some requested scopes cannot be shown แสดงว่าคุณยังไม่ได้ระบุขอบเขตการให้สิทธิ์ใดๆ ในไฟล์ appsscript.json ของโปรเจ็กต์ Apps Script ในกรณีส่วนใหญ่ Apps Script จะกำหนดขอบเขตที่สคริปต์ต้องการโดยอัตโนมัติ แต่เมื่อใช้บริการขั้นสูงของ Chat คุณต้องเพิ่ม ขอบเขตการให้สิทธิ์ที่สคริปต์ใช้ลงในไฟล์ Manifest ของโปรเจ็กต์ Apps Script ด้วยตนเอง ดูการตั้งค่าขอบเขตที่ชัดเจน

หากต้องการแก้ไขข้อผิดพลาด ให้เพิ่มขอบเขตการให้สิทธิ์ที่เหมาะสม ลงในไฟล์ appsscript.json ของโปรเจ็กต์ Apps Script ซึ่งเป็นส่วนหนึ่งของ อาร์เรย์ oauthScopes เช่น หากต้องการเรียกใช้เมธอด spaces.messages.create ให้เพิ่มข้อมูลต่อไปนี้

"oauthScopes": [
  "https://www.googleapis.com/auth/chat.messages.create"
]

ข้อจำกัดและข้อควรพิจารณา

บริการแชทขั้นสูงไม่รองรับสิ่งต่อไปนี้

หากต้องการดาวน์โหลดไฟล์แนบของข้อความหรือเรียกใช้เมธอดตัวอย่างสำหรับนักพัฒนาแอป ให้ใช้ UrlFetchApp แทน