slackapi / java-slack-sdk

Slack Developer Kit (including Bolt for Java) for any JVM language
https://slack.dev/java-slack-sdk/
MIT License
571 stars 212 forks source link

Updating view with viewsUpdate() in block action handler #1205

Closed karishma21mahajan21 closed 11 months ago

karishma21mahajan21 commented 12 months ago

I am trying to update modal view in Block action handler. I wanted to do fetch the view from request payload, Update this view as it is just a small block change and then use this updated view in viewUpdate().

Below is the block of code which i am using:

        public BlockActionHandler blockSuggestionHandlerTest = (req, ctx) -> {
            View reqView = req.getPayload().getView();
            // update the reqView with all change and set this updated view into a new variable

            View finalNewView = reqView;
            String viewId = req.getPayload().getView().getId();
            ViewsUpdateResponse viewsUpdateResp = null;
            try {

                viewsUpdateResp = ctx.client().viewsUpdate(r -> r
                        .viewId(viewId) // view id of the old view
                        .hash(finalNewView.getHash()) // hash of the old view
                        .view(finalNewView) // updated view
                );
            } catch (IOException | SlackApiException e) {
                throw new RuntimeException(e);
            }
        }

But while updating this updated view, I am getting below error:

ErrorResponseMetadata(messages=[[ERROR] failed to match all allowed schemas [json-pointer:/view], [ERROR] invalid additional property: id [json-pointer:/view], [ERROR] invalid additional property: team_id [json-pointer:/view], [ERROR] invalid additional property: state [json-pointer:/view], [ERROR] invalid additional property: hash [json-pointer:/view], [ERROR] invalid additional property: root_view_id [json-pointer:/view], [ERROR] invalid additional property: app_id [json-pointer:/view], [ERROR] invalid additional property: app_installed_team_id [json-pointer:/view], [ERROR] invalid additional property: bot_id [json-pointer:/view]])

I don't want to create a new view for this update every time a block action handler is called. Since it is an update call so i am assuming an update in the existing view should be fine.

WilliamBergamin commented 12 months ago

Hello @karishma21mahajan21 thanks for writing in 💯

Ideally the current view object found in the request should not be directly modified, the value passed to the .hash() method should be the hash value of the current view. In our template example we explicitly create a new View object and do not modify the view found in the request.

That being said it may be possible to create a copy of the id and hash before you perform the modification on the request object

public BlockActionHandler blockSuggestionHandlerTest = (req, ctx) -> {
    View reqView = req.getPayload().getView();
    var currentId = reqView.getId();
    var currentHash = reqView.getHash();

    View finalNewView = // update the reqView with all change and set this updated view into a new variable;
    ViewsUpdateResponse viewsUpdateResp = null;
    try {

        viewsUpdateResp = ctx.client().viewsUpdate(r -> r
                .viewId(currentId) // view id of the old view
                .hash(currentHash) // hash of the old view
                .view(finalNewView) // updated view
        );
    } catch (IOException | SlackApiException e) {
        throw new RuntimeException(e);
    }
}

Let me know if this resolves your issue

seratch commented 12 months ago

You can find the list of available properties for a view object in the API call request data: https://api.slack.com/reference/surfaces/views. The view data in the payload from Slack can include other properties, such as app_id, which are not allowed to be included in views API calls. For this reason, you cannot reuse the view data in the payload as is.

karishma21mahajan21 commented 12 months ago

Thank you for reverting @WilliamBergamin @seratch But i also wanted to keep the old view state as well. If i will create a new view, how would i ensure that the updated modal will open with the older selected values preserved as it is?

seratch commented 12 months ago

As long as the modal's callback_id and each block's block_id and action_id are the same, selected items can be kept. If it does not work in some cases, you can set initial_option for those input block elements.

karishma21mahajan21 commented 11 months ago

Thank you.It is working.

karishma21mahajan21 commented 11 months ago

Closing it now