How do I send a message with Blend Bot on Slack?

  • Page Owner: Not Set
  • Last Reviewed: 2021-08-26

I'd like to send notifications from DevOps after a build. How do I do it?


Answer

There are two tasks you'll want to add to your build. Here is the YAML version of each.

Success:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $headers = @{
          Authorization="Bearer ${env:TOKEN}"
      }
      
      $text = iex "`"${env:SLACK_SUCCESS_TEXT}`""
      
      $payload = @{
          "icon_emoji" = ":large_green_circle:"
          "text" = "${text}"
          "channel" = "${env:SLACK_CHANNEL}"
      }
      
      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
      Invoke-WebRequest `
          -UseBasicParsing `
          -Headers $headers `
          -Body (ConvertTo-Json -Compress -InputObject $payload) `
          -Method Post `
          -ContentType "application/json" `
          -Uri "https://slack.com/api/chat.postMessage"
    errorActionPreference: 'continue'
  env:
    TOKEN: $(SLACK_TOKEN)

Failure:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $headers = @{
          Authorization="Bearer ${env:TOKEN}"
      }
      
      $text = iex "`"${env:SLACK_FAILURE_TEXT}`""
      
      $payload = @{
          "icon_emoji" = ":red_circle:"
          "text" = "${text}"
          "channel" = "${env:SLACK_CHANNEL}"
      }
      
      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
      Invoke-WebRequest `
          -UseBasicParsing `
          -Headers $headers `
          -Body (ConvertTo-Json -Compress -InputObject $payload) `
          -Method Post `
          -ContentType "application/json" `
          -Uri "https://slack.com/api/chat.postMessage"
  env:
    TOKEN: $(SLACK_TOKEN)
  condition: failed()

Once those are set up, you'll need to add a few variables to your build pipeline:

  • slack_channel - The channel to post to. Should look something like: C026D7BJT
  • slack_failure_text - The message to send on failure. This can include interpolated strings. For example: Blend Developer Build Failed <https://dev.azure.com/BlendInteractive/Blend%20Developer%20Episerver%20Site/_build/results?buildId=$($env:Build_BuildId)&view=logs|View Log>
  • slack_success_text - The message to send on success. This can include interpolated strings. For example: Blend Developer Build Succeeded <https://dev.azure.com/BlendInteractive/Blend%20Developer%20Episerver%20Site/_build/results?buildId=$($env:Build_BuildId)&view=logs|View Log> | <https://developer.blendinteractive.com/|View Site>
  • slack_token - The Slack token to use for authentication. This can be found in 1password under BlendBot Slack Token.

NOTE: If the channel you're posting to is private, you will need to invite BlendBot to your channel before it can post there.

Comments

  • To find the slack channel ID, click the name of the channel, Select About, scroll to the bottom.