Search code examples
pythonjsonslackslack-apislack-block-kit

Is there a way to update individual block elements in a Slack message?


I am trying to update a Slack message's button style and text using a bot, but I can not find information regarding updating individual blocks, rather than the array as a whole. How can I only update "text" and "style" of the res_but element, while keeping the rest of the message contents?

EDIT: I forgot to mention that I am using Python3 and Bolt to program this

@app.action("res_but")
def resolve_toggle(ack, body, client):
    ack()

    resolvebutton_style = body["actions"][0]["style"]

    if(resolvebutton_style == "danger"):
        client.chat_update(
            channel = body["channel"]["id"],
            ts = body["message"]["ts"],

            blocks=[
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "*Issue:*\n{}\n*Urgency:*\n{}\n*Posted By*:\n{}\n*When:*\n<!date^{}^Posted {{date_num}} {{time_secs}}|Null Date>\n*Last Update:*\n".format(msg, urgency_val, username, posttimest_int)
                }
            },
            {
                "block_id": "issue_buttons",
                "type": "actions",
                "elements": [
                    {
                        "action_id": "res_but",
                        "type": "button",
                        "text": {
                            "type": "plain_text",
                            "emoji": True,
                            "text": "Resolved"  #issue status changed to resolved
                        },
                        "style": "primary",     #color changed to primary
                        "value": "resolve_but"
                    },
                    {
                        "action_id": "ogmes_but",
                        "type": "button",
                        "text": {
                            "type": "plain_text",
                            "emoji": True,
                            "text": "Original Message"
                        },
                        "value": "og_message"
                    }
                ]
            }
            ]
        )

Solution

  • I actually found a way to update individual elements. I may have poorly worded my question, but I was looking more for a shorthand way, rather than having to have large blocks taking up space in the program.

    The blocks from the message were retrieved and stored in a new variable called new_blocks. Elements of new_blocks are then updated with new values. blocks is then updated to be new_blocks, implementing the changes.

        @app.action("res_but")
    def resolve_toggle(ack, body, client, logger):
        ack()
    
        resolvebutton_style = body["actions"][0]["style"]
    
        if(resolvebutton_style == "danger"):
            new_blocks = body["message"]["blocks"] #assign message blocks to new variable
            new_blocks[1]["elements"][0]["style"] = "primary" #change button style
            new_blocks[1]["elements"][0]["text"]["text"] = "Resolved" #change button text
    
    
            client.chat_update(
                channel = body["channel"]["id"],
                ts = body["message"]["ts"],
                
                blocks = new_blocks) #update message with block alterations
    
        else:
            new_blocks = body["message"]["blocks"]
            new_blocks[1]["elements"][0]["style"] = "danger"
            new_blocks[1]["elements"][0]["text"]["text"] = "Unresolved"
    
            client.chat_update(
                channel = body["channel"]["id"],
                ts = body["message"]["ts"],
                
                blocks = new_blocks)
        body["style"] = 80
        logger.info(body)