Slack Blocks中email_text_input回车不触发dispatch action问题求助
问题
我想用Slack Blocks实现一个邮件输入框,要求用户输入邮箱并按下回车键时触发对应的action。目前组件能正常渲染,但action无法触发,而且不像plain_text_input那样显示“press 'enter' to submit”提示;把element.type改成plain_text_input时功能一切正常。请问怎么实现按下回车就能触发action的Slack邮件输入框?
对应的Blocks代码如下:
{ "blocks": [ { "type": "divider" }, { "type": "input", "label": { "type": "plain_text", "text": "Enter email", "emoji": false }, "element": { "type": "plain_text_input", "action_id": "confirm-block", "placeholder": { "type": "plain_text", "text": "example@example.com", "emoji": false }, "focus_on_load": true }, "dispatch_action": true, "optional": false }, { "type": "input", "label": { "type": "plain_text", "text": "Enter email", "emoji": false }, "element": { "type": "email_text_input", "action_id": "confirm-block", "placeholder": { "type": "plain_text", "text": "example@example.com", "emoji": false }, "focus_on_load": false }, "dispatch_action": true, "optional": false } ] }
该代码在Block Kit Builder中的渲染效果:
解决方案
- 添加
dispatch_action_config配置:email_text_input不会默认响应回车事件,需要明确指定触发action的事件类型。在email_text_input元素中添加dispatch_action_config,设置触发事件为on_enter_pressed。 - 设置唯一
action_id:你的代码中两个输入框使用了相同的action_id,会导致Slack无法区分触发来源,建议给每个输入框分配唯一的action_id。
修改后的代码示例:
{ "blocks": [ { "type": "divider" }, { "type": "input", "label": { "type": "plain_text", "text": "Enter email", "emoji": false }, "element": { "type": "plain_text_input", "action_id": "confirm-block-plain", "placeholder": { "type": "plain_text", "text": "example@example.com", "emoji": false }, "focus_on_load": true }, "dispatch_action": true, "optional": false }, { "type": "input", "label": { "type": "plain_text", "text": "Enter email", "emoji": false }, "element": { "type": "email_text_input", "action_id": "confirm-block-email", "placeholder": { "type": "plain_text", "text": "example@example.com", "emoji": false }, "focus_on_load": false, "dispatch_action_config": { "trigger_actions_on": ["on_enter_pressed"] } }, "dispatch_action": true, "optional": false } ] }
关键说明
dispatch_action_config.trigger_actions_on指定了触发action的事件,on_enter_pressed表示按下回车键时触发,添加后输入框会自动显示“press 'enter' to submit”提示。- 唯一的
action_id能让后端准确识别触发事件的输入框,避免逻辑混乱。
内容的提问来源于stack exchange,提问作者mikeb
相关产品推荐
相关产品推荐

