このガイドでは、次の Message
リソースで delete
メソッドを使用する方法について説明します。
テキストやカード メッセージを削除する。
「
Message
リソース
は、
テキスト
または
カード
Google Chat で管理できます。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-oauthlib google-auth
- Google Chat API が有効で構成されている Google Cloud プロジェクト。手順については、以下をご覧ください。 Google Chat アプリを作成します。
Chat 用アプリに認可が構成されました。削除しています メッセージは以下の両方の認証方法をサポートしています。
- ユーザー認証、
権限スコープが
chat.messages
であり、メッセージを削除できる そのユーザーが作成したプロパティです。スペースの管理者は、 他のメッセージを削除する。詳細については、次をご覧ください: 詳しくは、スペースの管理者の役割についてをご覧ください。 - アプリの認証、
ロールを持つユーザーによる削除は、
chat.bot
認証スコープ そのサービス アカウントを使用してアクセスできます。
- ユーザー認証、
権限スコープが
ユーザー認証を使用したメールを削除する
ユーザー認証が適用されたメールを削除するには: リクエストに以下を渡します。
chat.messages
認可スコープを指定します。- 呼び出し
delete
メソッド 日付Message
リソース。 name
は、削除するメッセージのリソース名に設定します。
次の例では、メッセージに ユーザー認証:
Python
- 作業ディレクトリに、先ほど作成した
chat_message_delete_user.py
。 chat_message_delete_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"] def main(): ''' Authenticates with Chat API via user credentials, then deletes 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().delete( # The message 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. name = 'spaces/SPACE/messages/MESSAGE' ).execute() # Prints response to the Chat API call. # When deleting a message, the response body is empty. print(result) if __name__ == '__main__': main()
コードの次のように置き換えます。
SPACE
: スペース名。spaces.list
メソッド スペースの URL から取得できます。MESSAGE
: メッセージ名。取得して取得できます。 非同期でメッセージを作成した後に返されるレスポンス本文から Chat API、または カスタム名 自動的に割り当てられます。
作業ディレクトリでサンプルをビルドして実行します。
python3 chat_message_delete_user.py
成功した場合、レスポンスの本文は空になります。これは、メッセージが 削除されました。
アプリの認証を使用したメッセージの削除
メッセージを削除するには、 アプリ認証を使う場合は、 追加します。
chat.bot
認可スコープを指定します。- 呼び出し
delete
メソッドMessage
リソースに対する権限。 name
は、削除するメッセージのリソース名に設定します。
次の例では、メッセージに アプリの認証:
Python
- 作業ディレクトリに、先ほど作成した
chat_delete_message_app.py
。 chat_delete_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) # Delete a Chat message. result = chat.spaces().messages().delete( # The message 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. name='spaces/SPACE/messages/MESSAGE' ).execute() # Print Chat API's response in your command line interface. # When deleting a message, the response body is empty. print(result)
コードの次のように置き換えます。
SPACE
: スペースのname
表示されます。この情報は、spaces.list
メソッド スペースの URL から取得できます。MESSAGE
: メッセージ名(取得可能) 非同期でメッセージを作成した後に返されるレスポンス本文から Chat API、または カスタム名 自動的に割り当てられます。
作業ディレクトリでサンプルをビルドして実行します。
python3 chat_delete_message_app.py
成功した場合、レスポンスの本文は空になります。これは、メッセージが 削除されました。