Skip to content

Commit

Permalink
Upgrade preview-link code sample for Node to include create 3P resources
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrick committed Jan 18, 2024
1 parent 4b23ceb commit c11b00e
Show file tree
Hide file tree
Showing 8 changed files with 479 additions and 198 deletions.
9 changes: 6 additions & 3 deletions apps-script/3p-resources/3p-resources.gs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ function caseLinkPreview(event) {
// If the event object URL matches a specified pattern for support case links.
if (event.docs.matchedUrl.url) {

// Uses the event object to parse the URL and identify the case ID.
// Uses the event object to parse the URL and identify the case details.
const segments = event.docs.matchedUrl.url.split('/');
const caseDetails = JSON.parse(decodeURIComponent(segments[segments.length - 1]));

// Builds a preview card with the case ID, title, and description
// Builds a preview card with the case name, and description
const caseHeader = CardService.newCardHeader()
.setTitle(`Case: ${caseDetails.name}`);
const caseDescription = CardService.newTextParagraph()
Expand Down Expand Up @@ -254,7 +254,10 @@ function createLinkRenderAction(title, url) {
return {
renderActions: {
action: {
links: [{ title, url }]
links: [{
title: title,
url: url
}]
}
}
};
Expand Down
9 changes: 0 additions & 9 deletions apps-script/3p-resources/appsscript.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@
},
"runFunction": "createCaseInputCard",
"logoUrl": "https://developers.google.com/workspace/add-ons/images/support-icon.png"
},
{
"id": "createHotlist",
"labelText": "Create a hotlist",
"localizedLabelText": {
"es": "Crear una hotlist"
},
"runFunction": "createHotlistInputCard",
"logoUrl": "https://developers.google.com/workspace/add-ons/images/link-icon.png"
}
]
}
Expand Down
82 changes: 82 additions & 0 deletions node/3p-resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Third-Party Resources

The solution is made of two Cloud Functions, one for the two link preview triggers and
one for the third-party resource create action trigger.
To learn about writing Cloud Functions,
see the documentation: https://cloud.google.com/functions/docs/writing.

For more information on preview link with Smart Chips, please read the
[guide](https://developers.google.com/apps-script/add-ons/editors/gsao/preview-links).

For more information on creating third-party resources from the @ menu, please read the
[guide](https://developers.devsite.corp.google.com/workspace/add-ons/guides/create-insert-resource-smart-chip).

## Create and deploy the Cloud Functions

### Turn on the Cloud Functions, Cloud Build, and the Add-ons API

```sh
gcloud services enable cloudfunctions cloudbuild.googleapis.com gsuiteaddons.googleapis.com
```

### Deploy the functions

```sh
gcloud functions deploy createLinkPreview --runtime nodejs16 --trigger-http
gcloud functions deploy create3pResources --runtime nodejs16 --trigger-http
```

### Set the URL of the create3pResources function

```sh
gcloud functions describe create3pResources
```

Run the following command after having replaced `$URL` with the deployed
function URL retrieved previously to set the environment variable `URL`.

```sh
gcloud functions deploy create3pResources --update-env-vars URL=$URL
```

## Create an add-on deployment

### Find the service account email for the add-on

```sh
gcloud workspace-add-ons get-authorization
```

### Grant the service account the ``cloudfunctions.invoker`` role

```sh
gcloud functions add-iam-policy-binding createLinkPreview \
--role roles/cloudfunctions.invoker \
--member serviceAccount:SERVICE_ACCOUNT_EMAIL
gcloud functions add-iam-policy-binding create3pResources \
--role roles/cloudfunctions.invoker \
--member serviceAccount:SERVICE_ACCOUNT_EMAIL
```

### Set the URLs of the deployed functions

```sh
gcloud functions describe createLinkPreview
gcloud functions describe create3pResources
```

Replace `$URL1` in deployment.json with the first deployed function URL
and replace `$URL2` in deployment.json with the second deployed function URL.

### Create the deployment

```sh
gcloud workspace-add-ons deployments create manageSupportCases \
--deployment-file=deployment.json
```

## Install the add-on

```sh
gcloud workspace-add-ons deployments install manageSupportCases
```
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"oauthScopes": [
"https://www.googleapis.com/auth/workspace.linkpreview"
"https://www.googleapis.com/auth/workspace.linkpreview",
"https://www.googleapis.com/auth/workspace.linkcreate"
],
"addOns": {
"common": {
"name": "Preview support cases",
"logoUrl": "https://developers.google.com/workspace/add-ons/images/link-icon.png",
"name": "Manage support cases",
"logoUrl": "https://developers.google.com/workspace/add-ons/images/support-icon.png",
"layoutProperties": {
"primaryColor": "#dd4b39"
}
},
"docs": {
"linkPreviewTriggers": [
{
"runFunction": "$URL",
"runFunction": "$URL1",
"patterns": [
{
"hostPattern": "example.com",
Expand All @@ -34,7 +35,7 @@
"logoUrl": "https://developers.google.com/workspace/add-ons/images/support-icon.png"
},
{
"runFunction": "$URL",
"runFunction": "$URL1",
"patterns": [
{
"hostPattern": "example.com",
Expand All @@ -47,6 +48,17 @@
},
"logoUrl": "https://developers.google.com/workspace/add-ons/images/person-icon.png"
}
],
"createActionTriggers": [
{
"id": "createCase",
"labelText": "Create support case",
"localizedLabelText": {
"es": "Crear caso de soporte"
},
"runFunction": "$URL2",
"logoUrl": "https://developers.google.com/workspace/add-ons/images/support-icon.png"
}
]
}
}
Expand Down
Loading

0 comments on commit c11b00e

Please sign in to comment.