Home > Blockchain >  Google app scrips Slack API Get pins:list
Google app scrips Slack API Get pins:list

Time:06-28

Using Google app script as serverless for a slack bot. Having an issue returning specific values from slack API. I'm using the pins:list call. I am able to get the JSON in response and items calls but get null when trying to get the next set of values. I am looking to return "permalinks" so I can then post back into slack what items are pinned to a room. here is my script:(without giving away company details)

function GetPinns() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()

  let url = "https://slack.com/api/pins.list?channel=C0XXXXXXXXX&pretty=1";

  let payload = {
    "ok": true,
     "channel": "C0XXXXXXXXX"
     "type": "message",
  
  }

var options = {
  "method": "get",
  "payload": JSON.stringify(payload),
  "headers": {
  "Content-type": "application/json; charset=utf-8",
  "Authorization": "Bearer xoxb-"}}
                    
   var response = UrlFetchApp.fetch(url, options)
  var json = response.getContentText();
  var data = JSON.parse(json);
  var items = data.item.permalinks;


Logger.log(items);

}

Thank you!!

CodePudding user response:

SUGGESTION

Upon reviewing Slack's enter image description here

Your script will look like this:

function GetPinns() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()

  let url = "https://slack.com/api/pins.list?channel=C0XXXXXXXXX&pretty=1";

  let payload = {
    "ok": true,
    "channel": "C0XXXXXXXXX"
         "type": "message",

  }

  var options = {
    "method": "get",
    "payload": JSON.stringify(payload),
    "headers": {
      "Content-type": "application/json; charset=utf-8",
      "Authorization": "Bearer xoxb-"
    }
  }

  var data = JSON.parse(json);

  //Iterate through the items via looping
  data.items.forEach(item => {
    Logger.log(item.message.permalink)
  });

}

Reference

CodePudding user response:

Thank you!!

I wound up with this in the end

 var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
var items = json.items

var linkList = ""
for(var x in items) {
  var link = items[x]["message"]["permalink"]
  var text = items[x]["message"]["text"]
  linkList  = "<"   link  "|"   text  ">"   "\n"
}
  • Related