このガイドでは、Google Chat API の Message
リソースの patch
メソッドを使用して、スペースのテキストまたはカード メッセージを更新する方法について説明します。メッセージを更新して、メッセージ属性(内容やカードのコンテンツなど)を変更します。カード メッセージの前にテキスト メッセージを追加したり、テキスト メッセージにカードを追加したりすることもできます。
Chat API は update
メソッドもサポートしていますが、patch
メソッドは PATCH
HTTP リクエストを使用するのに対し、update
は PUT
HTTP リクエストを使用するため、呼び出すことを強くおすすめします。詳細については、AIP-134 の PATCH
と PUT
のセクションをご覧ください。
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-oauthlib google-auth
- Google Chat API が有効で構成された Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
Chat アプリ用に構成された認可:
- テキスト メッセージの更新では、次の両方の認証方法がサポートされています。
- カード メッセージを更新するには、
chat.bot
承認スコープを使用したアプリ認証が必要です。
ユーザー認証を使用して、テキスト メッセージを更新したり、カード メッセージの前にテキスト メッセージを追加したりできます
ユーザー認証を使用してテキスト メッセージを更新するには、リクエストで次のものを渡します。
chat.messages
承認スコープ。- 更新するメッセージの
name
。 updateMask='text'
- 更新されたメッセージを指定する
body
。
更新されたメッセージがカード メッセージの場合、テキスト メッセージはカード メッセージの先頭に付加されます(引き続き表示されます)。
テキスト メッセージを更新する方法、またはユーザー認証を使用してカード メッセージの前にテキスト メッセージを追加する方法は次のとおりです。
Python
- 作業ディレクトリに
chat_update_text_message_user.py
という名前のファイルを作成します。 chat_update_text_message_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 updates 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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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', updateMask='text', body={'text': 'Updated message!'} ).execute() # Prints details about the updated message. print(result) if __name__ == '__main__': main()
コードで、次のように置き換えます。
SPACE
: スペース名。Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。MESSAGE
: メッセージ名。Chat API と非同期でメッセージを作成した後に返されたレスポンスの本文から取得するか、作成時にメッセージに割り当てられたカスタム名を使用して取得できます。
作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_update_text_message_user.py
アプリ認証でテキスト メッセージを更新したり、カード メッセージの前にテキスト メッセージを追加したりできます
アプリ認証を使用してテキスト メッセージを更新するには、リクエストに次の内容を渡します。
chat.bot
承認スコープ。- 更新するメッセージの
name
。 updateMask='text'
- 更新されたメッセージを指定する
body
。
更新されたメッセージがカード メッセージの場合、テキスト メッセージはカード メッセージの前に追加されます(引き続き表示されます)。
テキスト メッセージをテキスト メッセージに更新する方法や、アプリ認証を使用してカード メッセージの前にテキスト メッセージを追加する方法は次のとおりです。
Python
- 作業ディレクトリに
chat_update_text_message_app.py
という名前のファイルを作成します。 chat_update_text_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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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', updateMask='text', body={'text': 'Updated message!'} ).execute() # Print Chat API's response in your command line interface. print(result)
コードで、次のように置き換えます。
SPACE
: スペース名。Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。MESSAGE
: メッセージ名。Chat API と非同期でメッセージを作成した後に返されたレスポンスの本文から取得するか、作成時にメッセージに割り当てられたカスタム名を使用して取得できます。
作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_update_text_message_app.py
カード メッセージを更新する、テキスト メッセージにカード メッセージを追加する
カード メッセージを更新するには、リクエストに次の内容を渡します。
chat.bot
承認スコープ。カード メッセージを更新するには、アプリの認証が必要です。- 更新するメッセージの
name
。 updateMask='cardsV2'
- 更新されたメッセージを指定する
body
。
更新されたメッセージがテキスト メッセージの場合、テキスト メッセージにカードが追加されます(引き続き表示されます)。更新されたメッセージ自体がカードの場合、表示されるカードが更新されます。
メッセージをカード メッセージに更新する方法は次のとおりです。
Python
- 作業ディレクトリに
chat_update_card_message.py
という名前のファイルを作成します。 chat_update_card_message.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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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', updateMask='cardsV2', body= { 'cardsV2': [{ 'cardId': 'updateCardMessage', 'card': { 'header': { 'title': 'An Updated Card Message!', 'subtitle': 'Updated with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() # Print Chat API's response in your command line interface. print(result)
コードで、次のように置き換えます。
SPACE
: スペース名。Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。MESSAGE
: メッセージ名。Chat API と非同期でメッセージを作成した後に返されたレスポンスの本文から取得するか、作成時にメッセージに割り当てられたカスタム名を使用して取得できます。
作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_update_card_message.py
Chat API は、更新されたメッセージの詳細を示す Message
のインスタンスを返します。
複数のフィールドパスを含むメッセージを同時に更新する
メッセージが更新されると、一度に複数のメッセージ フィールド パスを更新できます。たとえば、メッセージの更新リクエストでは、text
フィールドパスと cardsv2
フィールドパスの変更を同時に指定できます。これにより、メッセージのテキストとカードの両方が更新されます。メッセージにテキストのみが含まれ、カードが含まれていない場合は、カードがメッセージに追加されます。サポートされているフィールドパスの詳細については、updateMask
パラメータをご覧ください。
ユーザー認証を使用してメッセージの text
と card
の両方を更新するには、リクエストに次の内容を渡します。
chat.messages
承認スコープ。- 更新するメッセージの
name
。 updateMask
。更新するメッセージ フィールド パスをカンマで区切って指定します(updateMask='text', 'cardsV2'
)。更新されたメッセージを指定する
body
(更新されたすべてのフィールドパスを含む)。
ユーザー認証を使用してメッセージ内の text
フィールドパスと cardsV2
フィールドパスを更新する方法は次のとおりです。
Python
- 作業ディレクトリに
chat_update_text_message_user.py
という名前のファイルを作成します。 chat_update_text_message_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 updates 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) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # 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', updateMask='text,cardsV2', body= {'text': 'Updated message!', 'cardsV2': [{ 'cardId': 'updateCardMessage', 'card': { 'header': { 'title': 'An Updated Card Message!', 'subtitle': 'Updated with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() # Prints details about the updated message. print(result) if __name__ == '__main__': main()
コードで、次のように置き換えます。
SPACE
: スペース名。Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。MESSAGE
: メッセージ名。Chat API と非同期でメッセージを作成した後に返されたレスポンスの本文から取得するか、作成時にメッセージに割り当てられたカスタム名を使用して取得できます。
作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_update_text_message_user.py