Slack extra feature to get different colours

slack
howto

#1

This useful tip was seen in the #community stackstorm channel by cygnetix. It allows extra.slack.color to hold a jinja templates but treated as a string by the StackStorm core. The template is processed by chatops.format_message's jinja engine.

Hey all. I’m trying to get an alias to show a different color in slack depending on the returned value. Slack looks like it’ll only work if the value is all one one line but I’m having some trouble getting my Jinja formatting to do this.

Here’s an example which fails because (I suspect) it introduces multiple lines in the color value:

    slack:
      color: |
        {%- if execution.result.positives > 0 -%}
        danger
        {%- else -%}
        good
        {%- endif -%}

This example results in a failure when I try to register the pack:

    slack:
      color: {%- if execution.result.positives > 0 -%} danger{%- else -%}good{%- endif -%}

Just FYI. I found a solution that works (use >-):

    slack:                                                                       
      color: >-                                                                  
        {%- if execution.result.result.results.positives == 0 -%}                
        good                                                                     
        {%- else -%}                                                             
        danger                                                                   
        {%- endif -%}

#2

Thanks! That’s a great use of the “block chomping indicator” from YAML. Glad you got it to work!