メッセージを削除する

このガイドでは、Google Chat API の Message リソースの delete メソッドを使用して、テキストまたはカード メッセージを削除する方法について説明します。

Message リソースは、Google Chat のテキスト メッセージまたはカード メッセージを表します。Google Chat API でメッセージを creategetupdatedelete のいずれかにするには、対応するメソッドを呼び出します。テキスト メッセージとカード メッセージの詳細については、Google Chat メッセージの概要をご覧ください。

前提条件

Python

  • Python 3.6 以降
  • pip パッケージ管理ツール
  • Python 用の最新の Google クライアント ライブラリ。これらをインストールまたは更新するには、コマンドライン インターフェースで次のコマンドを実行します。

    pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
    
  • Google Chat API が有効で構成された Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
  • Chat アプリ用に構成された認可。メッセージを削除すると、次の両方の認証方法がサポートされます。

    • chat.messages 認証スコープを持つユーザー認証は、そのユーザーが作成したメッセージを削除できます。スペースの管理者は、他のメッセージを削除できる場合があります。詳しくは、スペースの管理者の役割についてをご覧ください。
    • chat.bot 認証スコープを持つアプリ認証は、サービス アカウントを使用してそのアプリによって作成されたメッセージを削除できます。

ユーザー認証を使用したメッセージを削除する

ユーザー認証を使用してメッセージを削除するには、リクエストに次の内容を渡します。

  • chat.messages 承認スコープを指定します。
  • Message リソースdelete メソッドを呼び出します。
  • name を、削除するメッセージのリソース名に設定します。

次の例では、ユーザー認証を使用してメッセージを削除します。

Python

  1. 作業ディレクトリに chat_message_delete_user.py という名前のファイルを作成します。
  2. chat_message_delete_user.py に次のコードを追加します。

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.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.messages"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes a message.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                          'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().messages().delete(
    
            # The message 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.
            #
            # Replace MESSAGE with a message name.
            # Obtain the message name from the response body returned
            # after creating a message asynchronously with Chat REST API.
            name = 'spaces/SPACE/messages/MESSAGE'
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードで、次のように置き換えます。

    • SPACE: スペース名。Chat API の spaces.list メソッドまたはスペースの URL から取得できます。
    • MESSAGE: メッセージ名。Chat API と非同期でメッセージを作成した後に返されたレスポンスの本文から取得するか、作成時にメッセージに割り当てられたカスタム名を使用して取得できます。
  4. 作業ディレクトリで、サンプルをビルドして実行します。

    python3 chat_message_delete_user.py
    

成功した場合、レスポンスの本文は空です。これは、メッセージが削除されたことを示します。

アプリ認証を使用してメッセージを削除する

アプリ認証を使用してメッセージを削除するには、リクエストで次のものを渡します。

  • chat.bot 承認スコープを指定します。
  • Message リソースdelete メソッドを呼び出します。
  • name を、削除するメッセージのリソース名に設定します。

次の例では、アプリ認証を含むメッセージを削除します。

Python

  1. 作業ディレクトリに chat_delete_message_app.py という名前のファイルを作成します。
  2. chat_delete_message_app.py に次のコードを追加します。

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Delete a Chat message.
    result = chat.spaces().messages().delete(
    
      # The message 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.
      #
      # Replace MESSAGE with a message name.
      # Obtain the message name from the response body returned
      # after creating a message asynchronously with Chat REST API.
      name='spaces/SPACE/messages/MESSAGE'
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    # When deleting a message, the response body is empty.
    print(result)
    
  3. コードで、次のように置き換えます。

    • SPACE: メッセージが投稿されているスペースの name。Chat API の spaces.list メソッドまたはスペースの URL から取得できます。
    • MESSAGE: メッセージ名。Chat API と非同期にメッセージを作成した後に返されたレスポンスの本文から取得するか、作成時にメッセージに割り当てられたカスタム名を使用して取得できます。
  4. 作業ディレクトリで、サンプルをビルドして実行します。

    python3 chat_delete_message_app.py
    

成功した場合、レスポンスの本文は空です。これは、メッセージが削除されたことを示します。