Skip to content

Commit 9d2691e

Browse files
authored
feat: Python Preview Links samples (#169)
1 parent 356b347 commit 9d2691e

4 files changed

Lines changed: 238 additions & 0 deletions

File tree

python/preview-links/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Preview Links with Smart Chips
2+
3+
<!-- TODO: Replace guide link -->
4+
For more information on preview link with Smart Chips, please read the [guide]().
5+
6+
This Cloud Function specifies link previews for two link preview triggers.
7+
Alternatively, you can specify a Cloud Function for each trigger.
8+
To learn about writing Cloud Functions,
9+
see the documentation: https://cloud.google.com/functions/docs/writing.
10+
11+
## Create and deploy a Cloud Function
12+
13+
### Turn on the Cloud Functions, Cloud Build, and the Add-ons API
14+
15+
```sh
16+
gcloud services enable cloudfunctions cloudbuild.googleapis.com gsuiteaddons.googleapis.com
17+
```
18+
19+
### Deploy the function
20+
21+
```sh
22+
gcloud functions deploy create_link_preview --runtime python311 --trigger-http
23+
```
24+
25+
## Create an add-on deployment
26+
27+
### Find the service account email for the add-on
28+
29+
```sh
30+
gcloud workspace-add-ons get-authorization
31+
```
32+
33+
### Grant the service account the ``cloudfunctions.invoker`` role
34+
35+
```sh
36+
gcloud functions add-iam-policy-binding create_link_preview \
37+
--role roles/cloudfunctions.invoker \
38+
--member serviceAccount:SERVICE_ACCOUNT_EMAIL
39+
```
40+
41+
### Get URL of the deployed function
42+
43+
```sh
44+
gcloud functions describe create_link_preview
45+
```
46+
47+
Replace `$URL` in deployment.json with the deployed function URL
48+
49+
### Create the deployment
50+
51+
```sh
52+
gcloud workspace-add-ons deployments create linkpreview \
53+
--deployment-file=deployment.json
54+
```
55+
56+
## Install the add-on
57+
58+
```sh
59+
gcloud workspace-add-ons deployments install linkpreview
60+
```
61+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"oauthScopes": [
3+
"https://www.googleapis.com/auth/workspace.linkpreview"
4+
],
5+
"addOns": {
6+
"common": {
7+
"name": "Preview support cases",
8+
"logoUrl": "https://developers.google.com/workspace/add-ons/images/link-icon.png",
9+
"layoutProperties": {
10+
"primaryColor": "#dd4b39"
11+
}
12+
},
13+
"docs": {
14+
"linkPreviewTriggers": [
15+
{
16+
"runFunction": "$URL",
17+
"patterns": [
18+
{
19+
"hostPattern": "example.com",
20+
"pathPrefix": "support/cases"
21+
},
22+
{
23+
"hostPattern": "*.example.com",
24+
"pathPrefix": "cases"
25+
},
26+
{
27+
"hostPattern": "cases.example.com"
28+
}
29+
],
30+
"labelText": "Support case",
31+
"localizedLabelText": {
32+
"es": "Caso de soporte"
33+
}
34+
},
35+
{
36+
"runFunction": "$URL",
37+
"patterns": [
38+
{
39+
"hostPattern": "example.com",
40+
"pathPrefix": "people"
41+
}
42+
],
43+
"labelText": "People",
44+
"localizedLabelText": {
45+
"es": "Personas"
46+
}
47+
}
48+
]
49+
}
50+
}
51+
}

python/preview-links/main.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License")
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https:#www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# [START add_ons_preview_link]
15+
16+
from typing import Any, Mapping
17+
from urllib.parse import urlparse
18+
19+
import flask
20+
import functions_framework
21+
22+
23+
@functions_framework.http
24+
def create_link_preview(req: flask.Request):
25+
"""Responds to any HTTP request.
26+
Args:
27+
req: HTTP request context.
28+
Returns:
29+
The response object.
30+
"""
31+
event = req.get_json(silent=True)
32+
if event["docs"]["matchedUrl"]["url"]:
33+
return create_card(event["docs"]["matchedUrl"]["url"])
34+
35+
36+
def create_card(url):
37+
"""Creates a preview link card for either a case link or people link.
38+
Args:
39+
url: The matched url.
40+
Returns:
41+
A case link preview card or a people link preview card.
42+
"""
43+
parsed_url = urlparse(url)
44+
if parsed_url.hostname != "www.example.com":
45+
return {}
46+
47+
if parsed_url.path.startswith("/support/cases/"):
48+
return case_link_preview(url)
49+
50+
if parsed_url.path.startswith("/people/"):
51+
return people_link_preview()
52+
53+
return {}
54+
55+
56+
# [START add_ons_case_preview_link]
57+
58+
59+
def case_link_preview(url):
60+
"""A support case link preview.
61+
Args:
62+
url: The case link.
63+
Returns:
64+
A case link preview card.
65+
"""
66+
67+
# Parses the URL to identify the case ID.
68+
segments = url.split("/")
69+
case_id = segments[-1]
70+
71+
# Returns the card.
72+
# Uses the text from the card's header for the title of the smart chip.
73+
return {
74+
"header": {"title": f"Case {case_id}: Title bar is broken."},
75+
"sections": [
76+
{
77+
"widgets": [
78+
{
79+
"textParagraph": {
80+
"text": "Customer can't view title on mobile device."
81+
}
82+
}
83+
]
84+
}
85+
],
86+
}
87+
88+
89+
# [END add_ons_case_preview_link]
90+
# [START add_ons_people_preview_link]
91+
92+
93+
def people_link_preview():
94+
"""An employee profile link preview.
95+
Returns:
96+
A people link preview card.
97+
"""
98+
99+
# Builds a preview card with an employee's name, title, email, and profile photo.
100+
# Returns the card. Uses the text from the card's header for the title of the smart chip.
101+
return {
102+
"header": {"title": "Rosario Cruz"},
103+
"sections": [
104+
{
105+
"widgets": [
106+
{
107+
"image": {
108+
"imageUrl": "https:#developers.google.com/workspace/add-ons/images/employee-profile.png"
109+
}
110+
},
111+
{
112+
"keyValue": {
113+
"icon": "EMAIL",
114+
"content": "rosario@example.com",
115+
"bottomLabel": "Case Manager",
116+
}
117+
},
118+
]
119+
}
120+
],
121+
}
122+
123+
124+
# [END add_ons_people_preview_link]
125+
# [END add_ons_preview_link]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask>=2.2.2

0 commit comments

Comments
 (0)