メッセージからリアクションを削除する

このガイドでは、Google Chat API の Reaction リソースの delete メソッドを使用して、メッセージからリアクション(👍?、🚲?、をご参照ください)を削除する方法を説明します。リアクションを削除しても、メッセージは削除されません。

Reaction リソースは、人々がメッセージにリアクションするために使用できる絵文字を表します(👍?、🚲?、feedback など)。

前提条件

Python

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

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • Google Chat API が有効で構成された Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
  • Chat アプリ用に承認が構成されています。リアクションを削除するには、chat.messages.reactions または chat.messages 承認スコープを持つユーザー認証が必要です。

リアクションを削除する

メッセージからリアクションを削除するには、リクエストに以下を渡します。

  • chat.messages.reactions または chat.messages 承認スコープを指定します。
  • Reaction リソースdelete メソッドを呼び出します。
  • name を、削除するリアクションのリソース名に設定します。

次の例では、メッセージから 😃? リアクションを削除します。

Python

  1. 作業ディレクトリに、chat_reaction_delete.py という名前のファイルを作成します。
  2. chat_reaction_delete.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.reactions"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes a reaction to 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().reactions().delete(
    
            # The reaction 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.
            #
            # Replace REACTION with a reaction name.
            # Obtain the reaction name from the reaction resource of Chat API.
            name = 'spaces/SPACE/messages/MESSAGE/reactions/REACTION'
    
        ).execute()
    
    if __name__ == '__main__':
        main()
    
  3. コードで、次のように置き換えます。

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

    python3 chat_reaction_delete.py
    

成功した場合、レスポンスの本文は空になります。これは、リアクションが削除されたことを示します。