このガイドでは、Google Chat API の Space
リソースの findDirectMessage
メソッドを使用して、ダイレクト メッセージ(DM)スペースの詳細を取得する方法について説明します。
Space
リソースは、ユーザーと Chat アプリがメッセージの送信、ファイル共有、コラボレーションを行う場所を表します。スペースには次の種類があります。
- ダイレクト メッセージ(DM)は、2 人のユーザー間の会話、またはユーザーと Chat アプリ間の会話です。
- グループ チャットとは、3 人以上のユーザーと Chat アプリの間で交わされる会話のことです。
- 名前付きスペースは、メッセージの送信、ファイルの共有、共同編集を行う永続的な場所です。
アプリの認証で認証すると、Chat アプリは、Google Chat でアクセスできる DM(メンバーである DM など)を取得できます。ユーザー認証で認証すると、認証されたユーザーがアクセスできる DM が返されます。
前提条件
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 アプリ用に構成された承認です。ダイレクト メッセージの検索では、次の両方がサポートされています。
Node.js
- Node.js と npm
Node.js 用の最新の Google クライアント ライブラリ。これらをインストールするには、コマンドライン インターフェースで次のコマンドを実行します。
npm install @google-cloud/local-auth @googleapis/chat
- Google Chat API が有効で構成された Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
Chat アプリ用に構成された承認です。ダイレクト メッセージの検索では、次の両方がサポートされています。
ダイレクト メッセージを検索する
Google Chat でダイレクト メッセージを検索するには、リクエストで次のものを渡します。
- アプリ認証で、
chat.bot
承認スコープを指定します。ユーザー認証で、chat.spaces.readonly
またはchat.spaces
承認スコープを指定します。 User
リソースでfindDirectMessage
メソッドを呼び出し、DM 内の他のユーザーのname
を渡して返します。ユーザー認証を使用する場合、このメソッドは呼び出し元のユーザーと指定されたユーザーの DM を返します。アプリ認証を使用する場合、このメソッドは呼び出し元のアプリと指定されたユーザーの DM を返します。- 人間のユーザーをスペースのメンバーとして追加するには、
users/{user}
を指定します。ここで、{user}
は People API のperson
の{person_id}
か、Directory API のuser
の ID です。たとえば、People API のユーザーresourceName
がpeople/123456789
の場合、users/123456789
のメンバーシップをmember.name
として追加することで、ユーザーをスペースに追加できます。
ユーザー認証を使用するダイレクト メッセージを検索する
ユーザー認証を使用してダイレクト メッセージを見つける方法は次のとおりです。
Python
- 作業ディレクトリに、
chat_space_find_dm_user.py
という名前のファイルを作成します。 chat_space_find_dm_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.spaces.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then returns details about a specified DM. ''' # 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().findDirectMessage( # The other user in the direct message (DM) to return. # # Replace USER with a user name. name='users/USER' ).execute() # Prints details about the direct message. print(result) if __name__ == '__main__': main()
コードで、
USER
を Google Chat のUser
のname
に置き換えます。作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_space_find_dm_user.py
Node.js
作業ディレクトリに
find-direct-message-space.js
という名前のファイルを作成します。find-direct-message-space.js
に次のコードを追加します。const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Find a direct message Chat space for a user. * @return {!Promise<!Object>} */ async function findDirectMessageSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.spaces.readonly', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.findDirectMessage( {name: 'users/USER'}); } findDirectMessageSpace().then(console.log);
コードで、
USER
を Google Chat のUser
のname
に置き換えます。作業ディレクトリでサンプルを実行します。
node find-direct-message-space.js
Chat API は、指定された DM の詳細を示す Space
のインスタンスを返します。
アプリ認証を使用するダイレクト メッセージを見つける
アプリ認証を使用してダイレクト メッセージを見つける方法は次のとおりです。
Python
- 作業ディレクトリに、
chat_space_find_dm_app.py
という名前のファイルを作成します。 chat_space_find_dm_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) # Use the service endpoint to call Chat API. result = chat.spaces().findDirectMessage( # The other user in the direct message (DM) to return. # # Replace USER with a user name. name='users/USER' ).execute() print(result)
コードで、
USER
を Google Chat のUser
のname
に置き換えます。作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_space_find_dm_app.py
Node.js
作業ディレクトリに
app-find-direct-message-space.js
という名前のファイルを作成します。app-find-direct-message-space.js
に次のコードを追加します。const chat = require('@googleapis/chat'); /** * Find a direct message Chat space for a user. * @return {!Promise<!Object>} */ async function findDirectMessageSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.bot', ]; const auth = new chat.auth.GoogleAuth({ scopes, keyFilename: 'credentials.json', }); const authClient = await auth.getClient(); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.findDirectMessage( {name: 'users/USER'}); } findDirectMessageSpace().then(console.log);
コードで、
USER
を Google Chat のUser
のname
に置き換えます。作業ディレクトリでサンプルを実行します。
node app-find-direct-message-space.js
Chat API は、指定された DM の詳細を示す Space
のインスタンスを返します。