このガイドでは、Terraform で updateSpaceReadState
メソッドを使用する方法について説明します。
スペースを既読または未読としてマークするための Google Chat API の SpaceReadState
リソース。
「
SpaceReadState
リソース
特定のリソースに関する詳細を表すシングルトン リソースです。
Google Chat スペースでユーザーが最後に読んだメッセージ。
前提条件
Python
- 企業または大企業 以下にアクセスできる Google Workspace アカウント Google Chat。
- 環境を設定します。
<ph type="x-smartling-placeholder">
- </ph>
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成します。
- Google Chat API を有効にして構成する。名前、 アプリのアイコン、説明を入力します。
- Python Google API クライアント ライブラリ。
- <ph type="x-smartling-placeholder"></ph>
デスクトップ アプリケーション用の OAuth クライアント ID 認証情報を作成するサンプルを実行するには、
で、認証情報を
client_secrets.json
という名前の JSON ファイルとして ディレクトリにあります。
- <ph type="x-smartling-placeholder"></ph> ユーザー認証をサポートする認可スコープを選択します。
Node.js
- 企業または大企業 以下にアクセスできる Google Workspace アカウント Google Chat。
- 環境を設定します。
<ph type="x-smartling-placeholder">
- </ph>
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成します。
- Google Chat API を有効にして構成する。名前、 アプリのアイコン、説明を入力します。
- Node.js Google API クライアント ライブラリ。
- <ph type="x-smartling-placeholder"></ph>
デスクトップ アプリケーション用の OAuth クライアント ID 認証情報を作成するサンプルを実行するには、
で、認証情報を
client_secrets.json
という名前の JSON ファイルとして ディレクトリにあります。
- <ph type="x-smartling-placeholder"></ph> ユーザー認証をサポートする認可スコープを選択します。
Apps Script
- 企業または大企業 以下にアクセスできる Google Workspace アカウント Google Chat。
- 環境を設定します。
<ph type="x-smartling-placeholder">
- </ph>
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成します。
- Google Chat API を有効にして構成する。名前、 アプリのアイコン、説明を入力します。
- スタンドアロンの Apps Script プロジェクトを作成する [高度なチャット サービス] をオンにします。
- <ph type="x-smartling-placeholder"></ph> ユーザー認証をサポートする認可スコープを選択します。
呼び出し元ユーザーのスペース読み取り状態を更新する
スペース内のユーザーの読み取り状態を更新するには、 リクエスト:
chat.users.readstate
認可スコープを指定します。- 呼び出し
updateSpaceReadState
メソッド 日付SpaceReadState
リソース。 - 取得するスペースの読み取り状態の
name
を渡します。これにはユーザー ID または スペース ID が含まれます。スペースの読み取り状態の取得は、読み取りの取得のみをサポートします。 ステータス(発信元のユーザーの状態)は、 次のとおりです。 <ph type="x-smartling-placeholder">- </ph>
me
エイリアス。例:users/me/spaces/SPACE/spaceReadState
。- 呼び出し元のユーザーの Workspace メールアドレス。次に例を示します。
users/user@example.com/spaces/SPACE/spaceReadState
- 呼び出し元ユーザーのユーザー ID。次に例を示します。
users/USER/spaces/SPACE/spaceReadState
- スペース読み取り状態の要素を指定する
updateMask
を渡します。 update は、次のフィールドパスをサポートしています。 <ph type="x-smartling-placeholder">- </ph>
lastReadTime
: ユーザーのスペース読み取り状態が更新された時刻。 通常これは、最後に読み取られたメッセージのタイムスタンプか、 またはユーザーが指定したタイムスタンプで、 選択します。lastReadTime
が最新のメッセージ作成時間より前の場合、 UI では、スペースが未読として表示されます。スペースを既読にするには、lastReadTime
を、最後に作成されたメッセージより後の任意の値(大きい値) あります。lastReadTime
は、最新のメッセージ作成時刻と一致するように強制されます。 スペースの読み取り状態は、スペースの読み取り状態のみに影響し、 スペースの最上位の会話に表示されます。スレッド内の返信は このタイムスタンプの影響を受けず、代わりにスレッドの読み取り状態に依存します。
次の例では、呼び出し元ユーザーのスペース読み取り状態を更新します。
Python
- 作業ディレクトリに、先ほど作成した
chat_spaceReadState_update.py
。 chat_spaceReadState_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.users.readstate"] def main(): ''' Authenticates with Chat API via user credentials, then updates the space read state for the calling user. ''' # 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.users().spaces().updateSpaceReadState( # The space read state to update. # # Replace USER with the calling user's ID, Workspace email, # or the alias me. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. name='users/me/spaces/SPACE/spaceReadState', updateMask='lastReadTime', body={'lastReadTime': f'{datetime.datetime(2000, 1, 3).isoformat()}Z'} ).execute() # Prints the API's response. print(result) if __name__ == '__main__': main()
コードの次のように置き換えます。
SPACE
: スペース名。 こちらのspaces.list
メソッド スペースの URL から取得できます。
作業ディレクトリでサンプルをビルドして実行します。
python3 chat_spaceReadState_update.py
Node.js
- 作業ディレクトリに、先ほど作成した
chat_spaceReadState_update.js
。 chat_spaceReadState_update
に次のコードを含めます。const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Authenticates with Chat API via user credentials, * then updates the space read state for the calling user. * @return {!Promise<!Object>} */ async function updateSpaceReadState() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.users.readstate', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); /** * Build a service endpoint for Chat API. */ const chatClient = await chat.chat({version: 'v1', auth: authClient}); /** * Use the service endpoint to call Chat API. */ return await chatClient.users.spaces.updateSpaceReadState({ /** * The space read state to update. * * Replace USER with the calling user's ID, Workspace email, * or the alias me. * * Replace SPACE with a space name. * Obtain the space name from the spaces resource of Chat API, * or from a space's URL. */ name: 'users/me/spaces/SPACE/spaceReadState', updateMask: 'lastReadTime', requestBody: { lastReadTime: '{datetime.datetime(2000, 1, 3).isoformat()}Z' } }); } /** * Use the service endpoint to call Chat API. */ getSpaceReadState().then(console.log);
コードの次のように置き換えます。
SPACE
: スペース名。 こちらのspaces.list
メソッド スペースの URL から取得できます。
作業ディレクトリでサンプルをビルドして実行します。
node chat_spaceReadState_update.js
Apps Script
この例では、API 呼び出しを使用して Chat API を呼び出し、 高度なチャット サービス。
chat.users.readstate
承認スコープを以下に追加します。 Apps Script プロジェクトのappsscript.json
ファイル:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate" ]
このような関数を Apps Script プロジェクトの コード:
/** * Authenticates with Chat API via user credentials, * then updates the space read state for the calling user. * @param {string} spaceReadStateName The resource name of the space read state. */ function updateSpaceReadState(spaceReadStateName) { try { const time = new Date('January 1, 2000')).toJSON(); const body = {'lastReadTime': time}; Chat.Users.Spaces.updateSpaceReadState(spaceReadStateName, body); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to update read state with error %s', err.message); } }
Google Chat API は、指定されたスペースの読み取り状態を更新して、
Pod の
SpaceReadState
リソース。