How to run dialog in slack app with flask? How to run dialog in slack app with flask? flask flask

How to run dialog in slack app with flask?


You had 2 issues in your code:

  • you need to use an access token, not a verification token in the callto dialog.open
  • you need to send the dialog definition as JSON, not as as form array

I made these additional changes- Added code for using a slack token defined as environment variable- Use the get() method to access form parameters in from the request- Added code to show the API response from dialog.open

Here is a corrected version of your code:

import osimport requestsfrom flask import Flask, json, requestapp = Flask(__name__) #create the Flask app@app.route('/helpdesk', methods=['POST'])def helpdesk():    api_url = 'https://slack.com/api/dialog.open'    trigger_id = request.form.get('trigger_id')    dialog = {        "callback_id": "ryde-46e2b0",        "title": "Request a Ride",        "submit_label": "Request",        "notify_on_cancel": True,        "state": "Limo",        "elements": [            {                "type": "text",                "label": "Pickup Location",                "name": "loc_origin"            },            {                "type": "text",                "label": "Dropoff Location",                "name": "loc_destination"            }        ]    }    api_data = {        "token": os.environ['SLACK_TOKEN'],        "trigger_id": trigger_id,        "dialog": json.dumps(dialog)    }    res = requests.post(api_url, data=api_data)    print(res.content)    return make_response()if __name__ == '__main__':    app.run(debug=True, port=8000) #run app in debug mode on port 8000