Slack - Send Notification using Block Kit

Octopus.Script exported 2023-06-02 by justin-newman belongs to ‘Slack’ category.

Send a message notification to Slack using the Block Kit formatting. These messages will be limited to more basic formats (e.g., using functions and inputs probably won’t work), but you still will be able to make much nicer looking messages this way with the ability to preview them using the Block Kit Builder.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Hook URL

ssn_HookUrl =

The Webhook URL provided by Slack In the Incoming Webhook settings. This will provide the channel, Icon, and Username for the message by default.

Slack Channel

ssn_Channel =

Which Slack channel to post notification to. This will be ignored if using a webhook from an app.

Username

ssn_Username = Octopus Deploy

The username shown in Slack against the notification. This will be ignored if using a webhook from an app.

Icon URL

ssn_IconUrl = https://octopus.com/content/resources/favicon.png

The icon shown in Slack against the notification. This will be ignored if using a webhook from an app.

Block Object

ssn_BlockObj =

Paste the entire JSON object from the Block Kit Builder here.

Script body

Steps based on this template will execute the following PowerShell script.

$payload = ($OctopusParameters['ssn_BlockObj'] | ConvertFrom-Json)
$payload | Add-Member -MemberType NoteProperty -Name channel -Value $OctopusParameters['ssn_Channel']
$payload | Add-Member -MemberType NoteProperty -Name username -Value $OctopusParameters['ssn_Username']
$payload | Add-Member -MemberType NoteProperty -Name icon_url -Value $OctopusParameters['ssn_IconUrl']
$payload | Add-Member -MemberType NoteProperty -Name link_names -Value "true"

try {
	[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12
    if ($PSVersionTable.PSVersion.Major -ge 6)
    {
        Invoke-Restmethod -Method POST -Body ($payload | ConvertTo-Json -Depth 10) -Uri $OctopusParameters['ssn_HookUrl']
    }
    else
    {
        Invoke-Restmethod -Method POST -Body ($payload | ConvertTo-Json -Depth 10) -Uri $OctopusParameters['ssn_HookUrl'] -UseBasicParsing
    }
} catch {
    Write-Host "An error occurred while attempting to send Slack notification"
    Write-Host $_.Exception
    Write-Host $_
    throw
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "ed168ccc-a9b1-4990-86a6-cb6606c88d02",
  "Name": "Slack - Send Notification using Block Kit",
  "Description": "Send a message notification to Slack using the Block Kit formatting. These messages will be limited to more basic formats (e.g., using functions and inputs probably won't work), but you still will be able to make much nicer looking messages this way with the ability to preview them using the [Block Kit Builder](https://app.slack.com/block-kit-builder).",
  "Version": 2,
  "ExportedAt": "2023-06-02T20:17:55.244Z",
  "ActionType": "Octopus.Script",
  "Author": "justin-newman",
  "Packages": [],
  "Parameters": [
    {
      "Id": "b73ac3c2-1dc9-4196-9d87-71f03a0fb0fc",
      "Name": "ssn_HookUrl",
      "Label": "Hook URL",
      "HelpText": "The Webhook URL provided by Slack In the Incoming Webhook settings. This will provide the channel, Icon, and Username for the message by default.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Id": "c6c11feb-2613-4583-9fcf-3bc379936c11",
      "Name": "ssn_Channel",
      "Label": "Slack Channel",
      "HelpText": "Which Slack channel to post notification to. *This will be ignored if using a webhook from an app.*",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "dba932f9-8a73-4628-9b6f-b5850c29fd46",
      "Name": "ssn_Username",
      "Label": "Username",
      "HelpText": "The username shown in Slack against the notification. *This will be ignored if using a webhook from an app.*",
      "DefaultValue": "Octopus Deploy",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "9ca22abf-478c-4315-984f-681fa6d6c80c",
      "Name": "ssn_IconUrl",
      "Label": "Icon URL",
      "HelpText": "The icon shown in Slack against the notification. *This will be ignored if using a webhook from an app.*",
      "DefaultValue": "https://octopus.com/content/resources/favicon.png",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "6a80e10f-d09f-4765-84ba-aed6a3ee2abc",
      "Name": "ssn_BlockObj",
      "Label": "Block Object",
      "HelpText": "Paste the entire JSON object from the [Block Kit Builder](https://app.slack.com/block-kit-builder) here.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "$payload = ($OctopusParameters['ssn_BlockObj'] | ConvertFrom-Json)\n$payload | Add-Member -MemberType NoteProperty -Name channel -Value $OctopusParameters['ssn_Channel']\n$payload | Add-Member -MemberType NoteProperty -Name username -Value $OctopusParameters['ssn_Username']\n$payload | Add-Member -MemberType NoteProperty -Name icon_url -Value $OctopusParameters['ssn_IconUrl']\n$payload | Add-Member -MemberType NoteProperty -Name link_names -Value \"true\"\n\ntry {\n\t[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12\n    if ($PSVersionTable.PSVersion.Major -ge 6)\n    {\n        Invoke-Restmethod -Method POST -Body ($payload | ConvertTo-Json -Depth 10) -Uri $OctopusParameters['ssn_HookUrl']\n    }\n    else\n    {\n        Invoke-Restmethod -Method POST -Body ($payload | ConvertTo-Json -Depth 10) -Uri $OctopusParameters['ssn_HookUrl'] -UseBasicParsing\n    }\n} catch {\n    Write-Host \"An error occurred while attempting to send Slack notification\"\n    Write-Host $_.Exception\n    Write-Host $_\n    throw\n}"
  },
  "Category": "Slack",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/slack-send-notification-using-block-kit.json",
  "Website": "/step-templates/ed168ccc-a9b1-4990-86a6-cb6606c88d02",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5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XUDCu6iFuvVSIPY9tl9033KvBeI3M0sZJGyWEO3f6PEbrRD9Hxvy7Nr3yUCC34ZD/+efFkhKcOT4HStbfH1kAmluX0n2rnldkHP9+P2bXS40zWuDlI8KUkEqSAWpIBWkglSQClJBKkgFqSAVpIJUkKzxX4ABALbWvZucWNySAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Friday, June 2, 2023