为消息添加回应

本指南介绍了如何在 Google Chat API 的 Reaction 资源中使用 create 方法添加对消息的回应,例如 👍?、🚲? 和 🌞?。

Reaction 资源表示可供用户用于回应消息的表情符号,例如 👍?、🚲? 和 🌞?。

前提条件

Python

  • Python 3.6 或更高版本
  • pip 软件包管理工具
  • 适用于 Python 的最新 Google 客户端库。如需安装或更新这些软件包,请在命令行界面中运行以下命令:

    pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
    
  • 已发布的 Chat 应用。如需创建和发布 Chat 应用,请参阅构建 Google Chat 应用

  • 为 Chat 应用配置授权。创建回应需要通过 chat.messages.reactions.createchat.messages.reactionschat.messages 授权范围进行用户身份验证

为消息添加回应

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

  • 指定 chat.messages.reactions.createchat.messages.reactionschat.messages 授权范围。
  • Reaction 资源调用 create 方法
  • parent 设置为要回应的消息的资源名称。
  • body(请求正文)设置为 Reaction 的实例,其中 unicode 字段是由 Unicode 字符串表示的标准表情符号。

以下示例使用 😀? 表情符号响应消息:

Python

  1. 在您的工作目录中,创建一个名为 chat_reaction_create.py 的文件。
  2. chat_reaction_create.py 中添加以下代码:

    import os.path
    
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    
    # 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.create"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then creates 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().create(
    
            # The message to create a reaction 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',
    
            # The reaction to the message.
            body = {
    
                'emoji': {
    
                    # A standard emoji represented by a unicode string.
                    'unicode': '😀'
                }
    
            }
    
        ).execute()
    
        # Prints details about the created reaction.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中,替换以下内容:

    • SPACE:消息所在聊天室的 name,可通过 Chat API 中的 spaces.list 方法或聊天室的网址获取。

    • MESSAGE:消息名称,您可以从使用 Chat API 异步创建消息后返回的响应正文中获取,也可以从创建消息时为消息指定的自定义名称获取该名称。

  4. 在您的工作目录中,构建并运行示例:

    python3 chat_reaction_create.py
    

Chat API 会返回一个 Reaction 实例,详细说明所创建的回应。