Google app scrips Slack API Get pins:list

Viewed 92

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!!

2 Answers

SUGGESTION

Upon reviewing Slack's official docs for pins.list method, I suppose that this sample JSON response below is the same as the actual JSON response that you're getting:

Sample JSON response:

{
    "items": [
        {
            "channel": "C2U86NC6H",
            "created": 1508881078,
            "created_by": "U2U85N1RZ",
            "message": {
                "permalink": "https://hitchhikers.slack.com/archives/C2U86NC6H/p1508197641000151",
                "pinned_to": [
                    "C2U86NC6H"
                ],
                "text": "What is the meaning of life?",
                "ts": "1508197641.000151",
                "type": "message",
                "user": "U2U85N1RZ"
            },
            "type": "message"
        },
        {
            "channel": "C2U86NC6H",
            "created": 1508880991,
            "created_by": "U2U85N1RZ",
            "message": {
                "permalink": "https://hitchhikers.slack.com/archives/C2U86NC6H/p1508284197000015",
                "pinned_to": [
                    "C2U86NC6H"
                ],
                "text": "The meaning of life, the universe, and everything is 42.",
                "ts": "1503289197.000015",
                "type": "message",
                "user": "U2U85N1RZ"
            },
            "type": "message"
        }
    ],
    "ok": true
}

You can try iterating though the items array via looping in the JSON response to get each permalinks data, as seen on this quick test below:

Quick Test

function GetPinns() {
  //This sample JSON String response was from https://api.slack.com/methods/pins.list#examples
  var json = 
  "{\"items\": [{\"channel\": \"C2U86NC6H\",\"created\": 1508881078,\"created_by\": \"U2U85N1RZ\",\"message\": {\"permalink\": \"https://hitchhikers.slack.com/archives/C2U86NC6H/p1508197641000151\",\"pinned_to\": [\"C2U86NC6H\"],\"text\": \"What is the meaning of life?\",\"ts\": \"1508197641.000151\",\"type\": \"message\",\"user\": \"U2U85N1RZ\"},\"type\": \"message\"},{\"channel\": \"C2U86NC6H\",\"created\": 1508880991,\"created_by\": \"U2U85N1RZ\",\"message\": {\"permalink\": \"https://hitchhikers.slack.com/archives/C2U86NC6H/p1508284197000015\",\"pinned_to\": [\"C2U86NC6H\"],\"text\": \"The meaning of life, the universe, and everything is 42.\",\"ts\": \"1503289197.000015\",\"type\": \"message\",\"user\": \"U2U85N1RZ\"},\"type\": \"message\"}],\"ok\": true}";

  var data = JSON.parse(json);

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

Result

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

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