このガイドでは、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
と、更新したスペース属性を指定するupdateMask
とbody
を渡します。updateMask
は更新するスペースのアスペクトを指定します。これには以下が含まれます。displayName
: Google Chat UI に表示されるスペースの名前(ユーザー可読形式)を更新します。SPACE
タイプのスペースの表示名の変更、またはGROUP_CHAT
スペースタイプをSPACE
に変更するためのspaceType
マスクも組み込んだ場合にのみサポートされます。GROUP_CHAT
またはDIRECT_MESSAGE
スペースの表示名を更新しようとすると、無効な引数エラーが発生します。spaceType
: スペースのタイプを更新しますが、GROUP_CHAT
のスペースタイプをSPACE
に変更することのみをサポートします。更新マスクにdisplayName
をspaceType
とともに含め、指定したスペースが空でないdisplayName
とSPACE
のスペースタイプになるようにします。既存のスペースにすでにSPACE
タイプがある場合、表示名の更新時に指定されたスペースのspaceType
マスクやSPACE
タイプを含めるかどうかは任意です。他の方法でスペースタイプを更新しようとすると、無効な引数エラーが発生します。spaceDetails
: 説明とルールを含むスペースの詳細。spaceHistoryState
: 組織でユーザーに履歴設定の変更を許可している場合、スペースの履歴を有効または無効にすることができます。他のすべてのフィールドパスとは相互に排他的です。
既存のスペースの 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
リソースのインスタンスを返します。