How can I color part of Slack incoming-webhook messages?
Asked Answered
J

3

27

I want to compose a python script which sends a color-formatted message to a configured slack channel. I managed to send sentences of plain texts to slack channels but I could not figure out how to confer part of the sentences colors that I want.

I used slackweb library in python as below

import slackweb
slack = slackweb.Slack(url = url_of_the_configured_channel)
string_to_send = "test TEST"
payload = {'text' : string_to_send}
slack.notify(text=string_to_send)

how can I, say, format string_to_send or payload so that the word 'test' appears blue and 'TEST' appears red in the slack channel?

Thanks in advance.

Jochebed answered 15/2, 2017 at 7:29 Comment(4)
Are you sure you can do this at all?Offprint
Um... I heard it is possible, but i'm never sureJochebed
Well. I can't find this option on slack web or desktop client. And in bots APIOffprint
You can abuse the backtick "code" quotes ("`") to make text red.Promotion
T
29

Unfortunately it is at the moment not possible to color text of a Slack message with the API. You can use different styles, e.g. bold and italic, but not colors. (See Message Formatting in Slack documentation on how to use styles.)

You can use colors for your attachments. That will however result in a vertical color bar marker for the whole attachment, but not in colored text. (See Attachment Parameters in Slack documentation on how to color your attachments.)

Here is an example on how to use colors for Slack attachments (taken from the Slack documentation):

{
    "attachments": [
        {
            "fallback": "New ticket from Andrea Lee - Ticket #1943: Can't rest my password - https://groove.hq/path/to/ticket/1943",
            "pretext": "New ticket from Andrea Lee",
            "title": "Ticket #1943: Can't reset my password",
            "title_link": "https://groove.hq/path/to/ticket/1943",
            "text": "Help! I tried to reset my password but nothing happened!",
            "color": "#7CD197"
        }
    ]
}
Trituration answered 15/2, 2017 at 14:13 Comment(2)
Is there any explanation as to why? It seems like quite a simple thingEgghead
There is built-in "Create a text snippet" app that posts syntax-highlighted code as a selectable text so it should be possible somehow.Moderation
M
7

What you can do (as an admittedly clumsy workaround until/unless Slack supports more than primitive formatting) is:

  • Create your message as a small graphic image on your server
  • upload the image's web address to the slack channel using the webhook
  • the message is now displayed in channel

You can use ImageMagick for this.

.png images allow for transparency, so you don't have to worry too much about background matching.

This is, of course, a terribly inefficient way to go about it, but it has the agreeable characteristic of doing what you want.

Monostrophe answered 15/2, 2017 at 16:30 Comment(0)
N
0

For adding a color you need to add a attachment block to send message.

def slack_message(data):
        payload  = json.dumps(data)
        headers = { 'Content-type': 'application/json'}
        response = requests.request(url=slack_endpoint, method="post", headers=headers, data=payload)

You can send the request to slack_message in the below format:

slack_message(
{
   "attachments": [
      {
          "color": "#46567f",
          "blocks": [
           {
               "type": "section",
               "text": {
               "type": "mrkdwn",
               "text": f"Message"
               }
           }
         ]
       }
    ]
  }   
)

You can use this function to send slack message with color for a slack webhook generated for a channel.

Niigata answered 16/2 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.