このガイドでは、Google Chat API の Space
リソースで patch
メソッドを使用してスペースを更新する方法について説明します。スペースを更新して、スペースに関する属性(ユーザーに表示される表示名、説明、ガイドラインなど)を変更します。
Space
リソースは、ユーザーと Chat アプリがメッセージの送信、ファイルの共有、コラボレーションを行う場所を表します。スペースにはいくつかのタイプがあります。
- ダイレクト メッセージ(DM)は、2 人のユーザー、またはユーザーと Chat アプリ間の会話です。
- グループ チャットとは、3 人以上のユーザーと Chat アプリの間で行われる会話です。
- 名前付きスペースは、メッセージの送信、ファイルの共有、コラボレーションを行う永続的な場所です。
前提条件
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.spaces
認可スコープを使用したユーザー認証が必要です。
Node.js
- Node.js と npm
最新の Node.js 用 Google クライアント ライブラリ。これらをインストールするには、コマンドライン インターフェースで次のコマンドを実行します。
npm install @google-cloud/local-auth @googleapis/chat
- Google Chat API が有効で構成されている Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
Chat 用アプリ用に構成された認可。スペースを更新するには、
chat.spaces
認可スコープを使用したユーザー認証が必要です。
スペースの更新
Google Chat の既存のスペースを更新するには、リクエストに次の内容を渡します。
chat.spaces
認可スコープを指定します。Space
リソースのpatch
メソッドを呼び出します。リクエストでは、スペースname
フィールド、更新する 1 つ以上のフィールドを含むupdateMask
フィールド、更新されたスペース情報を含むbody
を指定します。
表示名、スペースタイプ、履歴の状態などを更新できます。更新できるすべてのフィールドを確認するには、リファレンス ドキュメントをご覧ください。
既存のスペースの spaceDetails
フィールドを更新する方法は次のとおりです。
Python
- 作業ディレクトリに、
chat_space_update.py
という名前のファイルを作成します。 chat_space_update.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.spaces"] def main(): ''' Authenticates with Chat API via user credentials, then updates the specified space description and guidelines. ''' # 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().patch( # The space to update, and the updated space details. # # Replace {space} with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. name='spaces/SPACE', updateMask='spaceDetails', body={ 'spaceDetails': { 'description': 'This description was updated with Chat API!', 'guidelines': 'These guidelines were updated with Chat API!' } } ).execute() # Prints details about the updated space. print(result) if __name__ == '__main__': main()
コードで
SPACE
をスペース名に置き換えます。スペースは、Chat API のspaces.list
メソッドまたはスペースの URL から取得します。作業ディレクトリでサンプルをビルドして実行します。
python3 chat_space_update.py
Node.js
- 作業ディレクトリに、
update-space.js
という名前のファイルを作成します。 update-space.js
に次のコードを含めます。const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Updates a Chat space with the description and guidelines. * @return {!Promise<!Object>} */ async function updateSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.spaces', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.patch({ name: 'spaces/SPACE', updateMask: 'spaceDetails', requestBody: { spaceDetails: { description: 'This description was updated with Chat API!', guidelines: 'These guidelines were updated with Chat API!' }, } }); } updateSpace().then(console.log);
コードで
SPACE
をスペース名に置き換えます。スペースは、Chat API のspaces.list
メソッドまたはスペースの URL から取得します。作業ディレクトリでサンプルを実行します。
node update-space.js
Google Chat API は、更新を反映した Space
リソースのインスタンスを返します。