将附加数据与 Slack Bolt python 中的 block_actions 有效负载一起传递给动作侦听器

Passing additional data to the action listeners along with the block_actions payload in Slack Bolt python

我是 Slack APP 开发的新手,我正在使用 Slack bolt python 开发应用程序。在我的应用程序中,我使用按钮单击配置了松弛交互,单击按钮时我调用了相应的侦听器(由按钮中提到的 action_id 标识)。在侦听器中,我得到 block_actions 有效载荷,其中包含事件的所有状态值,但除此之外,我还想为侦听器函数提供一些参数。我们是否可以通过任何方式向听众发送额外的参数

@app.action("change_time_period")
def handle_change_time_period(ack, context, body, client, logger, message, say):
   # want the arguments here such as passing email_id from post_data

def post_data(user_id, email_id):
    client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
    result = client.chat_postMessage(
        channel=user_id,
        blocks=[

            {
                "type": "actions",
                "elements": [
                    {
                        "type": "button",
                        "text": {
                            "type": "plain_text",
                            "emoji": True,
                            "text": "Change time period"
                        },
                        "style": "primary",
                        "value": "time_period",
                        "action_id": "change_time_period"
                    }
                ]
            }
        ]

    )


在上面的代码中,我将调用 post_data,它将 post 包含块工具包的消息,一旦用户单击按钮,将调用 handle_change_time_period

Slack 块工具包按钮没有我们可以将元数据传递给交互处理程序的字段。实现此目的的一种解决方法是使用按钮元素字典的 value 字段来转储具有所需数据的 JSON。 From the Slack API doc, 值字段最多可容纳 2000 个字符。您的值字段可以类似于

"value": json.dumps({"actual_value" : "time_period", "email" : "johndoe@abc.com"})

这可以通过减少键中的字符来进一步优化。如果您需要传递的额外数据超出 char 限制,您可能必须考虑在服务器上缓存数据并在块中传递缓存键,然后可以在处理程序中使用该键从缓存中检索实际数据。