64

I want to send a Markdown table to Slack with its postMessage API, but I got raw content in Slack instead of a rendered table. Does Slack support Markdown tables? Ir is there any other way to present tabular data in Slack? I know Slack doesn't support HTML.

I tried chat.postMessage and files.upload, also formatting text with fixed column length but it looks kind of ugly, so I am figuring out a way to make it look better.

Here is my HTTP request code, content-type is JSON:

url : https://slack.com/api/chat.postMessage
body :
{
    "channel": "RKAID4I",
    "text": " | Tables  | Are   | Cool  |
|---------- |:-------------:    |------:    |
| col 1 is  | left-aligned  | $1600     |
| col 2 is  | centered  | $12   |
| col 3 is  | right-aligned     | $1    |"

}

I was expect table like format, but the actual output is exactly as what I sent. Is it because my Markdown message is wrong or Slack simply doesn't support Markdown tables?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
cece2048
  • 1,121
  • 2
  • 11
  • 17

4 Answers4

69

No, in fact, Slack doesn't support Markdown in messages¹ at all. It uses its own similar-at-a-glance format called mrkdwn which has some notable differences with Markdown:

  • In Markdown, both * and _ are used for emphasis
  • In Markdown, both ** and __ are used for bold
  • In mrkdwn * is used for bold and _ is used for emphasis
  • Markdown has no syntax for strikethrough (though some implementations have added it, e.g. in GFM which uses ~~) but mrkdwn uses ~ for strikethrough
  • Link syntax is very different
  • mrkdwn doesn't support headings
  • Probably more

Don't expect arbitrary Markdown² to work in Slack messages.


¹Slack does support Markdown in posts which can be created using the files.upload API endpoint setting filetype to post.

²Note that tables aren't supported in regular Markdown either. Like strikethrough, some implementations have added these.

Michael J Swart
  • 3,060
  • 3
  • 29
  • 46
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • @MarcTamsky, I didn't say it supports tables. I said it supports _Markdown_, only in posts, which is different from messages where Markdown is not supported at all. A different syntax called `mrkdwn` is used for messages. (Note also that that link does not point to _Slack's_ documentation. It's some third-party site that may or may not be reliable. If Slack chooses to add table support at some point, they'll likely document that somewhere. The third-party site may remain out of date forever.) – ChrisGPT was on strike Feb 16 '23 at 14:43
22

Slack does not support rendering of tables so this markup will not work.

You have two alternatives:

  • You can use fields, which will be rendered as 2 columns on most devices. See fields in layout blocks.
  • You can convert your table into an image (outside Slack) and
    attach the image to your message.
Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • 3
    You can also paste the Markdown table in a GitHub gist (with the .md filename extension) and, in Slack, point to that URL. Markdown tables are nicely formatted in gist. Unfortunately Slack won't give any preview, only the bare link. – Nicolas Le Thierry d'Ennequin May 29 '19 at 15:57
  • 16
    If the result is not shown within Slack I don't think it has a lot of value for OP. – Erik Kalkoken May 29 '19 at 16:00
  • 9
    For completeness, you should know that using images/screenshots within Slack messages will make them unable to read by those relying on screen reader technologies such as blind or vision-impaired users. Always try other, more accessible methods before resorting to screenshots, unless you know that all the current and future recipients are able to view the image. – GregL Aug 23 '21 at 21:10
  • 1
    So... is there a way to submit a regular message in Slack chat that uses fields? Or this is only available as output from APIs/integrations? – Steven Lu Mar 04 '22 at 09:18
  • I happened to have a use case for a two-column table. I used fields, just to find out that fields are limited to 10 elements (5 rows) per section. So, you'll need to create a new section element every 5th element, which adds extra whitespace. – aJetHorn Mar 31 '22 at 19:01
  • Padding cell values with space characters improves readability. Using helper columns in Excel use the Rept() function to add spaces and subtract the cell chr length using Len() to determine the correct number of spaces to be added. When pasting, set the snippet type to Spreadsheet. Consider adding placeholder values for empty fields. Downside, besides these additional steps, is data may be appended with a 'truncated .. see it in full' link or outright post denial from exceeding the max snippet size which at that point an attachment probably makes more sense. – Brad Smith Apr 07 '22 at 13:31
8

Add ``` before and after your table in your str message. it will convert in code block in slack meassge.

  • 2
    This is actually a good answer. Making your markdown table into a code block will preserve the spacing, so it'll look like you expect it look. It just won't be rendered into any sort of proper table. – Adam_G Aug 11 '22 at 20:53
-3

Slack can support Markdown now though, I tried and it worked

def notifySlackChannel(content):
    df_markdown=df_variance.to_markdown(tablefmt="grid")
    def notifySlackChannel(content):
    url = SlackWebHookUrl
    request_body_json = {'channel': ChannelName, 'text': table_time_info +content}
    response = requests.post(url, json=request_body_json)


notifySlackChannel(df_markdown)
will7200
  • 1,496
  • 1
  • 13
  • 23
  • Just looking at this code snippet, I'm not sure what is going on. It doesn't compile due to the wrong indentation of the nested `notifySlackChannel` function. There's two different `notifySlackChannel` functions, and the first one just makes a method call and does nothing with the result. What is `df_variance`? What is `table_time_info`? There's no information explaining the answer. – Chris Apr 26 '23 at 12:53
  • Hey Chris, I have updated the code, please look at it now. I hope it would help you more – Sagar Rawal May 30 '23 at 09:48