列出对消息的回应

本指南介绍了如何对 Reaction 资源使用 list 方法 来列出对消息的回应,例如 👍?、🚲? 和 🌞?。

通过 Reaction 资源 代表用户可以用来回应信息的表情符号,例如 👍?、🚲?, 和 🌞?。

前提条件

Python

  • Python 3.6 或更高版本
  • pip 软件包管理工具
  • 最新的 Google 客户端库。若要安装或更新这些应用 在命令行界面中运行以下命令:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

列出回应

如需列出对消息的回应,请在请求中传递以下内容:

  • 指定 chat.messages.reactions.readonlychat.messages.reactionschat.messages.readonlychat.messages 授权范围。
  • 调用 [list 方法]/workspace(/chat/api/reference/rest/v1/spaces.messages.reactions/list) 在 Reaction 资源

以下示例列出了对指定消息的回应:

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:聊天室名称,您可以从中获取 spaces.list 方法 或通过聊天室网址发送。
    • MESSAGE:消息名称,您可以获取 从异步创建消息后返回的响应正文中 或者通过 自定义名称 分配给消息。
  4. 在您的工作目录中,构建并运行该示例:

    python3 chat_reactions_list.py
    

Chat API 会返回分页的回应数组。