ダイレクト メッセージ(DM)スペースを検索する

このガイドでは、SpacefindDirectMessage メソッドを使用する方法について説明します リソースを使用してダイレクト メッセージ(DM)スペースの詳細を取得することもできます。

Space リソース ユーザーと Chat アプリがメッセージを送信し、 ファイルの共有、共同編集を行えますスペースにはいくつかのタイプがあります。

  • ダイレクト メッセージ(DM)とは、2 人のユーザーまたはユーザー間の会話で、 作成することもできます。
  • グループ チャットとは、3 人以上のユーザーと Chat 用アプリ。
  • 名前付きスペースは、メッセージの送信、ファイルの共有、 考えています

認証 アプリの認証 これにより、Chat アプリは Chat 用アプリは Google Chat 内でアクセスできる (例: メンバーになっている DM)。認証 ユーザー認証によって、 アクセスできるようになります。

前提条件

Python

  • Python 3.6 以降
  • pip パッケージ管理ツール
  • 最新の Google クライアント ライブラリ。インストールまたは更新する手順は次のとおりです。 コマンドライン インターフェースで次のコマンドを実行します。
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 以降
  • npm パッケージ管理ツール
  • 最新の Google クライアント ライブラリ。インストールまたは更新する手順は次のとおりです。 コマンドライン インターフェースで次のコマンドを実行します。
    npm install @google-cloud/local-auth @googleapis/chat
    

ダイレクト メッセージを見つける

Google Chat でダイレクト メッセージを検索するには、 リクエスト:

  • アプリの認証では、 chat.bot 承認スコープ。あり ユーザー認証、 承認スコープ chat.spaces.readonly または chat.spaces を指定します。
  • 呼び出し findDirectMessage メソッド User リソースで、name を渡します。 返信することもできます。あり ユーザー認証、 このメソッドは、呼び出し元のユーザーと指定されたユーザーの間の DM を返します。あり アプリ認証の場合 呼び出し元アプリと指定されたユーザーの間の DM を返します。
  • 人間のユーザーをスペースのメンバーとして追加するには、users/{user} を指定します。 {user} は、サービスの {person_id} または person People API から取得する、または user ディレクトリ API で管理できます。たとえば、People API のユーザーが resourceName people/123456789 の場合は、スペースにユーザーを追加するには、 member.nameusers/123456789 のメンバーシップ。

ユーザー認証を使用したダイレクト メッセージを検索する

Google Chat で送信されたダイレクト メッセージ ユーザー認証:

Python

  1. 作業ディレクトリに、chat_space_find_dm_user.py という名前のファイルを作成します。
  2. 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()
    
  3. コード内の USER を、nameUser

  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_space_find_dm_user.py
    

Node.js

  1. 作業ディレクトリに、先ほど作成した find-direct-message-space.js

  2. 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);
    
  3. コード内の USER を、nameUser

  4. 作業ディレクトリでサンプルを実行します。

    node find-direct-message-space.js
    

Chat API は、メッセージに対して Space 詳細情報が記載されます。

アプリの認証を使用したダイレクト メッセージを検索する

Google Chat で送信されたダイレクト メッセージ アプリの認証:

Python

  1. 作業ディレクトリに、chat_space_find_dm_app.py という名前のファイルを作成します。
  2. 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)
    
  3. コード内の USER を、nameUser

  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_space_find_dm_app.py
    

Node.js

  1. 作業ディレクトリに、先ほど作成した app-find-direct-message-space.js

  2. 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);
    
  3. コード内の USER を、nameUser

  4. 作業ディレクトリでサンプルを実行します。

    node app-find-direct-message-space.js
    

Chat API は、メッセージに対して 指定した DM の詳細を示す Space