Trying to figure out a way to upload a Markdown file to be a formatted post in a channel. Is there an API call to do this?
The files.upload seems to only support Markdown raw upload.
Slack is a popular team messaging and collaboration application that supports a subset of the Markdown syntax. Different parts of the interface provide different levels of Markdown support.
To do so, open Slack's preferences menu by clicking the menu at the top-left corner of the Slack window and selecting “Preferences.” Click “Advanced” in the left pane and enable “Format messages with markup” under Input Options. The change will take effect immediately.
Not 100% sure what you mean by "Markdown raw upload" vs "Markdown file" but files.upload works with a .md
file. You seem to have gotten this to work:
curl -F filetype=post -F content="# [like this](https://someurl)" -F channels=C1.....7L -F token=xoxp-... https://slack.com/api/files.upload
... now swap content="..."
for [email protected]
curl -F filetype=post -F [email protected] -F channels=C1.....7L -F token=xoxp-... https://slack.com/api/files.upload
What this does is convert a standard MD file (e.g. from github) into a Slack Post document. It will try to keep all formatting, like headlines, code, etc.
However, keep in mind that Slack only supports a subset of MD, so e.g. tables will not be displayed correctly.
I'm using the following Python script to translate the most useful subset of Markdown to Slack format. It replaces:
**
with single asterisk *
#
with bold marker asterisks *
The script assumes that lists are indented with two spaces and that single underscores _
are used for italic in Markdown, so it is already compatible with Slack.
import re
import sys
REGEX_REPLACE = (
(re.compile('^- ', flags=re.M), '• '),
(re.compile('^ - ', flags=re.M), ' ◦ '),
(re.compile('^ - ', flags=re.M), ' ⬩ '),
(re.compile('^ - ', flags=re.M), ' ◽ '),
(re.compile('^#+ (.+)$', flags=re.M), r'*\1*'),
(re.compile('\*\*'), '*'),
)
def main(i, o):
s = i.read()
for regex, replacement in REGEX_REPLACE:
s = regex.sub(replacement, s)
o.write(s)
if __name__ == '__main__':
with open(sys.argv[1], encoding='utf-8') as i, \
open(sys.argv[1] + '.slack', 'w', encoding='utf-8') as o:
main(i, o)
The result might be good enough for most purposes.
Run the script with
python markdown-to-slack.py filename.md
Result will be in filename.md.slack
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With