【问题标题】:Is there a way to update individual block elements in a Slack message?有没有办法更新 Slack 消息中的单个块元素?
【发布时间】:2021-07-07 21:16:57
【问题描述】:

我正在尝试使用机器人更新 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"
                    }
                ]
            }
            ]
        )

【问题讨论】:

    标签: python json slack slack-api slack-block-kit


    【解决方案1】:

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

    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

    【讨论】:

    • 太好了,非常感谢!在我继续寻找修改单个元素的方法之前,我想知道是否是这种情况。
    【解决方案2】:

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

    消息中的块被检索并存储在一个名为 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-13
      • 2019-02-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 2021-06-13
      相关资源
      最近更新 更多