SmartBlocks icon indicating copy to clipboard operation
SmartBlocks copied to clipboard

Basic Giphy Integration

Open achalaggarwal opened this issue 5 years ago • 2 comments

✂️ Copy of your #42SmartBlock from Roam

- giphy #42SmartBlock
    - <%NOBLOCKOUTPUT%><%SET:search,<%INPUT:What GIF would you like?%>%>
    - <%NOBLOCKOUTPUT%><%JAVASCRIPT:```javascript
//get your API key from 
//https://developers.giphy.com/dashboard/?create=true
var key = '<---- PASTE API KEY HERE ---->';
var url = "https://api.giphy.com/v1/gifs/search?api_key="+ key + "&q=" + encodeURIComponent(roam42.smartBlocks.activeWorkflow.vars["search"]) + "&limit=1&offset=0&rating=r&lang=en";
var responseJSON = $.ajax({url:url, type:"GET", async:false, 
}).responseJSON;
roam42.smartBlocks.activeWorkflow.vars["gif"] =  responseJSON.data[0].images.original.url;
return '';```%>
    - ![](<%GET:gif%>)

📋 Describe the SmartBlock

Simple Giphy SB to pull the first gif based on the search term into your graph in the ![]() format

✅ Describe any prerequisites or dependencies that are required for this SmartBlock

📷 Screenshot of your #42SmartBlock workflow/template from Roam

image

💡 Additional Info

8roY9h

achalaggarwal avatar Dec 28 '20 16:12 achalaggarwal

Awesome SB! Will add this to my DNP for some daily inspiration :-)

I tweaked it a little for my needs. Here is a revision:

  • giphy #42SmartBlock
    
    • <%NOBLOCKOUTPUT%><%SET:search,<%INPUT:What GIF would you like?%>%>
      
    • <%NOBLOCKOUTPUT%><%JAVASCRIPTASYNC:```javascript
      //get your API key from //https://developers.giphy.com/dashboard/?create=true 
      var key = '--> your API key here <---'; 
      var url = "https://api.giphy.com/v1/gifs/search?api_key="+ key + "&q=" +
                  encodeURIComponent(roam42.smartBlocks.activeWorkflow.vars["search"]) + 
                  "&limit=1&offset=0&rating=r&lang=en"; 
      
      var responseJSON = await $.ajax({url:url, type:"GET", async:false, }).responseJSON; 
      roam42.smartBlocks.activeWorkflow.vars["gif"] = responseJSON.data[0].images.original.url; 
      
      return '';```%>
      
    • ![](<%GET:gif%>)
      

TfTHacker avatar Dec 28 '20 17:12 TfTHacker

Updated it to pick a random in the first 10 results

- giphy #42SmartBlock
    - <%NOBLOCKOUTPUT%><%SET:search,<%INPUT:What GIF would you like?%>%>
    - <%NOBLOCKOUTPUT%><%JAVASCRIPT:```javascript
//get your API key from 
//https://developers.giphy.com/dashboard/?create=true
var key = '--> your API key here <---';
var url = "https://api.giphy.com/v1/gifs/search?api_key="+ key + "&q=" + encodeURIComponent(roam42.smartBlocks.activeWorkflow.vars["search"]) + "&limit=10&offset=0&rating=r&lang=en";
var responseJSON = $.ajax({url:url, type:"GET", async:false, 
}).responseJSON;
var length = responseJSON.data.length;
var idx = Math.floor(Math.random()*length);
roam42.smartBlocks.activeWorkflow.vars["gif"] =  responseJSON.data[idx].images.original.url;
return '';```%>
    - ![](<%GET:gif%>)

achalaggarwal avatar Dec 28 '20 21:12 achalaggarwal