如何使用Python在slack bot中添加条件为用户添加强制输入字段?

问题描述 投票:0回答:1

我正在使用 slack Bolt 在 python 中创建一个 slack 机器人。我已经为用户创建了一个类似表单的结构来提供输入值,但我需要指定一些输入为强制输入。我尝试使用“可选”:true字段但没有得到任何结果 我需要将日期选择器输入和多选输入作为需要输入的用户的必填字段。 下面是我创建的块:

blocks = [
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Title"
        }
    },
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Date Duration"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "_Required Field_"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "Start Date"
        }
    },
    {
        "type": "input",
        "element": {
            "type": "datepicker",
            "placeholder": {
                "type": "plain_text",
                "text": "Select Start date"
            },
            "action_id": "start_date_action"
        },
        "label": {
            "type": "plain_text",
            "text": " "
        },
        "hint": {
            "type": "plain_text",
            "text": "Please ensure start date should be greater than current date "
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "End Date"
        }
    },
    {
        "type": "input",
        "element": {
            "type": "datepicker",
            "placeholder": {
                "type": "plain_text",
                "text": "Select End date"
            },
            "action_id": "end_date_action"
        },
        "label": {
            "type": "plain_text",
            "text": " "
        },
        "hint": {
            "type": "plain_text",
            "text": "Please ensure end date should be greater than start date "
        }
    },
    {
        "type": "divider"
    },
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Field1"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "_Required Field_"
        }
    },
    {
        "type": "input",
        "hint": {
            "type": "plain_text",
            "text": "Multiple inputs are accepted"
        },
        "element": {
            "type": "multi_static_select",
            "placeholder": {
                "type": "plain_text",
                "text": "Select field"
            },
            "options": [
                {
                    "text": {
                        "type": "plain_text",
                        "text": "11111"
                    },
                    "value": "value-0"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "22222"
                    },
                    "value": "value-1"
                }
            ],
            "action_id": "1_multi_select-action"
        },
        "label": {
            "type": "plain_text",
            "text": " "
        }
    },
    {
        "type": "divider"
    },
    {
        "type": "divider"
    },
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Field3"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "_Optional Field_"
        }
    },
    {
        "type": "input",
        "hint": {
            "type": "plain_text",
            "text": "Multiple inputs are accepted"
        },
        "element": {
            "type": "multi_static_select",
            "placeholder": {
                "type": "plain_text",
                "text": "Select Categories"
            },
            "options": [
                {
                    "text": {
                        "type": "plain_text",
                        "text": "adventure"
                    },
                    "value": "value-0"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "biking"
                    },
                    "value": "value-2"
                }
            ],
            "action_id": "categories_multi_select-action"
        },
        "label": {
            "type": "plain_text",
            "text": " "
        }
    },
    {
        "type": "divider"
    },
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": "Number"
        }
    },
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "_Optional Field_"
        }
    },
    {
        "type": "input",
        "hint": {
            "type": "plain_text",
            "text": "Numeric values only"
        },
        "element": {
            "type": "plain_text_input",
            "action_id": "plain_text_input-action",
            "placeholder": {
                "type": "plain_text",
                "text": "Enter Numeric Value"
            }
        },
        "label": {
            "type": "plain_text",
            "text": " "
        }
    },
    {
        "type": "divider"
    },
    {
        "type": "actions",
        "elements": [
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Submit"
                },
                "value": "submit_form",
                "action_id": "submit_action"
            }
        ]
    }
]

并通过黑人发布这样的消息

app.client.chat_postEphemeral(
    channel=body["channel"]["id"],
    user=body['user']['id'],
    blocks=param_block,
    text="text_msg",
    )

有没有办法考虑仅包含数值的文本输入字段? 我还附上了该块的屏幕截图

python slack slack-api slack-block-kit
1个回答
0
投票

这已经很老了,所以我希望这仍然对你有帮助/可能对其他人有帮助。

您想编写自己的验证来验证字符串输入是否可以转换为整数,然后在使用 if 语句更新模式时使用

ack(response_action=errors, errors="<your error message here>")
进行验证,而不是提交模式并为模式创建临时消息用户 - 谁想要提交表单只是为了发现它是无效的?

© www.soinside.com 2019 - 2024. All rights reserved.