Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions chat/client-libraries/cloud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Google Chat API - Cloud Client library samples

## Set up

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 `pip install -r requirements.txt`

## 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`, you should run
`python create-message-app-cred.py`.
71 changes: 71 additions & 0 deletions chat/client-libraries/cloud/authentication_utils.py
Original file line number Diff line number Diff line change
@@ -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]
56 changes: 56 additions & 0 deletions chat/client-libraries/cloud/create_membership_user_cred.py
Original file line number Diff line number Diff line change
@@ -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]
54 changes: 54 additions & 0 deletions chat/client-libraries/cloud/create_membership_user_cred_for_app.py
Original file line number Diff line number Diff line change
@@ -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]
47 changes: 47 additions & 0 deletions chat/client-libraries/cloud/create_message_app_cred.py
Original file line number Diff line number Diff line change
@@ -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]
76 changes: 76 additions & 0 deletions chat/client-libraries/cloud/create_message_app_cred_with_cards.py
Original file line number Diff line number Diff line change
@@ -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]
48 changes: 48 additions & 0 deletions chat/client-libraries/cloud/create_message_user_cred.py
Original file line number Diff line number Diff line change
@@ -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]
Loading