From 6b9698836c2bdca0b11d659900a1773d858ef63f Mon Sep 17 00:00:00 2001 From: Steve Bazyl Date: Fri, 9 Jun 2023 14:51:05 -0600 Subject: [PATCH] feat: Import smartchip sample --- solutions/add-on/book-smartchip/.clasp.json | 1 + solutions/add-on/book-smartchip/Code.js | 53 +++++++++++++++++++ .../add-on/book-smartchip/appsscript.json | 40 ++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 solutions/add-on/book-smartchip/.clasp.json create mode 100644 solutions/add-on/book-smartchip/Code.js create mode 100644 solutions/add-on/book-smartchip/appsscript.json diff --git a/solutions/add-on/book-smartchip/.clasp.json b/solutions/add-on/book-smartchip/.clasp.json new file mode 100644 index 000000000..490e7e41d --- /dev/null +++ b/solutions/add-on/book-smartchip/.clasp.json @@ -0,0 +1 @@ +{"scriptId":"14tK6PD4C624ivRyGk-S6eYCbYJnDfA24xeP0Jhb1U8sPgAvZXeZm5gpb"} diff --git a/solutions/add-on/book-smartchip/Code.js b/solutions/add-on/book-smartchip/Code.js new file mode 100644 index 000000000..f054c4d18 --- /dev/null +++ b/solutions/add-on/book-smartchip/Code.js @@ -0,0 +1,53 @@ +function getBook(id) { + const apiKey = 'YOUR_API_KEY'; // Replace with your API key + const apiEndpoint = `https://www.googleapis.com/books/v1/volumes/${id}?key=${apiKey}&country=US`; + const response = UrlFetchApp.fetch(apiEndpoint); + return JSON.parse(response); +} + +function bookLinkPreview(event) { + if (event.docs.matchedUrl.url) { + const segments = event.docs.matchedUrl.url.split('/'); + const volumeID = segments[segments.length - 1]; + + const bookData = getBook(volumeID); + const bookTitle = bookData.volumeInfo.title; + const bookDescription = bookData.volumeInfo.description; + const bookImage = bookData.volumeInfo.imageLinks.small; + const bookAuthors = bookData.volumeInfo.authors; + const bookPageCount = bookData.volumeInfo.pageCount; + + const previewHeader = CardService.newCardHeader() + .setSubtitle('By ' + bookAuthors) + .setTitle(bookTitle); + + const previewPages = CardService.newDecoratedText() + .setTopLabel('Page count') + .setText(bookPageCount); + + const previewDescription = CardService.newDecoratedText() + .setTopLabel('About this book') + .setText(bookDescription).setWrapText(true); + + const previewImage = CardService.newImage() + .setAltText('Image of book cover') + .setImageUrl(bookImage); + + const buttonBook = CardService.newTextButton() + .setText('View book') + .setOpenLink(CardService.newOpenLink() + .setUrl(event.docs.matchedUrl.url)); + + const cardSectionBook = CardService.newCardSection() + .addWidget(previewImage) + .addWidget(previewPages) + .addWidget(CardService.newDivider()) + .addWidget(previewDescription) + .addWidget(buttonBook); + + return CardService.newCardBuilder() + .setHeader(previewHeader) + .addSection(cardSectionBook) + .build(); + } +} diff --git a/solutions/add-on/book-smartchip/appsscript.json b/solutions/add-on/book-smartchip/appsscript.json new file mode 100644 index 000000000..4e8538061 --- /dev/null +++ b/solutions/add-on/book-smartchip/appsscript.json @@ -0,0 +1,40 @@ +{ + "timeZone": "America/Los_Angeles", + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8", + "oauthScopes": [ + "https://www.googleapis.com/auth/workspace.linkpreview", + "https://www.googleapis.com/auth/script.external_request" + ], + "addOns": { + "common": { + "name": "Preview Books Add-on", + "logoUrl": "https://developers.google.com/workspace/add-ons/images/library-icon.png", + "layoutProperties": { + "primaryColor": "#dd4b39" + } + }, + "docs": { + "linkPreviewTriggers": [ + { + "runFunction": "bookLinkPreview", + "patterns": [ + { + "hostPattern": "*.google.*", + "pathPrefix": "books" + }, + { + "hostPattern": "*.google.*", + "pathPrefix": "books/edition" + } + ], + "labelText": "Book", + "logoUrl": "https://developers.google.com/workspace/add-ons/images/book-icon.png", + "localizedLabelText": { + "es": "Libros" + } + } + ] + } + } +} \ No newline at end of file