尋找即時訊息 (DM) 聊天室

本指南說明如何在 Space 上使用 findDirectMessage 方法 該 Google Chat API 資源,取得即時訊息 (DM) 聊天室的詳細資料。

Space 項資源 代表使用者和 Chat 擴充應用程式可以傳送訊息的地方 分享檔案及協同合作聊天室分為以下幾類:

  • 即時訊息 (DM) 是指兩位使用者或使用者之間的對話, Chat 應用程式。
  • 群組通訊是 3 位以上使用者之間的對話, Chat 擴充應用程式。
  • 已命名的聊天室是使用者傳送訊息、共用檔案、 這項原則著重於透過對開放式探究 嚴謹治學、誠信與合作的堅持來達到上述目標

使用以下憑證進行驗證: 應用程式驗證 可讓 Chat 應用程式接收 在 Google Chat 中可存取 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 擷取的 ID user 。舉例來說,如果 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 Google Chat 中的 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 Google Chat 中的 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 Google Chat 中的 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 Google Chat 中的 User

  4. 在工作目錄中執行範例:

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

Chat API 會傳回 Space,用於提供指定即時訊息的詳細資訊。