查找私信 (DM) 聊天室

本指南介绍了如何在 Space 上使用 findDirectMessage 方法 资源,以获取私信 (DM) 聊天室的详细信息。

通过 Space 资源 代表用户和 Chat 扩展应用能够发送消息的位置, 共享文件和协作聊天室分为以下几种类型:

  • 私信 (DM) 是指两位用户或一位用户与 Chat 应用。
  • 群聊是三位或更多用户之间的对话, 聊天应用。
  • 已命名的聊天室是用户永久保留的位置,可供用户发送消息、共享文件 和协作。

身份验证方式 应用身份验证 向聊天应用发送私信时 Chat 应用有权在 Google Chat 中访问 (例如其成员的私信)。身份验证方式 进行用户身份验证时,系统会返回 可以访问哪些文件

前提条件

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.readonlychat.spaces 授权范围。
  • 调用 findDirectMessage 方法 针对 User 资源,传递 name 其他用户所返回的内容包含 用户身份验证, 此方法会返回调用方用户与指定用户之间的私信。包含 应用身份验证,此方法 返回发起调用的应用和指定用户之间的私信。
  • 如需将真人用户添加为聊天室成员,请指定 users/{user},其中 {user}{person_id} person 来自 People API 的 user 目录 API。例如,如果 People API 人员 resourceNamepeople/123456789,您可以通过添加 users/123456789member.name的会员。

查找经过用户身份验证的私信

下面介绍了如何查找私信 用户身份验证

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 替换为 name User

  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 替换为 name User

  4. 在您的工作目录中,运行该示例:

    node find-direct-message-space.js
    

Chat API 会返回 Space 指定私信的详细信息

查找通过应用身份验证的私信

下面介绍了如何查找私信 应用身份验证

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 替换为 name User

  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 替换为 name User

  4. 在您的工作目录中,运行该示例:

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

Chat API 会返回 Space:详细说明指定私信。