0

我正在尝试使用机器人更新 Slack 消息的按钮样式和文本,但我找不到有关更新单个块而不是整个数组的信息。我怎样才能只更新 res_but 元素的“文本”和“样式”,同时保留其余的消息内容?

编辑:我忘了提到我正在使用 Python3 和 Bolt 来编程

@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"
                    }
                ]
            }
            ]
        )
4

2 回答 2

0

目前,无法更新块的部分。需要更新完整的视图。
尽管有一种方法可以保留在“输入块”中输入的数据:

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

于 2021-07-08T04:54:21.527 回答
0

我实际上找到了一种更新单个元素的方法。我的问题可能措辞不当,但我更多的是寻找一种速记方式,而不是让大块占用程序空间。

消息中的块被检索并存储在一个名为 new_blocks 的新变量中。然后用新值更新 new_blocks 的元素。然后将 blocks 更新为 new_blocks,实现更改。

    @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)
于 2021-07-08T15:49:10.647 回答