Alon Barad
Alon Barad

Reputation: 1991

Is there a way to send rendered table via Slack API?

I'm trying to send SQL query results via Python Slack bot it should look like a three-column table

Things I tried:

None of the above worked, the ASCII one did not align properly and the others did not render at all

I read in the official API docs that there is a way to make a two-column table using section, but I need three or more columns.

Is there any way to send via Python + SlackAPI a nice looking table?

Upvotes: 4

Views: 7847

Answers (2)

Jose
Jose

Reputation: 41

This is the best way I have found, using prettytable

import requests
from prettytable import PrettyTable
  
x = PrettyTable()
  
x.field_names = ["First name", "Last name", "Salary", "City", "DOB"]
x.add_row(["Shubham", "Chauhan", 60000, "Lucknow", "22 Feb 1999"])
x.add_row(["Saksham", "Chauhan", 50000, "Hardoi", "21 Aug 2000"])
x.add_row(["Preeti", "Singh", 40000, "Unnao", "10 Jan 1995"])
x.add_row(["Ayushi", "Chauhan", 65000, "Haridwar", "30 Jan 2002"])
x.add_row(["Abhishek", "Rai", 70000, "Greater Noida", "16 Jan 1999"])
x.add_row(["Dinesh", "Pratap", 80000, "Delhi", "3 Aug 1998"])
x.add_row(["Chandra", "Kant", 85000, "Ghaziabad", "18 Sept 1997"])
table = x.get_string()
pay={
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": f"```{table}```"
            }
        }
    ]
}

url="https://hooks.slack.com/services/ajarl/epeich/agromanauer"

requests.post(url, json=pay)

Upvotes: 2

rmssoares
rmssoares

Reputation: 355

As @OneCricketeer said, Slack does not support tables.

You can find alternative ways to display a table here. If you decide to transform a table into an image, you can start here.

Upvotes: 4

Related Questions