Skip to content

Commit

Permalink
feat: Import smartchip sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sqrrrl committed Jun 9, 2023
1 parent 25dad78 commit 6b96988
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions solutions/add-on/book-smartchip/.clasp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"scriptId":"14tK6PD4C624ivRyGk-S6eYCbYJnDfA24xeP0Jhb1U8sPgAvZXeZm5gpb"}
53 changes: 53 additions & 0 deletions solutions/add-on/book-smartchip/Code.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
40 changes: 40 additions & 0 deletions solutions/add-on/book-smartchip/appsscript.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
}
}

0 comments on commit 6b96988

Please sign in to comment.