邀請或將使用者、Google 群組或 Google Chat 應用程式新增至聊天室

本指南說明如何針對 membership 資源使用 create 方法 使用 Google Chat API 邀請或新增使用者、Google 群組,或 將 Chat 應用程式傳送到聊天室,也稱為建立 會員。建立成員資格時,如果指定成員具備 已關閉自動接受政策,然後再受邀,且必須接受聊天室 邀請。否則,建立會員方案會新增成員 指定到指定空間

Membership 項資源 代表受邀參加的使用者或 Google Chat 應用程式 屬於或不存在於空格中。

必要條件

Python

  • Python 3.6 以上版本
  • pip 套件管理工具
  • 最新的 Python 專用 Google 用戶端程式庫。安裝或更新 在指令列介面中執行下列指令:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • 已啟用並設定 Google Chat API 的 Google Cloud 專案。詳細步驟請參閱 建構 Google Chat 應用程式
  • 已為 Chat 應用程式設定授權。建立中 必須具備會員資格 使用者驗證 擁有 chat.membershipschat.memberships.app 授權範圍。

Node.js

  • Node.js 與npm
  • Node.js 的最新 Google 用戶端程式庫。如要安裝這些應用程式,請執行 執行下列指令:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • 已啟用並設定 Google Chat API 的 Google Cloud 專案。詳細步驟請參閱 建構 Google Chat 應用程式
  • 已為 Chat 應用程式設定授權。建立中 必須具備會員資格 使用者驗證 擁有 chat.membershipschat.memberships.app 授權範圍。

邀請或新增使用者至聊天室

如要邀請或新增使用者至聊天室,請在 要求:

  • 指定 chat.memberships 授權範圍。
  • create 方法membership 項資源
  • parent 設為要在其中建立成員資格的聊天室資源名稱。
  • member 設為 users/{user},其中 {user} 是您要邀請的對象 為以下使用者建立成員資格:
    • 人物 People API 中的說明。舉例來說,假設 People API 人物 resourceNamepeople/123456789,然後設定 membership.member.nameusers/123456789
    • 使用者
    • 使用者的電子郵件地址。例如 users/222larabrown@gmail.comusers/larabrown@cymbalgroup.com。如果使用者使用 Google 帳戶或 隸屬於其他 Google Workspace 機構,您必須使用他們的 建立一個 Fitbit 帳戶。

以下範例將使用者新增至聊天室:

Python

  1. 在工作目錄中,建立名為 chat_membership_user_create.py
  2. chat_membership_user_create.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.memberships"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then adds a user to a Chat space by creating a membership.
        '''
    
        # 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().members().create(
    
            # The space in which to create a membership.
            parent = 'spaces/SPACE',
    
            # Specify which user the membership is for.
            body = {
              'member': {
                'name':'users/USER',
                'type': 'HUMAN'
              }
            }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 請在程式碼中替換下列內容:

    • SPACE:聊天室名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

    • USER:使用者 ID。

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

    python3 chat_membership_user_create.py
    

Node.js

  1. 在工作目錄中,建立名為 add-user-to-space.js 的檔案。
  2. add-user-to-space.js 中加入下列程式碼:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Adds the user to the Chat space.
    * @return {!Promise<!Object>}
    */
    async function addUserToSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.members.create({
        parent: 'spaces/SPACE',
        requestBody: {member: {name: 'users/USER', type: 'HUMAN'}}
      });
    }
    
    addUserToSpace().then(console.log);
    
  3. 請在程式碼中替換下列內容:

    • SPACE:聊天室名稱,可從 spaces.list 方法 或聊天室網址傳送

    • USER:使用者 ID。

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

    node add-user-to-space.js
    

Chat API 會傳回 membership敬上 這裡會顯示建立的使用者成員資格

邀請或新增 Google 群組至聊天室

如要邀請或將 Google 群組新增至聊天室,請在 要求:

  • 指定 chat.memberships 授權範圍。
  • create 方法membership 項資源
  • parent 設為要在其中建立成員資格的聊天室資源名稱。
  • groupMember 設為 groups/{group},其中 {group} 是您產生的群組 ID 我想為自己建立會員可使用 Cloud Identity API。 舉例來說,如果 Cloud Identity API 會傳回名為 groups/123456789 的群組,然後設定 membership.groupMember.namegroups/123456789

Google 網路論壇無法加入群組通訊或即時訊息 命名的空格以下範例會將群組新增至已命名的聊天室:

Python

  1. 在工作目錄中,建立名為 chat_membership_group_create.py
  2. chat_membership_group_create.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.memberships"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then adds a group to a Chat space by creating a membership.
        '''
    
        # 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().members().create(
    
            # The named space in which to create a membership.
            parent = 'spaces/SPACE',
    
            # Specify which group the membership is for.
            body = {
              'groupMember': {
                'name':'groups/GROUP',
              }
            }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 請在程式碼中替換下列內容:

    • SPACE:聊天室名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

    • GROUP:群組 ID。

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

    python3 chat_membership_group_create.py
    

Node.js

  1. 在工作目錄中,建立名為 add-group-to-space.js 的檔案。
  2. add-group-to-space.js 中加入下列程式碼:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Adds the group to the Chat space.
    * @return {!Promise<!Object>}
    */
    async function addUserToSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.members.create({
        parent: 'spaces/SPACE',
        requestBody: {groupMember: {name: 'groups/GROUP'}}
      });
    }
    
    addUserToSpace().then(console.log);
    
  3. 請在程式碼中替換下列內容:

    • SPACE:聊天室名稱,可從 spaces.list 方法 或聊天室網址傳送

    • GROUP:群組 ID。

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

    node add-group-to-space.js
    

Chat API 會傳回 membership敬上 詳細說明建立的群組成員

將 Chat 應用程式新增至聊天室

Chat 應用程式無法將其他應用程式新增為 空白鍵。如何將 Chat 應用程式新增至聊天室 或真人使用者之間的即時訊息,請在要求中傳送下列資訊:

  • 指定 chat.memberships.app 授權範圍。
  • create 方法membership 資源而言。
  • parent 設為要在其中建立成員資格的聊天室資源名稱。
  • member 設為 users/app;別名,代表呼叫 Chat API。

以下範例會將 Chat 應用程式新增至聊天室:

Python

  1. 在工作目錄中,建立名為 chat_membership_app_create.py
  2. chat_membership_app_create.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.memberships.app"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then adds the Chat app to a Chat space.
        '''
    
        # 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().members().create(
    
            # The space in which to create a membership.
            parent = 'spaces/SPACE',
    
            # Set the Chat app as the entity that gets added to the space.
            # 'app' is an alias for the Chat app calling the API.
            body = {
                'member': {
                  'name':'users/app',
                  'type': 'BOT'
                }
            }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 將程式碼中的 SPACE 替換成空格名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

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

    python3 chat_membership_app_create.py
    

Node.js

  1. 在工作目錄中,建立名為 add-app-to-space.js 的檔案。
  2. add-app-to-space.js 中加入下列程式碼:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Adds the app to the Chat space.
    * @return {!Promise<!Object>}
    */
    async function addAppToSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships.app',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.members.create({
        parent: 'spaces/SPACE',
        requestBody: {member: {name: 'users/app', type: 'BOT'}}
      });
    }
    
    addAppToSpace().then(console.log);
    
  3. 將程式碼中的 SPACE 替換成空格名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

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

    node add-app-to-space.js
    

Chat API 會傳回 membership敬上 詳細資料,為建立的應用程式成員提供詳細資料