Content-Length: 1000609 | pFad | http://github.com/googleworkspace/python-samples/commit/823468ed7f0a311b2261bb769f5c0833575d0f12

63 feat: add Cloud Client library samples for Chat (#1666) · googleworkspace/python-samples@823468e · GitHub
Skip to content

Commit 823468e

Browse files
PierrickVouletpierrick
and
pierrick
authored
feat: add Cloud Client library samples for Chat (#1666)
* feat: add Cloud Client library samples for Chat * README fixes --------- Co-authored-by: pierrick <pierrick@google.com>
1 parent c9c3f28 commit 823468e

29 files changed

+1423
-0
lines changed

chat/client-libraries/cloud/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Google Chat API - Cloud Client library samples
2+
3+
## Set up
4+
5+
1. Add `service_account.json` and/or `client_secrets.json` to the current
6+
folder depending on the credentials used by the samples to run:
7+
8+
1. `service_account.json` for
9+
[app credentials](https://developers.google.com/workspace/chat/authenticate-authorize-chat-app)
10+
11+
1. `client_secrets.json` for
12+
[user credentials](https://developers.google.com/workspace/chat/authenticate-authorize-chat-user)
13+
14+
1. Execute `pip install -r requirements.txt`
15+
16+
## Run
17+
18+
Execute `python replace-with-the-sample-file.py` wih the sample file path of the sample.
19+
20+
For example, to run the sample `create-message-app-cred`, you should run
21+
`python create-message-app-cred.py`.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2024 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# To install the latest published package dependency, execute the following:
17+
# python3 -m pip install google-apps-chat
18+
19+
# [START chat_authentication_utils]
20+
import json
21+
22+
import google.oauth2.credentials
23+
24+
from google_auth_oauthlib.flow import InstalledAppFlow
25+
from google.apps import chat_v1 as google_chat
26+
27+
CLIENT_SECRETS_FILE = 'client_secrets.json'
28+
29+
SERVICE_ACCOUNT_FILE = 'service_account.json'
30+
31+
APP_AUTH_OAUTH_SCOPE = ["https://www.googleapis.com/auth/chat.bot"]
32+
33+
def create_client_with_app_credentials():
34+
# For more information on app authentication, see
35+
# https://developers.google.com/workspace/chat/authenticate-authorize-chat-app
36+
creds = google.oauth2.service_account.Credentials.from_service_account_file(
37+
SERVICE_ACCOUNT_FILE)
38+
39+
return google_chat.ChatServiceClient(
40+
credentials = creds,
41+
client_options={
42+
"scopes": APP_AUTH_OAUTH_SCOPE
43+
})
44+
45+
def create_client_with_user_credentials(scopes):
46+
# For more information on user authentication, see
47+
# https://developers.google.com/workspace/chat/authenticate-authorize-chat-user
48+
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, scopes)
49+
cred = flow.run_local_server()
50+
installed = json.load(open(CLIENT_SECRETS_FILE))["installed"]
51+
52+
creds = google.oauth2.credentials.Credentials(
53+
token = cred.token,
54+
refresh_token = cred.refresh_token,
55+
token_uri = installed["token_uri"],
56+
client_id = installed["client_id"],
57+
client_secret = installed["client_secret"],
58+
scopes = scopes
59+
)
60+
61+
# Create a client
62+
client = google_chat.ChatServiceClient(
63+
credentials = creds,
64+
client_options = {
65+
"scopes" : scopes
66+
}
67+
)
68+
69+
return client
70+
71+
# [END chat_authentication_utils]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2024 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# It may require modifications to work in your environment.
17+
18+
# To install the latest published package dependency, execute the following:
19+
# python3 -m pip install google-apps-chat
20+
21+
22+
# [START chat_create_membership_user_cred]
23+
from authentication_utils import create_client_with_user_credentials
24+
from google.apps import chat_v1 as google_chat
25+
26+
SCOPES = ["https://www.googleapis.com/auth/chat.memberships"]
27+
28+
# This sample shows how to create membership with user credential for a human
29+
# user
30+
def create_membership_with_user_cred():
31+
# Create a client
32+
client = create_client_with_user_credentials(SCOPES)
33+
34+
# Initialize request argument(s)
35+
request = google_chat.CreateMembershipRequest(
36+
# Replace SPACE_NAME here
37+
parent = "spaces/SPACE_NAME",
38+
membership = {
39+
"member": {
40+
# Replace USER_NAME here
41+
"name": "users/USER_NAME",
42+
# user type for the membership
43+
"type_": "HUMAN"
44+
}
45+
}
46+
)
47+
48+
# Make the request
49+
response = client.create_membership(request)
50+
51+
# Handle the response
52+
print(response)
53+
54+
create_membership_with_user_cred()
55+
56+
# [END chat_create_membership_user_cred]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2024 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# It may require modifications to work in your environment.
17+
18+
# To install the latest published package dependency, execute the following:
19+
# python3 -m pip install google-apps-chat
20+
21+
# [START chat_create_membership_user_cred_for_app]
22+
from authentication_utils import create_client_with_user_credentials
23+
from google.apps import chat_v1 as google_chat
24+
25+
SCOPES = ["https://www.googleapis.com/auth/chat.memberships.app"]
26+
27+
# This sample shows how to create membership with app credential for an app
28+
def create_membership_with_user_cred_for_app():
29+
# Create a client
30+
client = create_client_with_user_credentials(SCOPES)
31+
32+
# Initialize request argument(s)
33+
request = google_chat.CreateMembershipRequest(
34+
# Replace SPACE_NAME here
35+
parent = "spaces/SPACE_NAME",
36+
membership = {
37+
"member": {
38+
# member name for app membership, do not change this.
39+
"name": "users/app",
40+
# user type for the membership
41+
"type_": "BOT"
42+
}
43+
}
44+
)
45+
46+
# Make the request
47+
response = client.create_membership(request)
48+
49+
# Handle the response
50+
print(response)
51+
52+
create_membership_with_user_cred_for_app()
53+
54+
# [END chat_create_membership_user_cred_for_app]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2024 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# It may require modifications to work in your environment.
17+
18+
# To install the latest published package dependency, execute the following:
19+
# python3 -m pip install google-apps-chat
20+
21+
# [START chat_create_message_app_cred]
22+
from authentication_utils import create_client_with_app_credentials
23+
from google.apps import chat_v1 as google_chat
24+
25+
# This sample shows how to create message with app credential
26+
def create_message_with_app_cred():
27+
# Create a client
28+
client = create_client_with_app_credentials()
29+
30+
# Initialize request argument(s)
31+
request = google_chat.CreateMessageRequest(
32+
# Replace SPACE_NAME here.
33+
parent = "spaces/SPACE_NAME",
34+
message = {
35+
"text" : "Hello with app credential!"
36+
}
37+
)
38+
39+
# Make the request
40+
response = client.create_message(request)
41+
42+
# Handle the response
43+
print(response)
44+
45+
create_message_with_app_cred()
46+
47+
# [END chat_create_message_app_cred]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2024 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# It may require modifications to work in your environment.
17+
18+
# To install the latest published package dependency, execute the following:
19+
# python3 -m pip install google-apps-chat
20+
21+
# [START chat_create_message_app_cred_with_cards]
22+
from authentication_utils import create_client_with_app_credentials
23+
from google.apps import chat_v1 as google_chat
24+
25+
# This sample shows how to create message with a card attached with app
26+
# credential
27+
def create_message_with_app_cred_with_cards():
28+
# Create a client
29+
client = create_client_with_app_credentials()
30+
31+
# Initialize request argument(s)
32+
request = google_chat.CreateMessageRequest(
33+
# Replace SPACE_NAME here
34+
parent = "spaces/SPACE_NAME",
35+
message = {
36+
"text": "Hello with app credential!",
37+
"cards_v2" : [{
38+
"card_id": "card-id",
39+
"card": {
40+
"sections": [{
41+
"widgets": [{
42+
"button_list": {
43+
"buttons": [
44+
{
45+
"text": "Edit",
46+
"disabled": True,
47+
},
48+
{
49+
"icon": {
50+
"known_icon": "INVITE",
51+
"alt_text": "check calendar"
52+
},
53+
"on_click": {
54+
"open_link": {
55+
"url": "https://www.google.com"
56+
}
57+
}
58+
}
59+
]
60+
}
61+
}]
62+
}]
63+
}
64+
}]
65+
}
66+
)
67+
68+
# Make the request
69+
response = client.create_message(request)
70+
71+
# Handle the response
72+
print(response)
73+
74+
create_message_with_app_cred_with_cards()
75+
76+
# [END chat_create_message_app_cred_with_cards]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2024 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# It may require modifications to work in your environment.
17+
18+
# To install the latest published package dependency, execute the following:
19+
# python3 -m pip install google-apps-chat
20+
21+
# [START chat_create_message_user_cred]
22+
from authentication_utils import create_client_with_user_credentials
23+
from google.apps import chat_v1 as google_chat
24+
25+
SCOPES = ["https://www.googleapis.com/auth/chat.messages.create"]
26+
27+
def create_message_with_user_cred():
28+
# Create a client
29+
client = create_client_with_user_credentials(SCOPES)
30+
31+
# Initialize request argument(s)
32+
request = google_chat.CreateMessageRequest(
33+
# Replace SPACE_NAME here.
34+
parent = "spaces/SPACE_NAME",
35+
message = {
36+
"text": "Hello with user credential!"
37+
}
38+
)
39+
40+
# Make the request
41+
response = client.create_message(request)
42+
43+
# Handle the response
44+
print(response)
45+
46+
create_message_with_user_cred()
47+
48+
# [END chat_create_message_user_cred]

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleworkspace/python-samples/commit/823468ed7f0a311b2261bb769f5c0833575d0f12

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy