diff --git a/chat/client-libraries/cloud/README.md b/chat/client-libraries/cloud/README.md new file mode 100644 index 00000000..bd42691c --- /dev/null +++ b/chat/client-libraries/cloud/README.md @@ -0,0 +1,21 @@ +# Google Chat API - Cloud Client library samples + +## Setup + +1. Add `service_account.json` and/or `client_secrets.json` to the current + folder depending on the credentials used by the samples to run: + + 1. `service_account.json` for + [app credentials](https://developers.google.com/workspace/chat/authenticate-authorize-chat-app) + + 1. `client_secrets.json` for + [user credentials](https://developers.google.com/workspace/chat/authenticate-authorize-chat-user) + +1. Execute `npm install` + +## Run + +Execute `python replace-with-the-sample-file.py` wih the sample file path of the sample. + +For example, to run the sample `create-message-app-cred`, your should run +`python create-message-app-cred.py`. diff --git a/chat/client-libraries/cloud/authentication_utils.py b/chat/client-libraries/cloud/authentication_utils.py new file mode 100644 index 00000000..d35ef798 --- /dev/null +++ b/chat/client-libraries/cloud/authentication_utils.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_authentication_utils] +import json + +import google.oauth2.credentials + +from google_auth_oauthlib.flow import InstalledAppFlow +from google.apps import chat_v1 as google_chat + +CLIENT_SECRETS_FILE = 'client_secrets.json' + +SERVICE_ACCOUNT_FILE = 'service_account.json' + +APP_AUTH_OAUTH_SCOPE = ["https://www.googleapis.com/auth/chat.bot"] + +def create_client_with_app_credentials(): + # For more information on app authentication, see + # https://developers.google.com/workspace/chat/authenticate-authorize-chat-app + creds = google.oauth2.service_account.Credentials.from_service_account_file( + SERVICE_ACCOUNT_FILE) + + return google_chat.ChatServiceClient( + credentials = creds, + client_options={ + "scopes": APP_AUTH_OAUTH_SCOPE + }) + +def create_client_with_user_credentials(scopes): + # For more information on user authentication, see + # https://developers.google.com/workspace/chat/authenticate-authorize-chat-user + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, scopes) + cred = flow.run_local_server() + installed = json.load(open(CLIENT_SECRETS_FILE))["installed"] + + creds = google.oauth2.credentials.Credentials( + token = cred.token, + refresh_token = cred.refresh_token, + token_uri = installed["token_uri"], + client_id = installed["client_id"], + client_secret = installed["client_secret"], + scopes = scopes + ) + + # Create a client + client = google_chat.ChatServiceClient( + credentials = creds, + client_options = { + "scopes" : scopes + } + ) + + return client + +# [END chat_authentication_utils] diff --git a/chat/client-libraries/cloud/create_membership_user_cred.py b/chat/client-libraries/cloud/create_membership_user_cred.py new file mode 100644 index 00000000..fec4fc90 --- /dev/null +++ b/chat/client-libraries/cloud/create_membership_user_cred.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + + +# [START chat_create_membership_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.memberships"] + +# This sample shows how to create membership with user credential for a human +# user +def create_membership_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.CreateMembershipRequest( + # Replace SPACE_NAME here + parent = "spaces/SPACE_NAME", + membership = { + "member": { + # Replace USER_NAME here + "name": "users/USER_NAME", + # user type for the membership + "type_": "HUMAN" + } + } + ) + + # Make the request + response = client.create_membership(request) + + # Handle the response + print(response) + +create_membership_with_user_cred() + +# [END chat_create_membership_user_cred] diff --git a/chat/client-libraries/cloud/create_membership_user_cred_for_app.py b/chat/client-libraries/cloud/create_membership_user_cred_for_app.py new file mode 100644 index 00000000..57840f5c --- /dev/null +++ b/chat/client-libraries/cloud/create_membership_user_cred_for_app.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_create_membership_user_cred_for_app] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.memberships.app"] + +# This sample shows how to create membership with app credential for an app +def create_membership_with_user_cred_for_app(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.CreateMembershipRequest( + # Replace SPACE_NAME here + parent = "spaces/SPACE_NAME", + membership = { + "member": { + # member name for app membership, do not change this. + "name": "users/app", + # user type for the membership + "type_": "BOT" + } + } + ) + + # Make the request + response = client.create_membership(request) + + # Handle the response + print(response) + +create_membership_with_user_cred_for_app() + +# [END chat_create_membership_user_cred_for_app] diff --git a/chat/client-libraries/cloud/create_message_app_cred.py b/chat/client-libraries/cloud/create_message_app_cred.py new file mode 100644 index 00000000..c36ea7f0 --- /dev/null +++ b/chat/client-libraries/cloud/create_message_app_cred.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_create_message_app_cred] +from authentication_utils import create_client_with_app_credentials +from google.apps import chat_v1 as google_chat + +# This sample shows how to create message with app credential +def create_message_with_app_cred(): + # Create a client + client = create_client_with_app_credentials() + + # Initialize request argument(s) + request = google_chat.CreateMessageRequest( + # Replace SPACE_NAME here. + parent = "spaces/SPACE_NAME", + message = { + "text" : "Hello with app credential!" + } + ) + + # Make the request + response = client.create_message(request) + + # Handle the response + print(response) + +create_message_with_app_cred() + +# [END chat_create_message_app_cred] diff --git a/chat/client-libraries/cloud/create_message_app_cred_with_cards.py b/chat/client-libraries/cloud/create_message_app_cred_with_cards.py new file mode 100644 index 00000000..9cf04f2f --- /dev/null +++ b/chat/client-libraries/cloud/create_message_app_cred_with_cards.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_create_message_app_cred_with_cards] +from authentication_utils import create_client_with_app_credentials +from google.apps import chat_v1 as google_chat + +# This sample shows how to create message with a card attached with app +# credential +def create_message_with_app_cred_with_cards(): + # Create a client + client = create_client_with_app_credentials() + + # Initialize request argument(s) + request = google_chat.CreateMessageRequest( + # Replace SPACE_NAME here + parent = "spaces/SPACE_NAME", + message = { + "text": "Hello with app credential!", + "cards_v2" : [{ + "card_id": "card-id", + "card": { + "sections": [{ + "widgets": [{ + "button_list": { + "buttons": [ + { + "text": "Edit", + "disabled": True, + }, + { + "icon": { + "known_icon": "INVITE", + "alt_text": "check calendar" + }, + "on_click": { + "open_link": { + "url": "https://www.google.com" + } + } + } + ] + } + }] + }] + } + }] + } + ) + + # Make the request + response = client.create_message(request) + + # Handle the response + print(response) + +create_message_with_app_cred_with_cards() + +# [END chat_create_message_app_cred_with_cards] diff --git a/chat/client-libraries/cloud/create_message_user_cred.py b/chat/client-libraries/cloud/create_message_user_cred.py new file mode 100644 index 00000000..b39b72e3 --- /dev/null +++ b/chat/client-libraries/cloud/create_message_user_cred.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_create_message_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.messages.create"] + +def create_message_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.CreateMessageRequest( + # Replace SPACE_NAME here. + parent = "spaces/SPACE_NAME", + message = { + "text": "Hello with user credential!" + } + ) + + # Make the request + response = client.create_message(request) + + # Handle the response + print(response) + +create_message_with_user_cred() + +# [END chat_create_message_user_cred] diff --git a/chat/client-libraries/cloud/create_message_user_cred_at_mention.py b/chat/client-libraries/cloud/create_message_user_cred_at_mention.py new file mode 100644 index 00000000..6dde1344 --- /dev/null +++ b/chat/client-libraries/cloud/create_message_user_cred_at_mention.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + + +# [START chat_create_message_user_cred_at_mention] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.messages.create"] + +# This sample shows how to create message with user credential with at mention +def create_message_with_user_cred_at_mention(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.CreateMessageRequest( + # replace SPACE_NAME here + parent = "spaces/SPACE_NAME", + message = { + # The user with USER_NAME will be at mentioned if they are in the + # space. + # Replace USER_NAME here + "text": "Hello !" + } + ) + + # Make the request + response = client.create_message(request) + + # Handle the response + print(response) + +create_message_with_user_cred_at_mention() + +# [END chat_create_message_user_cred_at_mention] diff --git a/chat/client-libraries/cloud/create_message_user_cred_message_id.py b/chat/client-libraries/cloud/create_message_user_cred_message_id.py new file mode 100644 index 00000000..6e52cde5 --- /dev/null +++ b/chat/client-libraries/cloud/create_message_user_cred_message_id.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + + +# [START chat_create_message_user_cred_message_id] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.messages.create"] + +# This sample shows how to create message with user credential with message id +def create_message_with_user_cred_message_id(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.CreateMessageRequest( + # Replace SPACE_NAME here + parent = "spaces/SPACE_NAME", + # Message id let chat apps get, update or delete a message without needing + # to store the system assigned ID in the message's resource name. + message_id = "client-MESSAGE-ID", + message = { + "text": "Hello with user credential!" + } + ) + + # Make the request + response = client.create_message(request) + + # Handle the response + print(response) + +create_message_with_user_cred_message_id() + +# [END chat_create_message_user_cred_message_id] diff --git a/chat/client-libraries/cloud/create_message_user_cred_request_id.py b/chat/client-libraries/cloud/create_message_user_cred_request_id.py new file mode 100644 index 00000000..58c67b25 --- /dev/null +++ b/chat/client-libraries/cloud/create_message_user_cred_request_id.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + + +# [START chat_create_message_user_cred_request_id] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.messages.create"] + +# This sample shows how to create message with user credential with request id +def create_message_with_user_cred_request_id(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.CreateMessageRequest( + # Replace SPACE_NAME here + parent = "spaces/SPACE_NAME", + # Specifying an existing request ID returns the message created with + # that ID instead of creating a new message. + request_id = "REQUEST_ID", + message = { + "text": "Hello with user credential!" + } + ) + + # Make the request + response = client.create_message(request) + + # Handle the response + print(response) + +create_message_with_user_cred_request_id() + +# [END chat_create_message_user_cred_request_id] diff --git a/chat/client-libraries/cloud/create_message_user_cred_thread_key.py b/chat/client-libraries/cloud/create_message_user_cred_thread_key.py new file mode 100644 index 00000000..75cda5f1 --- /dev/null +++ b/chat/client-libraries/cloud/create_message_user_cred_thread_key.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_create_message_user_cred_thread_key] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +import google.apps.chat_v1.CreateMessageRequest.MessageReplyOption + +SCOPES = ["https://www.googleapis.com/auth/chat.messages.create"] + +# This sample shows how to create message with user credential with thread key +def create_message_with_user_cred_thread_key(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.CreateMessageRequest( + # Replace SPACE_NAME here + parent = "spaces/SPACE_NAME", + # Creates the message as a reply to the thread specified by thread_key. + # If it fails, the message starts a new thread instead. + message_reply_option = MessageReplyOption.REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD, + message = { + "text": "Hello with user credential!", + "thread": { + # Thread key specifies a thread and is unique to the chat app + # that sets it. + "thread_key": "THREAD_KEY" + } + } + ) + + # Make the request + response = client.create_message(request) + + # Handle the response + print(response) + +create_message_with_user_cred_thread_key() + +# [END chat_create_message_user_cred_thread_key] diff --git a/chat/client-libraries/cloud/create_message_user_cred_thread_name.py b/chat/client-libraries/cloud/create_message_user_cred_thread_name.py new file mode 100644 index 00000000..c608261e --- /dev/null +++ b/chat/client-libraries/cloud/create_message_user_cred_thread_name.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + + +# [START chat_create_message_user_cred_thread_name] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +import google.apps.chat_v1.CreateMessageRequest.MessageReplyOption + +SCOPES = ["https://www.googleapis.com/auth/chat.messages.create"] + +# This sample shows how to create message with user credential with thread name +def create_message_with_user_cred_thread_name(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.CreateMessageRequest( + # Replace SPACE_NAME here + parent = "spaces/SPACE_NAME", + # Creates the message as a reply to the thread specified by thread.name. + # If it fails, the message starts a new thread instead. + message_reply_option = MessageReplyOption.REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD, + message = { + "text": "Hello with user credential!", + "thread": { + # Resource name of a thread that uniquely identify a thread. + # Replace SPACE_NAME and THREAD_NAME here + "name": "spaces/SPACE_NAME/threads/THREAD_NAME" + } + } + ) + + # Make the request + response = client.create_message(request) + + # Handle the response + print(response) + +create_message_with_user_cred_thread_name() + +# [END chat_create_message_user_cred_thread_name] diff --git a/chat/client-libraries/cloud/delete_message_app_cred.py b/chat/client-libraries/cloud/delete_message_app_cred.py new file mode 100644 index 00000000..a4706255 --- /dev/null +++ b/chat/client-libraries/cloud/delete_message_app_cred.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_delete_message_app_cred] +from authentication_utils import create_client_with_app_credentials +from google.apps import chat_v1 as google_chat + +# This sample shows how to delete a message with app credential +def delete_message_with_app_cred(): + # Create a client + client = create_client_with_app_credentials() + + # Initialize request argument(s) + request = google_chat.DeleteMessageRequest( + # Replace SPACE_NAME and MESSAGE_NAME here + name = "spaces/SPACE_NAME/messages/MESSAGE_NAME", + ) + + # Make the request + response = client.delete_message(request) + + # Handle the response + print(response) + +delete_message_with_app_cred() + +# [END chat_delete_message_app_cred] diff --git a/chat/client-libraries/cloud/delete_message_user_cred.py b/chat/client-libraries/cloud/delete_message_user_cred.py new file mode 100644 index 00000000..d8a046a2 --- /dev/null +++ b/chat/client-libraries/cloud/delete_message_user_cred.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_delete_message_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.messages"] + +# This sample shows how to delete a message with user credential +def delete_message_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.DeleteMessageRequest( + # Replace SPACE_NAME and MESSAGE_NAME here + name = "spaces/SPACE_NAME/messages/MESSAGE_NAME", + ) + + # Make the request + response = client.delete_message(request) + + # Handle the response + print(response) + +delete_message_with_user_cred() + +# [END chat_delete_message_user_cred] diff --git a/chat/client-libraries/cloud/get_membership_app_cred.py b/chat/client-libraries/cloud/get_membership_app_cred.py new file mode 100644 index 00000000..12325a1d --- /dev/null +++ b/chat/client-libraries/cloud/get_membership_app_cred.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_get_membership_app_cred] +from authentication_utils import create_client_with_app_credentials +from google.apps import chat_v1 as google_chat + +# This sample shows how to get membership with app credential +def get_membership_with_app_cred(): + # Create a client + client = create_client_with_app_credentials() + + # Initialize request argument(s) + request = google_chat.GetMembershipRequest( + # Replace SPACE_NAME and MEMBER_NAME here + name = 'spaces/SPACE_NAME/members/MEMBER_NAME', + ) + + # Make the request + response = client.get_membership(request) + + # Handle the response + print(response) + +get_membership_with_app_cred() + +# [END chat_get_membership_app_cred] diff --git a/chat/client-libraries/cloud/get_membership_user_cred.py b/chat/client-libraries/cloud/get_membership_user_cred.py new file mode 100644 index 00000000..bab553a4 --- /dev/null +++ b/chat/client-libraries/cloud/get_membership_user_cred.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_get_membership_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.memberships.readonly"] + +# This sample shows how to get membership with user credential +def get_membership_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.GetMembershipRequest( + # Replace SPACE_NAME and MEMBER_NAME here + name = 'spaces/SPACE_NAME/members/MEMBER_NAME', + ) + + # Make the request + response = client.get_membership(request) + + # Handle the response + print(response) + +get_membership_with_user_cred() + +# [END chat_get_membership_user_cred] diff --git a/chat/client-libraries/cloud/get_message_app_cred.py b/chat/client-libraries/cloud/get_message_app_cred.py new file mode 100644 index 00000000..ca8e7888 --- /dev/null +++ b/chat/client-libraries/cloud/get_message_app_cred.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_get_message_app_cred] +from authentication_utils import create_client_with_app_credentials +from google.apps import chat_v1 as google_chat + +# This sample shows how to get message with app credential +def get_message_with_app_cred(): + # Create a client + client = create_client_with_app_credentials() + + # Initialize request argument(s) + request = google_chat.GetMessageRequest( + # Replace SPACE_NAME and MESSAGE_NAME here + name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME', + ) + + # Make the request + response = client.get_message(request=request) + + # Handle the response + print(response) + +get_message_with_app_cred() + +# [END chat_get_message_app_cred] diff --git a/chat/client-libraries/cloud/get_message_user_cred.py b/chat/client-libraries/cloud/get_message_user_cred.py new file mode 100644 index 00000000..8f160b35 --- /dev/null +++ b/chat/client-libraries/cloud/get_message_user_cred.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_get_message_user_cred] +from authentication_utils import create_client_with_user_credentials +import google.oauth2.credentials + +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"] + +# This sample shows how to get message with user credential +def get_message_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.GetMessageRequest( + # Replace SPACE_NAME and MESSAGE_NAME here + name = "spaces/SPACE_NAME/messages/MESSAGE_NAME", + ) + + # Make the request + response = client.get_message(request) + + # Handle the response + print(response) + +get_message_with_user_cred() + +# [END chat_get_message_user_cred] diff --git a/chat/client-libraries/cloud/get_space_app_cred.py b/chat/client-libraries/cloud/get_space_app_cred.py new file mode 100644 index 00000000..1f5bb341 --- /dev/null +++ b/chat/client-libraries/cloud/get_space_app_cred.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_get_space_app_cred] +from authentication_utils import create_client_with_app_credentials +from google.apps import chat_v1 as google_chat + +# This sample shows how to get space with app credential +def get_space_with_app_cred(): + # Create a client + client = create_client_with_app_credentials() + + # Initialize request argument(s) + request = google_chat.GetSpaceRequest( + # Replace SPACE_NAME here + name = "spaces/SPACE_NAME", + ) + + # Make the request + response = client.get_space(request) + + # Handle the response + print(response) + +get_space_with_app_cred() + +# [END chat_get_space_app_cred] diff --git a/chat/client-libraries/cloud/get_space_user_cred.py b/chat/client-libraries/cloud/get_space_user_cred.py new file mode 100644 index 00000000..3287762d --- /dev/null +++ b/chat/client-libraries/cloud/get_space_user_cred.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Snippet for GetSpace + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_get_space_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.spaces.readonly"] + +# This sample shows how to get space with user credential +def get_space_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.GetSpaceRequest( + # Replace SPACE_NAME here + name = "spaces/SPACE_NAME", + ) + + # Make the request + response = client.get_space(request) + + # Handle the response + print(response) + +get_space_with_user_cred() + +# [END chat_get_space_user_cred] diff --git a/chat/client-libraries/cloud/list_memberships_app_cred.py b/chat/client-libraries/cloud/list_memberships_app_cred.py new file mode 100644 index 00000000..6ebe1eb3 --- /dev/null +++ b/chat/client-libraries/cloud/list_memberships_app_cred.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_list_memberships_app_cred] +from authentication_utils import create_client_with_app_credentials +from google.apps import chat_v1 as google_chat + +# This sample shows how to list memberships with app credential +def list_memberships_app_cred(): + # Create a client + client = create_client_with_app_credentials() + + # Initialize request argument(s) + request = google_chat.ListMembershipsRequest( + # Replace SPACE_NAME here + parent = 'spaces/SPACE_NAME', + # Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or + # ROLE_MANAGER) + filter = 'member.type = "HUMAN"', + # Number of results that will be returned at once + page_size = 100 + ) + + # Make the request + page_result = client.list_memberships(request) + + # Handle the response. Iterating over page_result will yield results and + # resolve additional pages automatically. + for response in page_result: + print(response) + +list_memberships_app_cred() + +# [END chat_list_memberships_app_cred] diff --git a/chat/client-libraries/cloud/list_memberships_user_cred.py b/chat/client-libraries/cloud/list_memberships_user_cred.py new file mode 100644 index 00000000..4d50c959 --- /dev/null +++ b/chat/client-libraries/cloud/list_memberships_user_cred.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_list_memberships_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.memberships.readonly"] + +# This sample shows how to list memberships with user credential +def list_memberships_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.ListMembershipsRequest( + # Replace SPACE_NAME here + parent = 'spaces/SPACE_NAME', + # Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or + # ROLE_MANAGER) + filter = 'member.type = "HUMAN"', + # Number of results that will be returned at once + page_size = 100 + ) + + # Make the request + page_result = client.list_memberships(request) + + # Handle the response. Iterating over page_result will yield results and + # resolve additional pages automatically. + for response in page_result: + print(response) + +list_memberships_user_cred() + +# [END chat_list_memberships_user_cred] diff --git a/chat/client-libraries/cloud/list_messages_user_cred.py b/chat/client-libraries/cloud/list_messages_user_cred.py new file mode 100644 index 00000000..cacef343 --- /dev/null +++ b/chat/client-libraries/cloud/list_messages_user_cred.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_list_messages_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"] + +# This sample shows how to list messages with user credential +def list_messages_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.ListMessagesRequest( + # Replace SPACE_NAME here + parent = 'spaces/SPACE_NAME', + # Number of results that will be returned at once + page_size = 100 + ) + + # Make the request + page_result = client.list_messages(request) + + # Handle the response. Iterating over page_result will yield results and + # resolve additional pages automatically. + for response in page_result: + print(response) + +list_messages_with_user_cred() + +# [END chat_list_messages_user_cred] diff --git a/chat/client-libraries/cloud/list_spaces_app_cred.py b/chat/client-libraries/cloud/list_spaces_app_cred.py new file mode 100644 index 00000000..0c41ae20 --- /dev/null +++ b/chat/client-libraries/cloud/list_spaces_app_cred.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_list_spaces_app_cred] +from authentication_utils import create_client_with_app_credentials +from google.apps import chat_v1 as google_chat + +# This sample shows how to list spaces with app credential +def list_spaces_app_cred(): + # Create a client + client = create_client_with_app_credentials() + + # Initialize request argument(s) + request = google_chat.ListSpacesRequest( + # Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE) + filter = 'space_type = "SPACE"', + # Number of results that will be returned at once + page_size = 100 + ) + + # Make the request + page_result = client.list_spaces(request) + + # Handle the response. Iterating over page_result will yield results and + # resolve additional pages automatically. + for response in page_result: + print(response) + +list_spaces_app_cred() + +# [END chat_list_spaces_app_cred] diff --git a/chat/client-libraries/cloud/list_spaces_user_cred.py b/chat/client-libraries/cloud/list_spaces_user_cred.py new file mode 100644 index 00000000..5ae906cf --- /dev/null +++ b/chat/client-libraries/cloud/list_spaces_user_cred.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_list_spaces_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.spaces.readonly"] + +# This sample shows how to list spaces with user credential +def list_spaces_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.ListSpacesRequest( + # Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE) + filter = 'space_type = "SPACE"', + # Number of results that will be returned at once + page_size = 100 + ) + + # Make the request + page_result = client.list_spaces(request) + + # Handle the response. Iterating over page_result will yield results and + # resolve additional pages automatically. + for response in page_result: + print(response) + +list_spaces_with_user_cred() + +# [END chat_list_spaces_user_cred] diff --git a/chat/client-libraries/cloud/requirements.txt b/chat/client-libraries/cloud/requirements.txt new file mode 100644 index 00000000..b8d11a9a --- /dev/null +++ b/chat/client-libraries/cloud/requirements.txt @@ -0,0 +1,3 @@ +google_auth_oauthlib==1.2.0 +protobuf==4.21.12 +google-apps-chat==0.1.0 diff --git a/chat/client-libraries/cloud/update_message_app_cred.py b/chat/client-libraries/cloud/update_message_app_cred.py new file mode 100644 index 00000000..b9081dc4 --- /dev/null +++ b/chat/client-libraries/cloud/update_message_app_cred.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + +# [START chat_update_message_app_cred] +from authentication_utils import create_client_with_app_credentials +from google.apps import chat_v1 as google_chat + +# This sample shows how to update a message with app credential +def update_message_with_app_cred(): + # Create a client + client = create_client_with_app_credentials() + + # Initialize request argument(s) + request = google_chat.UpdateMessageRequest( + message = { + # Replace SPACE_NAME and MESSAGE_NAME here + "name": "spaces/SPACE_NAME/messages/MESSAGE_NAME", + "text": "Updated with app credential!" + }, + # The field paths to update. Separate multiple values with commas or use + # `*` to update all field paths. + update_mask = "text" + ) + + # Make the request + response = client.update_message(request) + + # Handle the response + print(response) + +update_message_with_app_cred() + +# [END chat_update_message_app_cred] diff --git a/chat/client-libraries/cloud/update_message_user_cred.py b/chat/client-libraries/cloud/update_message_user_cred.py new file mode 100644 index 00000000..18146e3d --- /dev/null +++ b/chat/client-libraries/cloud/update_message_user_cred.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + + +# [START chat_update_message_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.messages"] + +# This sample shows how to update a message with user credential +def update_message_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.UpdateMessageRequest( + message = { + # Replace SPACE_NAME and MESSAGE_NAME here + "name": "spaces/SPACE_NAME/messages/MESSAGE_NAME", + "text": "Updated with user credential!" + }, + # The field paths to update. Separate multiple values with commas or use + # `*` to update all field paths. + update_mask = "text" + ) + + # Make the request + response = client.update_message(request) + + # Handle the response + print(response) + +update_message_with_user_cred() + +# [END chat_update_message_user_cred] diff --git a/chat/client-libraries/cloud/update_space_user_cred.py b/chat/client-libraries/cloud/update_space_user_cred.py new file mode 100644 index 00000000..3d257d0b --- /dev/null +++ b/chat/client-libraries/cloud/update_space_user_cred.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-apps-chat + + +# [START chat_update_space_user_cred] +from authentication_utils import create_client_with_user_credentials +from google.apps import chat_v1 as google_chat + +SCOPES = ["https://www.googleapis.com/auth/chat.spaces"] + +# This sample shows how to update a space with user credential +def update_space_with_user_cred(): + # Create a client + client = create_client_with_user_credentials(SCOPES) + + # Initialize request argument(s) + request = google_chat.UpdateSpaceRequest( + space = { + # Replace SPACE_NAME here + 'name': 'spaces/SPACE_NAME', + 'display_name': 'New space display name' + }, + # The field paths to update. Separate multiple values with commas. + update_mask = 'display_name' + ) + + # Make the request + response = client.update_space(request) + + # Handle the response + print(response) + +update_space_with_user_cred() + +# [END chat_update_space_user_cred]