question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Submitting modal from views_open() always fails

See original GitHub issue

Description

Describe your issue here.

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I’ve read and understood the Contributing guidelines and have done my best effort to follow them.
  • I’ve read and agree to the Code of Conduct.
  • I’ve searched for any related issues and avoided creating a duplicate issue.

Bug Report

Hey- I’m using the python-slackclient v2.7.2 in my Flask app to open a modal and get user input. I followed this guide https://python-slackclient.readthedocs.io/en/latest/basic_usage.html under the section ‘Opening a modal’. I followed the exact steps there, but always get the error ‘We had some trouble connecting. Try again?’ when I try to submit the modal. I’ve been searching for a solution, but the example seems right- it IS returning a 200 response with an empty body on success. I’ve tried a bunch of other examples as well, but the form won’t submit even though the proper response is being sent in less than 3 seconds

Reproducible in:

slackclient version: 2.7.2

python version: 3.8.2

OS version(s): OSX catalina 10.15.4

Steps to reproduce:

  1. Add the following to an app.py file:
from flask import Flask, request, make_response, Response
from slack import WebClient
from slack.errors import SlackApiError
from slack.signature import SignatureVerifier

import os
import json


# Your app's Slack bot user token
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
SLACK_VERIFICATION_TOKEN = os.environ["SLACK_VERIFICATION_TOKEN"]

# Slack client for Web API requests
client = WebClient(SLACK_BOT_TOKEN)

# Flask web server for incoming traffic from Slack
app = Flask(__name__)
signature_verifier = SignatureVerifier(os.environ["SLACK_SIGNING_SECRET"])

@app.route("/", methods=["POST"])
def slack_app():
  if not signature_verifier.is_valid_request(request.get_data(), request.headers):
    return make_response("invalid request", 403)

  if "command" in request.form \
    and request.form["command"] == "/open-modal":
    trigger_id = request.form["trigger_id"]
    try:
      response = client.views_open(
        trigger_id=trigger_id,
        view={
          "type": "modal",
          "callback_id": "modal-id",
          "title": {
            "type": "plain_text",
            "text": "Awesome Modal"
          },
          "submit": {
            "type": "plain_text",
            "text": "Submit"
          },
          "close": {
            "type": "plain_text",
            "text": "Cancel"
          },
          "blocks": [
            {
              "type": "input",
              "block_id": "b-id",
              "label": {
                "type": "plain_text",
                "text": "Input label",
              },
              "element": {
                "action_id": "a-id",
                "type": "plain_text_input",
              }
            }
          ]
        }
      )
      return make_response("", 200)
    except SlackApiError as e:
      code = e.response["error"]
      return make_response(f"Failed to open a modal due to {code}", 200)

  elif "payload" in request.form:
    payload = json.loads(request.form["payload"])
    if payload["type"] == "view_submission" \
      and payload["view"]["callback_id"] == "modal-id":
      submitted_data = payload["view"]["state"]["values"]
      print(submitted_data)  # {'b-id': {'a-id': {'type': 'plain_text_input', 'value': 'your input'}}}
      return make_response("", 200)

  return make_response("", 404)

if __name__ == "__main__":
  # export SLACK_SIGNING_SECRET=***
  # export SLACK_API_TOKEN=xoxb-***
  # export FLASK_ENV=development
  app.run("localhost", 5000)


if __name__ == "__main__":
    app.run()
  1. Set your environment variables.
  2. Run the app with python3 -m flask run
  3. Start ngrok with ngrok http 5000
  4. Create a slash command in your slack app called /open-modal and set the endpoint to the ngrok https endpoint from above.
  5. Invoke /open-modal in slack, enter input, and click Submit
  6. See error
Screen Shot 2020-07-05 at 2 54 04 PM

Expected result:

The form submits successfully and POST’s data back to the endpoint…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
arevaloleocommented, Dec 2, 2020

@seratch I have the right interactivity endpoint setup and the endpoint can receive the modal id. I am using fastapi not flask btw.

image

image

@app.post("/interactive")
async def interactive(request: Request):
    form = await request.form()
    body = jsonable_encoder(form['payload'])
    dict_payload = json.loads(body)
    print("received callback_id"+dict_payload.get('view').get('callback_id'))
    return ""

I guess the issue seem to be slackclient won’t close the modal after the submission somehow?

could you solve it?

1reaction
seratchcommented, Jul 5, 2020

Hi, thanks for sharing this!

I’m afraid that you haven’t yet set up the Request URL in the Interactivity & Shortcuts section. To receive view submission requests, telling your endpoint URL to the Slack API server beforehand is required. Refer to https://api.slack.com/interactivity/handling#setup for more details.

Also, I will improve this SDK’s document to be more friendly for developers. Thanks a lot 🙇

Read more comments on GitHub >

github_iconTop Results From Across the Web

Submitting modal from views_open() always fails #741 - GitHub
Bug Report. Hey- I'm using the python-slackclient v2.7.2 in my Flask app to open a modal and get user input. I followed ...
Read more >
views.open method - Slack API
Open a modal with a user by exchanging a trigger_id received from another interaction. See the modals documentation to learn how to obtain...
Read more >
Slack app opening new modal after previous modal submit is ...
When I deploy the app and disable socket mode, the form submission never triggers the second modal to be displayed. I am able...
Read more >
Modals | Slack SDK for Java
As always, your app has to respond to the request within 3 seconds by ack() ... When a modal view is submitted, you'll...
Read more >
How to keep modal window open on form submit? - CSS-Tricks
The problem is that the modal window closes on submit. This prevents visitors from seeing success or error messages .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found