node.jsslack-apibotkit

Use "response_action" to respond to view_submission when using botkit


I have a simple modal with view pushed using:

           const result = await bot.api.views.open({
                trigger_id: message.trigger_id,
                view: {
                    "type": "modal",
                    "submit": {
                        "type": "plain_text",
                        "text": "Submit",
                        "emoji": true
                    },
                    "title": {
                        "type": "plain_text",
                        "text": "Request",
                        "emoji": true
                    },
                    "blocks": [
                        {
                            "type": "input",
                            "block_id" : "accblock",
                            "element": {
                                "type": "plain_text_input",
                                "action_id": "account_name",
                                "multiline": false
                            },
                            "label": {
                                "type": "plain_text",
                                "text": "Account Name",
                                "emoji": true
                            }
                        }
                    ]
                }
            });

I need to add error to the block if certain value is entered on view_submission. I understand I should send response with the following JSON:

       {
          response_action: 'errors',
          errors: {
            "ticket-desc": 'I will never accept a value, you are doomed!'
          }
        }

But, how do I send it? I've tried using bot.httpBody(). Does this JSON need to be included as the view object? Any help is appreciated.


Solution

  • bot.httpBody() is the method you need.

    Make sure that the key to the errors dictionary matches the block_id of the element you are providing an error message for.

    In your case this will work:

    bot.httpBody({
      response_action: 'errors',
      errors: {
        accblock: 'Your account name is invalid or in use.'
      }
    })
    

    You do not need to JSON.stringify the response message and indeed the process fails if you do.