0

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"
                    }
                ]
            }
            ]
        )
IGoodlet
  • 11
  • 4

2 Answers2

1

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)
IGoodlet
  • 11
  • 4
0

Currently, it is not possible to update the parts of the block. The complete view need to be updated.
Although there is a way to preserve data entered in the "Input blocks :

Preserving input entry
Data entered or selected in input blocks can be preserved while updating views. 
The new view object that you use with views.update should contain 
the same input blocks and elements with identical block_id and action_id values.

https://api.slack.com/surfaces/modals/using#updating_views

Suyash Gaur
  • 2,481
  • 2
  • 9
  • 22
  • Great, thank you so much! I wanted to know if that was the case before I continued searching for a method to modify single elements. – IGoodlet Jul 08 '21 at 13:13