本指南說明如何對 Google Chat API 的 Message
資源使用 get
方法,以傳回文字或卡片訊息的詳細資料。
Message
資源代表 Google Chat 中的文字或資訊卡訊息。您可以透過呼叫對應的方法,在 Google Chat API 中 create
、get
、update
或 delete
訊息。如要進一步瞭解文字和資訊卡訊息,請參閱 Google Chat 訊息總覽。
必要條件
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.readonly
或chat.messages
授權範圍。 - 呼叫
Message
資源上的get
方法。 - 將
name
設為要取得的訊息資源名稱。
以下範例會收到含有使用者驗證的訊息:
Python
- 在工作目錄中,建立名為
chat_message_get_user.py
的檔案。 在
chat_message_get_user.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.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 created membership. print(result) if __name__ == '__main__': main()
在程式碼中,替換以下內容:
SPACE
:可從 Chat API 的spaces.list
方法或聊天室網址取得的聊天室名稱。MESSAGE
:訊息名稱,您可以在使用 Chat API 以非同步方式建立訊息後,從傳回的回應主體中取得,或在建立訊息時指派給訊息的自訂名稱。
在工作目錄中建構並執行範例:
python3 chat_message_get_user.py
Chat API 會傳回 Message
的執行個體,其中包含指定訊息的詳細資料。
使用應用程式驗證功能接收訊息
如要透過應用程式驗證取得訊息的詳細資料,請在要求中傳遞以下內容:
- 指定
chat.bot
授權範圍。 - 呼叫
Message
資源上的get
方法。 - 將
name
設為要取得的訊息資源名稱。
以下範例會取得含有應用程式驗證的訊息:
Python
- 在工作目錄中,建立名為
chat_get_message_app.py
的檔案。 在
chat_get_message_app.py
中加入下列程式碼:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # 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
,您可以透過 Chat API 的spaces.list
方法或聊天室網址取得。MESSAGE
:訊息名稱,您可以在使用 Chat API 以非同步方式建立訊息後,從傳回的回應主體中取得,或在建立訊息時指派給訊息的自訂名稱。
在工作目錄中建構並執行範例:
python3 chat_get_message_app.py
Chat API 會傳回 Message
的執行個體,其中包含指定訊息的詳細資料。