Created
June 8, 2023 18:58
-
-
Save tubaman/d94d608c5e34de8f746e6050c6e6ef40 to your computer and use it in GitHub Desktop.
Automatically post markdown to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import argparse | |
import json | |
from urllib.parse import quote | |
def main(argv=None): | |
if argv is None: | |
argv = sys.argv | |
parser = argparse.ArgumentParser(description="Encode Slack block kit payloads as block kit builder URLs") | |
parser.add_argument('blockkitpayloadfile', type=argparse.FileType('r'), help="file containing Block Kit Payloads. use '-' to read from stdin") | |
args = parser.parse_args(argv[1:]) | |
blockkitpayloads = json.load(args.blockkitpayloadfile) | |
for blockkitpayload in blockkitpayloads: | |
blockkitpayload_txt = json.dumps(blockkitpayload) | |
url = "https://app.slack.com/block-kit-builder/T02V1D15D#" + quote(blockkitpayload_txt) | |
print(url) | |
if __name__ == '__main__': | |
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import argparse | |
import json | |
def split_along_newline_by_size(file, max_length=3000): | |
"""Split because block kit section text fields have a max length of 3000 | |
https://api.slack.com/reference/block-kit/blocks#section_fields | |
""" | |
lines = list() | |
lines_len = 0 | |
for line in file: | |
if len(line) + lines_len > max_length: | |
if lines: | |
yield ''.join(lines) | |
lines = list() | |
lines_len = 0 | |
else: | |
raise ValueError("line exceeds max length(%d): %s" % (max_length, line)) | |
lines.append(line) | |
lines_len += len(line) | |
if lines: | |
yield ''.join(lines) | |
def main(argv=None): | |
if argv is None: | |
argv = sys.argv | |
parser = argparse.ArgumentParser(description="Splits mrkdwn by maxsize and encode as block kit payloads") | |
parser.add_argument('mrkdwnfile', type=argparse.FileType('r'), help="file containing mrkdwn formatting text. Use '-' to read from stdin") | |
parser.add_argument('-i', '--indent', type=int, default=4, help="how many spaces to indent(default: 4)") | |
parser.add_argument('-m', '--maxsize', type=int, default=3000, help="maxsize of block kit payload text(default: 3000)") | |
args = parser.parse_args(argv[1:]) | |
block_kit_payloads = list() | |
for text in split_along_newline_by_size(args.mrkdwnfile, max_length=args.maxsize): | |
block_kit_payload = { | |
'blocks': [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": text, | |
}, | |
}, | |
], | |
} | |
block_kit_payloads.append(block_kit_payload) | |
print(json.dumps(block_kit_payloads, indent=args.indent)) | |
if __name__ == '__main__': | |
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MARKDOWNFILE="$1" | |
wget https://raw.githubusercontent.com/tubaman/pandoc-md-to-slack/tubaman/slack.lua | |
pandoc -t slack.lua -f markdown -i "$MARKDOWNFILE" | ./asblockkitpayload - | ./asblockkitbuilderurl - | xargs open |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment