刪除聊天室

本指南說明如何使用 Google Chat API 的 Space 資源中的 delete() 方法,在不再需要具名聊天室時將其刪除。刪除聊天室也會一併刪除所有內容,包括訊息和附件。

如果您是 Google Workspace 管理員,可以呼叫 delete() 方法,刪除 Google Workspace 機構中的任何具名聊天室。

Space資源代表使用者和 Chat 應用程式可傳送訊息、共用檔案及協作的空間。聊天室分為以下幾種類型:

  • 即時訊息 (DM) 是指兩位使用者之間,或使用者與 Chat 應用程式之間的對話。
  • 群組對話是指三位以上使用者和即時通訊應用程式之間的對話。
  • 具名聊天室是持續存在的空間,可供使用者傳送訊息、分享檔案及協作。

必要條件

Node.js

  • Google Chat 聊天室。如要使用 Google Chat API 建立聊天室,請參閱「建立聊天室」。如要在 Chat 中建立群組,請參閱說明中心文件

以使用者身分刪除具名空間

如要使用使用者驗證刪除 Google Chat 中的現有聊天室,請在要求中傳遞下列項目:

  • 指定 chat.delete 授權範圍。
  • 呼叫 DeleteSpace() 方法。
  • 傳遞要刪除的空間的 name

如要刪除聊天室,請按照下列步驟操作:

Node.js

chat/client-libraries/cloud/delete-space-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.delete'];

// This sample shows how to delete a space 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 here
    name: 'spaces/SPACE_NAME'
  };

  // Make the request
  const response = await chatClient.deleteSpace(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

如要執行這個範例,請將 SPACE_NAME 替換為空間的 name 欄位中的 ID。您可以呼叫 ListSpaces() 方法,或從空間的網址取得 ID。

以 Chat 應用程式身分刪除已命名的聊天室

應用程式驗證需要管理員核准一次。

使用應用程式驗證時,您只能刪除由 Chat 應用程式建立的聊天室。

如要使用應用程式驗證刪除 Google Chat 中的現有聊天室,請在要求中傳遞下列項目:

建立 API 金鑰

如要呼叫開發人員預先發布版 API 方法,您必須使用非公開的開發人員預先發布版 API 探索文件。如要驗證要求,您必須傳遞 API 金鑰。

如要建立 API 金鑰,請開啟應用程式的 Google Cloud 專案,然後執行下列操作:

  1. 在 Google Cloud 控制台中,依序前往「選單」圖示 >「API 和服務」 >「憑證」

    前往「憑證」

  2. 依序按一下「建立憑證」>「API 金鑰」
  3. 系統會顯示新的 API 金鑰。
    • 按一下「複製」圖示 ,即可複製 API 金鑰,在應用程式的程式碼中使用。您也可以在專案憑證的「API 金鑰」部分找到 API 金鑰。
    • 按一下「限制金鑰」,即可更新進階設定並限制 API 金鑰的使用方式。詳情請參閱「套用 API 金鑰限制」一文。

編寫呼叫 Chat API 的指令碼

如要刪除聊天室,請按照下列步驟操作:

Python

  1. 在工作目錄中,建立名為 chat_space_delete_app.py 的檔案。
  2. chat_space_delete_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.delete"]
    
    def main():
        '''
        Authenticates with Chat API using app authentication,
        then deletes the specified space.
        '''
    
        # 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().delete(
    
              # The space to delete.
              #
              # Replace SPACE with a space name.
              # Obtain the space name from the spaces resource of Chat API,
              # or from a space's URL.
              name='spaces/SPACE'
    
          ).execute()
    
        # Print Chat API's response in your command line interface.
        # When deleting a space, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在程式碼中,請替換下列項目:

    • API_KEY:您建立的 API 金鑰,用於建構 Chat API 的服務端點。

    • SPACE,您可透過 Chat API 的 spaces.list 方法或聊天室網址取得聊天室名稱。

  4. 在工作目錄中,建構並執行範例:

    python3 chat_space_delete_app.py

如果成功,回應主體會留白,表示空間已刪除。

以 Google Workspace 管理員身分刪除具名聊天室

如果您是 Google Workspace 管理員,可以呼叫 DeleteSpace() 方法,刪除 Google Workspace 機構中的任何具名空間。

如要以 Google Workspace 管理員身分呼叫這個方法,請按照下列步驟操作:

  • 使用使用者驗證呼叫方法,並指定支援使用管理員權限呼叫方法的授權範圍
  • 在要求中,將查詢參數 useAdminAccess 指定為 true

如需更多資訊和範例,請參閱「以 Google Workspace 管理員身分管理 Google Chat 聊天室」。