メッセージのリアクションを一覧表示する

このガイドでは、Google Chat API の Reaction リソースの list メソッドを使用して、メッセージ(👍?、🚲?、背景など)のリアクションを一覧表示する方法について説明します。

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.readonlychat.messages.reactionschat.messages.readonly、または chat.messages 承認スコープを持つユーザー認証が必要です。

リアクションを一覧表示する

メッセージのリアクションを一覧表示するには、リクエストに以下を渡します。

  • chat.messages.reactions.readonlychat.messages.reactionschat.messages.readonly、または chat.messages 承認スコープを指定します。
  • Reaction リソースで [list method]/workspace(/chat/api/reference/rest/v1/spaces.messages.reactions/list) を呼び出します。

次の例では、指定したメッセージのリアクションを一覧表示します。

Python

  1. 作業ディレクトリに、chat_reactions_list.py という名前のファイルを作成します。
  2. chat_reactions_list.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.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists reactions 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().list(
    
            # The message to list reactions to.
            #
            # 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.
            parent = 'spaces/SPACE/messages/MESSAGE'
    
        ).execute()
    
        # Prints details about the created reactions.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードで、次のように置き換えます。

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

    python3 chat_reactions_list.py
    

Chat API は、ページ分けされたリアクションの配列を返します。