You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Slack Bolt Python开发:如何将modal中的输入字段设置为可选?

解决方案

该需求完全可以实现,Slack Modal 的input类型块默认提供了是否必填的配置项。
默认情况下input块的optional参数值为False,即必须填写内容才能提交,你只需要在对应的输入块配置中添加"optional": True即可将输入框设为可选,用户留空也能正常提交Modal。

修改后的代码示例

view = {
    "type": "modal",
    "title": {
        "type": "plain_text",
        "text": "My App",
        "emoji": True
    },
    "submit": {
        "type": "plain_text",
        "text": "Submit",
        "emoji": True
    },
    "close": {
        "type": "plain_text",
        "text": "Cancel",
        "emoji": True
    },
    "blocks": [
        {
            "type": "input",
            "element": {
                "type": "plain_text_input",
                "action_id": "plain_text_input-action"
            },
            "label": {
                "type": "plain_text",
                "text": "Please leave feedback here",
                "emoji": True
            },
            "optional": True # 新增该配置项即可
        }
    ]
}
slack_client.views_open(trigger_id=body['trigger_id'],view=view)

效果示意图

输入框示意图

内容的提问来源于stack exchange,提问作者Rachel Dong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 11:36:03