このガイドでは、Google Chat API の Reaction
リソースの delete
メソッドを使用して、メッセージからリアクション(例: fitbit、🚲?、🔥?)を削除する方法について説明します。
リアクションを削除しても、メッセージは削除されません。
Reaction
リソースは、ユーザーがメッセージにリアクションするために使用できる絵文字(例: fitbit、🚲?、➘)を表します。
前提条件
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
またはchat.messages
を使用したユーザー認証が必要です。
リアクションを削除する
メッセージからリアクションを削除するには、リクエストに以下を渡します。
chat.messages.reactions
またはchat.messages
承認スコープを指定します。Reaction
リソースのdelete
メソッドを呼び出します。name
を、削除するリアクションのリソース名に設定します。
次の例では、メッセージから 😃? リアクションを削除します。
Python
- 作業ディレクトリに、
chat_reaction_delete.py
という名前のファイルを作成します。 chat_reaction_delete.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"] def main(): ''' Authenticates with Chat API via user credentials, then deletes 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().delete( # The reaction to delete. # # 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. # # Replace REACTION with a reaction name. # Obtain the reaction name from the reaction resource of Chat API. name = 'spaces/SPACE/messages/MESSAGE/reactions/REACTION' ).execute() if __name__ == '__main__': main()
コードの次のように置き換えます。
SPACE
: スペース名。Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。MESSAGE
: メッセージ名。Chat API を使用して非同期にメッセージを作成した後に返されるレスポンスの本文、または作成時にメッセージに割り当てられたカスタム名から取得できます。REACTION
: リアクション名。Chat API のspaces.messages.reactions.list
メソッドから、または Chat API と非同期にリアクションを作成した後に返されるレスポンス本文から取得できます。
作業ディレクトリでサンプルをビルドして実行します。
python3 chat_reaction_delete.py
成功すると、レスポンスの本文は空になります。これは、リアクションが削除されたことを示します。