Home > other >  How to Send Hyperlinked text from Google sheet to Slack?
How to Send Hyperlinked text from Google sheet to Slack?

Time:08-03

I want to send a hyperlinked text from Google sheet to Slack, for that, I have been able to develop this code using an online resource:

function sendNotificationtoSlack() {
  
  var fileLink = SpreadsheetApp.getActiveSheet().getRange(5,2).getValue(); 
  var url = 'https://hooks.slack.com/XXXXX'
  var payload ={
          text: fileLink
          }
          
 var headers = {
            'Content-type':'application/json'
          }
         
 var options = {
            headers:headers,
            method:'POST',
            payload:JSON.stringify(payload) 
          }
        
  UrlFetchApp.fetch(url,options) }

The hyperlinked text in the sheet looks like this:

enter image description here

And in slack it comes in just plain text and is unclickable like this:

enter image description here

This source might be of any help. Can you guide me on how to send hyperlinked text in slack, so that it is clickable in the slack channel? Please let me know if you have any questions. Thank you

CodePudding user response:

You can add hyperlink via attachment with this syntax <url|text>

Try this way (and adapt to your speadsheet)

function slack() {
  let message = {
    "blocks": [
      {
        "type": "section",
        "text": {
          "type": "mrkdwn",
          "text": "my question"
        }
      }
    ],
    "attachments": [
      {
        "blocks": [
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": "<https://stackoverflow.com/questions/73218375/how-to-send-hyperlinked-text-from-google-sheet-to-slack|How to Send Hyperlinked text from Google sheet to Slack?"
            }
          }
        ]
      }
    ]
  };
  const webhook = ""; //Paste your webhook URL here
  sendSlackAlert(webhook, message)
}
function sendSlackAlert(webhook, payload) {
  var options = {
    "method": "post",
    "contentType": "application/json",
    "muteHttpExceptions": true,
    "payload": JSON.stringify(payload)
  };
  try {
    UrlFetchApp.fetch(webhook, options);
  } catch (e) {
    Logger.log(e);
  }
}

CodePudding user response:

I found a way to attach the link to the text while sending it to slack, here is the workaround:

function sendNotificationtoSlack() {
  
  var fileName = SpreadsheetApp.getActiveSheet().getRange(5,2).getValue(); 
  var url = 'https://hooks.slack.com/XXXXX'
  var payload = {
          text:  'Someone *Updated* the Sheet! <'   'TextLink'  "|" fileName ">"
          }
          
  var headers = {
            'Content-type':'application/json'
          }
         
  var options = {
            headers:headers,
            method:'POST',
            payload:JSON.stringify(payload) 
          }
        
  UrlFetchApp.fetch(url,options)
 }

This will send text with a URL attached with it

  • Related