Search code examples
pythonformatslackslack-apimonospace

Python: format string as table in non-monospace font


I'm working on a Slack Webhooks integration with Slack Slash commands. My script flow is as following:

  1. Getting a slash command request from a Slack.
  2. Processing the data (in a form of dict or Pandas DataFrame)
  3. Posting back to the channel using the Slack webhook.

My response is a set of key:value records or two columns dataframe. I want to print them nicely into Slack, however I ran into a formatting problem.

  1. Using Slack back-ticks to make a code block with monospace font would not work, because I might have very large records, and Slack trims the string after 8000 characters.

  2. Printing the lines without monospace, as a plain text, results in an ugly columns with different width in each row.

I tried all sorts of formatting tricks, but could not find a good way to format a response using the Slack API as two very long columns. I guess I am looking for a good library that would format a string for me given the font being used.

Any help would be appreciated.


Solution

  • Slack messages are not really designed to handle large sets of structured data sets. In my personal opinion the best option would be to provide a link to a webpage for showing that data.

    However,if you want to show the data within Slack I would recommend to upload it as plain text file. Those can be much larger than 8.000 chars and they can be displayed with a monospaced font, which would allow you to format them properly.

    Check out files.upload with filetype set to text to upload and share a file in a channel.