Search code examples
pythonslack

Slack modal view immediately closes when using views.push


I'm creating a Slack app using Python and Flask. The app uses a Slack modal with two views: The first is opened when a slash command is given and the second should be opened when the user presses the submit button on the first view. When I run this, the first view opens correctly. However, when I try to push the second view it opens and then closes immediately, reverting back to the first view without any interaction from the user.

I have tried using the notify_on_close flag to see if the second view is being closed for some reason, however I am not getting any 'form closed' messages after the second view closes.

Here is an example of where this problem occurs:

@app.route('/slash_command', methods=['POST'])
def open_modal():
    trigger_id = request.form['trigger_id']

    sc.views_open(trigger_id=trigger_id, view=views.first_view)
    return '', 200


@app.route('/actions', methods=['POST'])
def action_endpoint():
    payload = json.loads(request.form['payload'])
    callback_id = payload['view']['callback_id']
    trigger_id = payload['trigger_id']

    # Push the second view if the first view is submitted
    if callback_id == 'first_view':
        sc.views_push(trigger_id=trigger_id, view=views.second_view)
    return '', 200

My views are very simple:

first_view = {
    "type": "modal",
    'callback_id': 'first_view',

    "title": {
        "type": "plain_text",
        "text": "First View"
    },
    "submit": {
        "type": "plain_text",
        "text": "Submit"
    },
    "close": {
        "type": "plain_text",
        "text": "Cancel"
    },
    'blocks': []
}

second_view = {
    'type': 'modal',
    'callback_id': 'second_view',

    'title': {
        'type': 'plain_text',
        'text': 'Second View',
    },

    'submit': {
        'type': 'plain_text',
        'text': 'Submit'
    },

    'close': {
        'type': 'plain_text',
        'text': 'Cancel'
    },
    'blocks': []
}

Solution

  • Slack support resolved this by explaining that the above code pushes a new view and then returns a 200 response, which is interpreted by Slack as a request to close the current view. I was able to make the code work correctly by updating it as follows:

    # Push the second view if the first view is submitted
    if callback_id == 'first_view':
        return {
            'response_action': 'push',
            'view': views.second_view
        }
    
    return '', 200