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

本指南說明如何使用 Google Chat API 的 Membership 資源中的 create() 方法,邀請或新增使用者、Google 群組或 Chat 應用程式加入聊天室,也就是建立成員資格。建立成員時,如果指定成員關閉了自動接受政策,系統會傳送邀請,成員必須接受邀請才能加入。否則,建立成員資格會直接將成員新增至指定空間。

如果您是 Google Workspace 管理員,可以將使用者、Google 群組或 Chat 應用程式新增至 Google Workspace 機構中的任何聊天室。

Membership 資源代表使用者或 Google Chat 應用程式是否受邀加入、參與或缺席聊天室。

必要條件

Node.js

Python

Java

Apps Script

邀請使用者加入聊天室,或將使用者新增為聊天室成員

如要邀請或新增使用者至啟用使用者驗證的聊天室,請在要求中傳遞下列項目:

以下範例會使用使用者驗證,將使用者新增至即時通訊空間:

Node.js

Python

Java

Apps Script

如要執行範例,請替換下列項目:

Chat API 會傳回 Membership 的執行個體,詳細說明建立的使用者成員資格。

邀請 Google 群組加入聊天室,或將其新增至聊天室

如要邀請或新增 Google 群組加入具有使用者驗證的聊天室 (應用程式驗證不支援邀請或新增 Google 群組加入聊天室),請在要求中傳遞下列項目:

Google 群組只能加入具名聊天室,無法加入群組對話或即時訊息。

以下範例會透過使用者驗證,將群組新增至具名空間:

Node.js

Python

Java

Apps Script

如要執行範例,請替換下列項目:

Chat API 會傳回 Membership 的執行個體,詳細說明建立的使用者成員資格。

將 Chat 應用程式加入聊天室

Chat 應用程式無法將其他應用程式新增為聊天室成員。如要將 Chat 應用程式新增至聊天室或兩位使用者之間的即時訊息,請在要求中傳遞下列項目,並進行使用者驗證 (應用程式驗證不支援邀請或新增 Chat 應用程式至聊天室):

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

Node.js

Python

Java

Apps Script

如要執行範例,請將 SPACE_NAME 替換為空間的 name ID。您可以呼叫 ListSpaces() 方法或從空間的網址取得 ID。

Chat API 會傳回 Membership 的執行個體,詳細說明建立的使用者成員資格。

邀請使用者或將使用者新增至聊天室 (以 Chat 應用程式身分)

應用程式驗證需要管理員核准一次。

如要邀請或新增使用者至具有應用程式驗證的空間,請在要求中傳遞下列項目:

編寫呼叫 Chat API 的指令碼

以下範例會使用應用程式驗證,將使用者新增至聊天室:

Python

  1. 在工作目錄中,建立名為 chat_membership_app_create.py 的檔案。
  2. chat_membership_app_create.py 中加入下列程式碼:
from google.oauth2 import service_account  
from apiclient.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.app.memberships"]  
def main():  
    '''  
    Authenticates with Chat API using app authentication,  
    then adds a user to a Chat space by creating a membership.  
    '''  
    # Specify service account details.  
    creds = (  
        service_account.Credentials.from_service_account_file('credentials.json')  
        .with_scopes(SCOPES)  
    )  
    # 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()  
  1. 在程式碼中,請按照以下方式替換:
    • SPACE:聊天室名稱,可透過 Chat API 中的 spaces.list 方法或聊天室網址取得。
    • USER:使用者 ID。
  2. 在工作目錄中建構及執行範例:
python3 chat_membership_app_create.py  

以 Google Workspace 管理員身分將使用者或 Google 群組新增至聊天室

如果您是 Google Workspace 管理員,可以呼叫 create() 方法,將使用者、Google 群組或 Chat 應用程式新增至 Google Workspace 機構中的任何聊天室。

如要以 Google Workspace 管理員身分呼叫這個方法,請執行下列操作:

如需更多資訊和範例,請參閱「以 Google Workspace 管理員身分管理 Google Chat 聊天室」。

限制和注意事項