【问题标题】:Responding to slack view_submission event with error object使用错误对象响应 slack view_submission 事件
【发布时间】:2020-04-17 11:47:35
【问题描述】:

我使用 ASP.NET Core 2.1 构建与 Slack 的集成 这是我的 json 格式的模态。

{
  "type": "modal",
  "callback_id": "send_sms",
  "title": {
    "type": "plain_text",
    "text": "Send SMS",
    "emoji": false
  },
  "submit": {
    "type": "plain_text",
    "text": "Submit"
  },
  "close": {
    "type": "plain_text",
    "text": "Cancel"
  },
  "blocks": [
    {
      "type": "divider"
    },
    {
      "type": "input",
      "block_id": "phone_number",
      "label": {
        "type": "plain_text",
        "text": "Enter phone number",
        "emoji": false
      },
      "element": {
        "type": "plain_text_input",
        "placeholder": {
          "type": "plain_text",
          "text": "Phone number",
          "emoji": false
        },
        "action_id": "action_phone_number"
      }
    },
    {
      "type": "input",
      "block_id": "message",
      "label": {
        "type": "plain_text",
        "text": "Enter message",
        "emoji": false
      },
      "element": {
        "placeholder": {
          "type": "plain_text",
          "text": "Message",
          "emoji": false
        },
        "action_id": "message",
        "type": "plain_text_input",
        "multiline": true
      }
    }
  ]
}

因此,当用户提交表单时,我的 .net 核心应用程序会收到一个 view_submission 事件,如果电话号码格式无效,我想以错误响应此事件。 Slack 文档说我应该用这样的 json 对象来响应:

`{
  "response_action": "errors",
  "errors": {
    "phone_number": "Invalid phone number format"
  }
}`

在调试过程中,我发现我的应用程序实际上确实从文件中加载了 json,并使用包含此 json 的字符串进行响应。但是我的模态仍然会出现这个错误 enter image description here

controller 方法返回 Task> 对象,但我不确定它是否正确 所以我的问题是: 有人知道我应该如何使用 .net 核心来响应这个松弛事件吗? 据我了解,即使我的 .net 核心应用程序中有验证错误并且我想将错误对象返回给 slack,我也应该以状态 200 OK 进行响应

【问题讨论】:

    标签: .net-core asp.net-core-webapi slack slack-dialog


    【解决方案1】:

    最后这个问题通过返回解决了

    Content('json_string', "application/json");
    

    【讨论】:

    • 虽然问题是针对 ASP.NET 但如果您使用的是 Django 并来到这里寻求答案,那么您可以使用:return JsonResponse({ "response_action": "errors", "errors": { "phone_number": "Invalid phone number format" } }) 不要忘记导入:from django.http import JsonResponse
    猜你喜欢
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2022-12-05
    相关资源
    最近更新 更多