Z tego przewodnika dowiesz się, jak używać metody create
w zasobie membership
interfejsu Google Chat API do zapraszania i dodawania do pokoju użytkownika lub aplikacji do obsługi czatu nazywanej też tworzeniem subskrypcji. Jeśli podczas tworzenia członkostwa określony użytkownik ma wyłączoną zasadę automatycznego akceptowania, jest on zaproszony i musi zaakceptować zaproszenie do pokoju, zanim do niego dołączy. W przeciwnym razie utworzenie subskrypcji spowoduje dodanie
użytkownika bezpośrednio do określonego pokoju.
Zasób Membership
wskazuje, czy użytkownik lub aplikacja Google Chat są zaproszone do pokoju, które jest jego częścią lub poza nim.
Wymagania wstępne
Python
- Python 3.6 lub nowszy
- Narzędzie do zarządzania pakietami pip
Najnowsze biblioteki klienta Google dla języka Python. Aby je zainstalować lub zaktualizować, uruchom w interfejsie wiersza poleceń to polecenie:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- Projekt Google Cloud z włączonym i skonfigurowanym interfejsem Google Chat API. Instrukcje znajdziesz w artykule Tworzenie aplikacji Google Chat.
Autoryzacja dla aplikacji Google Chat została skonfigurowana. Tworzenie subskrypcji wymaga uwierzytelniania użytkownika z zakresem autoryzacji
chat.memberships
lubchat.memberships.app
.
Node.js
- Node.js i npm
Najnowsze biblioteki klienta Google dla środowiska Node.js. Aby je zainstalować, uruchom w interfejsie wiersza poleceń to polecenie:
npm install @google-cloud/local-auth @googleapis/chat
- Projekt Google Cloud z włączonym i skonfigurowanym interfejsem Google Chat API. Instrukcje znajdziesz w artykule Tworzenie aplikacji Google Chat.
Autoryzacja dla aplikacji Google Chat została skonfigurowana. Tworzenie subskrypcji wymaga uwierzytelniania użytkownika z zakresem autoryzacji
chat.memberships
lubchat.memberships.app
.
Zapraszanie i dodawanie użytkowników do pokoju
Aby zaprosić użytkownika do pokoju lub dodać do niego użytkownika, przekaż w swojej prośbie:
- Określ zakres autoryzacji
chat.memberships
. - Wywołaj metodę
create
w zasobiemembership
. - Ustaw
parent
na nazwę zasobu pokoju, w którym chcesz utworzyć członkostwo. - Ustaw
member
nausers/{user}
, gdzie{user}
to osoba, dla której chcesz utworzyć członkostwo. Możliwe wartości:- Identyfikator personelu w interfejsie People API. Jeśli na przykład w interfejsie People API person (osoba)
resourceName
ma wartośćpeople/123456789
, ustaw wartośćmembership.member.name
nausers/123456789
. - Identyfikator użytkownika w interfejsie Directory API.
- Adres e-mail użytkownika. Na przykład
users/222larabrown@gmail.com
lubusers/larabrown@cymbalgroup.com
. Jeśli użytkownik korzysta z konta Google lub należy do innej organizacji Google Workspace, musisz użyć jego adresu e-mail.
- Identyfikator personelu w interfejsie People API. Jeśli na przykład w interfejsie People API person (osoba)
Poniższy przykład pokazuje dodawanie użytkownika do pokoju:
Python
- W katalogu roboczym utwórz plik o nazwie
chat_membership_user_create.py
. Umieść ten kod w elemencie
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()
Zastąp w nim ten fragment kodu:
SPACE
: nazwa pokoju, którą można uzyskać za pomocą metodyspaces.list
w interfejsie Chat API lub z adresu URL pokoju.USER
: identyfikator użytkownika.
W katalogu roboczym skompiluj i uruchom przykład:
python3 chat_membership_user_create.py
Node.js
- W katalogu roboczym utwórz plik o nazwie
add-user-to-space.js
. Umieść ten kod w elemencie
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);
Zastąp w nim ten fragment kodu:
SPACE
: nazwa pokoju, którą można uzyskać za pomocą metodyspaces.list
w interfejsie Chat API lub z adresu URL pokoju.USER
: identyfikator użytkownika.
W katalogu roboczym uruchom przykład:
node add-user-to-space.js
Interfejs Chat API zwraca instancję membership
, która zawiera szczegółowe informacje o utworzonej subskrypcji.
Dodawanie aplikacji Google Chat do pokoju
Aplikacja Google Chat nie może dodać innej aplikacji jako użytkownika pokoju. Aby dodać aplikację Google Chat do pokoju lub czatu między 2 użytkownikami, przekaż w swojej prośbie te informacje:
- Określ zakres autoryzacji
chat.memberships.app
. - Wywołaj metodę
create
w zasobiemembership
. - Ustaw
parent
na nazwę zasobu pokoju, w którym chcesz utworzyć członkostwo. - Ustaw
member
nausers/app
, czyli alias reprezentujący aplikację wywołującą interfejs Chat API.
Poniższy przykład pokazuje dodawanie aplikacji Google Chat do pokoju:
Python
- W katalogu roboczym utwórz plik o nazwie
chat_membership_app_create.py
. Umieść ten kod w elemencie
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()
W kodzie zastąp
SPACE
nazwą pokoju, którą możesz uzyskać za pomocą metodyspaces.list
w interfejsie Chat API lub z adresu URL pokoju.W katalogu roboczym skompiluj i uruchom przykład:
python3 chat_membership_app_create.py
Node.js
- W katalogu roboczym utwórz plik o nazwie
add-app-to-space.js
. Umieść ten kod w elemencie
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);
W kodzie zastąp
SPACE
nazwą pokoju, którą możesz uzyskać za pomocą metodyspaces.list
w interfejsie Chat API lub z adresu URL pokoju.W katalogu roboczym uruchom przykład:
node add-app-to-space.js
Interfejs Chat API zwraca instancję membership
, która zawiera szczegółowe informacje o utworzonej subskrypcji.
Powiązane artykuły
- Wyświetlanie szczegółowych informacji o subskrypcji użytkownika lub aplikacji Google Chat
- Wyświetlanie listy użytkowników w pokoju
- Aktualizowanie członkostwa użytkownika w pokoju w Google Chat
- Usuwanie użytkownika lub aplikacji do obsługi czatu z pokoju