Python3 slack_bolt is there easy way to format attachment responses?

155 views Asked by At

Currently my responses to a command given come back to Slack in a very hard format to read. I am hoping there is a way to format the out so it's easier on the eyes.

Console
APP  11:26
[{'imageCL': '1838900-unix64-clang-debug'}, {'imageCL': '1838872-unix64-clang-debug'}, {'imageCL': '1838851-unix64-clang-debug'}, {'imageCL': '1838754-unix64-clang-debug'}, {'imageCL': '1838697-unix64-clang-debug'}, {'imageCL': '1838694-unix64-clang-debug'}, {'imageCL': '1838600-unix64-clang-debug'}, {'imageCL': '1838588-unix64-clang-debug'}, {'imageCL': '1838534-unix64-clang-debug'}, {'imageCL': '1838512-unix64-clang-debug'}, {'imageCL': '1838487-unix64-clang-debug'}, {'imageCL': '1838285-unix64-clang-debug'}, {'imageCL': '1838256-unix64-clang-debug'}, {'configCL': '1838900'}, {'configCL': '1838894'}, {'configCL': '1838893'}, {'configCL': '1838872'}, {'configCL': '1838851'}, {'configCL': '1838849'}, {'configCL': '1838754'}, {'configCL': '1838697'}, {'configCL': '1838694'}, {'configCL': '1838600'}, {'configCL': '1838588'}, {'configCL': '1838534'}, {'configCL': '1838512'}]

I have other responses that come back all bunched up. The above is a single k,v pair but I have others are the more complicated to read.

The code I use is as follows:

from slack_bolt import App

app = App(token=slack_user_token)

def handle_command(ack, event):
    ack()
    ....
    for response in responses:
        if not isinstance(response, list):
            response = [response]
        app.client.chat_postMessage(
            channel = event["channel"],
            attachments=response
            )

if __name__ == "__main__":
    SocketModeHandler(app, slack_app_token).start()

In the above I pass into response a dictionary. My question is, is there a way I can format the output rather than a big clump of text? Even if it's some for loop where I can break apart the whole dictionary output to make it easier to read? I found this documentation https://api.slack.com/methods/chat.postMessage#arg_attachments which only has a super basic one kind of example, but I have also found other documentation for Slack messages that allow a huge range of customizations. Perhaps I need to use something else other that chat_postMessage and attachments?

Update

I am pasting my addendum here as I have made a little progress, and the comments doesn't format:

So I am getting warm, but it's still not working properly:

test_dict2 = [{"color": "#FF0000", "fields": [{"title": "imageCL","value": "1838900-unix64-clang-debug"},{"title": "configCL","value": "1838534"}]}]

test_response2 = {
    #"ok": True,
    #"ts": "1503435956.000247",
    "message": {
        "text": "Here's a message for you",
        "username": "ecto1",
        "attachments": json.dumps(test_dict2),
        "type": "message",
        "subtype": "bot_message",
        "ts": "1503435956.000247"
    }
}

But all I get is:

Console
APP  16:36
Here's a message for you
Aug 22nd, 2017
0

There are 0 answers