本指南介绍了如何对以下资源的 Message
资源使用 get
方法:
使用 Google Chat API 返回有关文本或卡片消息的详细信息。
通过
Message
资源
代表
文本
或
卡片
消息。您可以
在 Google Chat API 中通过调用 create
、get
、update
或 delete
消息
相应的方法有关短信和卡片消息的详情,请参阅
Google Chat 消息概览。
前提条件
Python
- Business 或 Enterprise 有权访问以下内容的 Google Workspace 账号: Google Chat。
- Python 3.6 或更高版本
- pip 软件包管理工具
- 最新的 Google 客户端库。若要安装或更新这些应用
在命令行界面中运行以下命令:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- 设置您的环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,指定一个名称, 图标和说明。
- 根据您希望在 Google Chat API 请求中进行身份验证的方式创建访问凭据:
- 如需以 Chat 用户身份进行身份验证,请按以下步骤操作:
创建 OAuth 客户端 ID
凭据,然后将凭据保存为名为
client_secrets.json
复制到您的本地目录。 - 如需以 Chat 应用的身份进行身份验证,请执行以下操作: 创建服务账号 凭据。
- 如需以 Chat 用户身份进行身份验证,请按以下步骤操作:
创建 OAuth 客户端 ID
凭据,然后将凭据保存为名为
- 选择授权范围取决于您是想以用户身份还是 Chat 应用。
接收通过用户身份验证的消息
使用 用户身份验证, 请在请求中传递以下内容:
- 指定
chat.messages.readonly
或chat.messages
授权范围。 - 调用
get
方法 在Message
资源。 - 将
name
设置为要获取的消息的资源名称。
以下示例使用 用户身份验证:
Python
- 在您的工作目录中,创建一个名为
chat_message_get_user.py
的文件。 在
chat_message_get_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.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then gets 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().get( # The message to get. # # 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 message. print(result) if __name__ == '__main__': main()
在代码中进行以下替换:
SPACE
:聊天室名称,您可以从中获取spaces.list
方法 或通过聊天室网址发送。MESSAGE
:消息名称,您可以获取 从异步创建消息后返回的响应正文中 或者通过 自定义名称 分配给消息。
在您的工作目录中,构建并运行该示例:
python3 chat_message_get_user.py
Chat API 会返回
Message
用于详细说明指定消息的内容。
通过应用身份验证接收消息
使用 应用身份验证, 请在请求中传递以下内容:
- 指定
chat.bot
授权范围。 - 调用
get
方法 在Message
资源。 - 将
name
设置为要获取的消息的资源名称。
以下示例使用 应用身份验证:
Python
- 在您的工作目录中,创建一个名为
chat_get_message_app.py
的文件。 在
chat_get_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) # Get a Chat message. result = chat.spaces().messages().get( # The message to get. # # 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. print(result)
在代码中进行以下替换:
SPACE
:聊天室的name
该消息会发布,您可以从spaces.list
方法 或通过聊天室网址发送。MESSAGE
:消息名称,您可以获取 从异步创建消息后返回的响应正文中 或者通过 自定义名称 分配给消息。
在您的工作目录中,构建并运行该示例:
python3 chat_get_message_app.py
Chat API 会返回
Message
用于详细说明指定消息的内容。