Slack Bolt(Python)模态框提交报错及视图提交监听问题排查
问题分析与解决建议
出现We had some trouble connecting. Try again?错误,可从以下几个核心点排查解决:
1. 输入元素缺少必填的action_id
Slack要求所有input类型的表单元素必须指定action_id,否则提交时Slack无法正确识别和传递输入数据,直接导致请求失败。你的代码中,第一个plain_text_input元素未设置该字段:
原错误代码片段:
{ "type": "input", "element": { "type": "plain_text_input" }, "label": { "type": "plain_text", "text": "Label", "emoji": True } }
修改后:
{ "type": "input", "element": { "type": "plain_text_input", "action_id": "feedback_label_input" # 新增必填的action_id }, "label": { "type": "plain_text", "text": "Label", "emoji": True } }
2. 确认监听器的注册逻辑
确保@app.view("view123")装饰的监听器是在app初始化完成后注册的,且没有被分支逻辑、条件判断等代码覆盖,保证Bolt框架能正确识别并绑定该监听事件。
3. 检查Slack应用的权限与配置
- 确认应用已添加
commands、views:write、views:read权限,修改权限后需重新安装应用到工作区,确保权限生效; - 验证事件订阅的请求URL是否正确,且能正常接收Slack的POST请求(可通过日志或调试工具确认是否收到
view_submission事件)。
4. 保证ack()调用及时
Bolt框架要求必须在3秒内完成ack()调用,若后续有耗时业务逻辑,需先调用ack()再异步处理,避免超时导致连接失败。
修改后的完整代码示例
@app.command("/echo") def handle_command(body, ack, client, logger): logger.info(body) ack() res = client.views_open( trigger_id=body["trigger_id"], view={ "title": { "type": "plain_text", "text": "Add info to feedback", "emoji": True }, "submit": { "type": "plain_text", "text": "Save", "emoji": True }, "type": "modal", "callback_id": "view123", "blocks": [ { "type": "input", "element": { "type": "plain_text_input", "action_id": "feedback_label_input" }, "label": { "type": "plain_text", "text": "Label", "emoji": True } }, { "type": "actions", "elements": [ { "type": "datepicker", "initial_date": "1990-04-28", "placeholder": { "type": "plain_text", "text": "Select a date", "emoji": True }, "action_id": "actionId-0" }, { "type": "datepicker", "initial_date": "1990-04-28", "placeholder": { "type": "plain_text", "text": "Select a date", "emoji": True }, "action_id": "actionId-1" } ] } ] }, ) logger.info(res) @app.view("view123") def handle_submission(ack, body, client, view, logger): # 可选:获取并处理表单提交的数据 label_value = view["state"]["values"]["feedback_label_input"]["plain_text_input"]["value"] date1 = view["state"]["values"]["actionId-0"]["datepicker"]["selected_date"] date2 = view["state"]["values"]["actionId-1"]["datepicker"]["selected_date"] logger.info(f"Received label: {label_value}, dates: {date1}, {date2}") ack()
内容的提问来源于stack exchange,提问作者ZigaK
相关产品推荐
相关产品推荐

