You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add your integration by name (give it full access).
Step 3: Find Your Database ID
Open your database as a full page in Notion.
Copy the URL. The Database ID is the long string after the last dash (-).
Example: abcdef1234567890abcdef1234567890
Step 4: Add the Script to Scriptable
Copy the code below into a new script in Scriptable.
Replace YOUR_DATABASE_ID and YOUR_SECRET_TOKEN with your values.
// Notion settingsconstdatabaseID='YOUR_DATABASE_ID';consttoken='YOUR_SECRET_TOKEN';constnotionVersion='2022-06-28';constnotionApi=`https://api.notion.com/v1/databases/${databaseID}/query`;// Main function(async()=>{letquote=awaitreadNotionQuote();if(!quote){console.log('No quote found');return;}letwidget=awaitcreateWidget(quote.text,quote.author,quote.book,quote.tags);if(config.runsInWidget){Script.setWidget(widget);}else{awaitwidget.presentMedium();}Script.complete();})();asyncfunctionreadNotionQuote(){letreq=newRequest(notionApi);req.method='POST';req.headers={"Authorization": `Bearer ${token}`,"Notion-Version": notionVersion,"Content-Type": "application/json"};req.body=JSON.stringify({page_size: 100});letres=awaitreq.loadJSON();if(!res.results||res.results.length===0){returnnull;}letquotes=res.results.map(page=>{letquoteText='';if(page.properties.title&&Array.isArray(page.properties.title.title)){quoteText=page.properties.title.title.map(t=>t.plain_text).join('');}elseif(page.properties.Quote&&Array.isArray(page.properties.Quote.title)){quoteText=page.properties.Quote.title.map(t=>t.plain_text).join('');}letauthorName=page.properties.Author?.select?.name||'';letbookTitle=page.properties.Book?.select?.name||'';letrating=(page.properties.Rating?.select?.name||'').length;lettags=page.properties.Tags?.multi_select?.map(tag=>tag.name)||[];return{text: quoteText,author: authorName,book: bookTitle,rating: rating,tags: tags};}).filter(quote=>quote.text.trim()!==''&"e.author.trim()!=='');if(quotes.length===0){returnnull;}// Weighted random selection based on ratinglettotalWeight=quotes.reduce((sum,quote)=>sum+Math.pow(2,quote.rating),0);letrandomWeight=Math.random()*totalWeight;letweightSum=0;for(letquoteofquotes){weightSum+=Math.pow(2,quote.rating);if(weightSum>randomWeight){returnquote;}}// Fallback to random selection if something goes wrongreturnquotes[Math.floor(Math.random()*quotes.length)];}asyncfunctioncreateWidget(text,author,book,tags){letwidget=newListWidget();widget.backgroundColor=newColor("#1C1C1E");letquoteText=widget.addText(`"${text}"`);quoteText.centerAlignText();quoteText.font=Font.boldSystemFont(16);quoteText.textColor=newColor("#FFFFFF");quoteText.minimumScaleFactor=0.5;widget.addSpacer(8);letauthorText=widget.addText(`- ${author}`);authorText.centerAlignText();authorText.font=Font.boldSystemFont(14);authorText.textColor=newColor("#AAAAAA");authorText.minimumScaleFactor=0.5;if(book){widget.addSpacer(4);letbookText=widget.addText(book);bookText.centerAlignText();bookText.font=Font.italicSystemFont(12);bookText.textColor=newColor("#AAAAAA");bookText.minimumScaleFactor=0.5;}if(tags.length>0){widget.addSpacer(4);lettagsText=widget.addText(`Tags: ${tags.join(', ')}`);tagsText.centerAlignText();tagsText.font=Font.systemFont(12);tagsText.textColor=newColor("#888888");tagsText.minimumScaleFactor=0.5;}widget.refreshAfterDate=newDate(Date.now()+1000*60*60);returnwidget;}
Troubleshooting
Check your Database ID, Secret Token, and Database Field Names.
Make sure your database is connected to the correct integration.
If you see "No Quote Found," check your field names and database setup.