Does Slack support Markdown tables?
Asked Answered
B

5

74

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?

Bertram answered 23/4, 2019 at 17:16 Comment(3)
Note that Slack’s markup language is called mrkdown (missing “a”).Subjunction
Rather, it's called mrkdwn (missing "a" and "o").Pressey
for this, I use acii tables placed inside code blocks of slackInhere
G
81

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.

Gauntlet answered 23/4, 2019 at 18:42 Comment(0)
U
24

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.
Unfleshly answered 23/4, 2019 at 17:27 Comment(6)
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.Cochin
If the result is not shown within Slack I don't think it has a lot of value for OP.Unfleshly
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.Trichinopoly
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?Obsess
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.Dexamethasone
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.Bartholomew
R
14

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

Rivalee answered 29/6, 2022 at 11:51 Comment(1)
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.Pergolesi
I
7

As stated above Slack does not support Markdown, but as a workaround, you can convert your table to CSV and add as text snippet. It will look like this:

enter image description here

Infallibilism answered 22/3 at 11:1 Comment(1)
This seemed great, while I was working on my computer all day and testing on the desktop app. Unfortunately, this workaround does not work the same on mobile and tablet.Lunch
A
-4

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)
Aftertaste answered 22/12, 2022 at 22:51 Comment(2)
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.Gauntlet
Hey Chris, I have updated the code, please look at it now. I hope it would help you moreAftertaste

© 2022 - 2024 — McMap. All rights reserved.