API endpoit of opening dialog vs submitting dialog

Hello Mattermost community,
i’m new to mattermost, so i’m struggling with the documentation of Interactive dialogs.
i’m using django, and i’m trying to open a dialog in a mattermost channel using slash command, i succeded in that, but the problem is when the user submit the fields of the dialog, how can it be done? knowing that opening dialog and submitting it, have differents endpoint!
submit uses: http://your-mattermost-url.com/api/v4/actions/dialogs/submit
opening uses: http://your-mattermost-url.com/api/v4/actions/dialogs/open

what 's the best way so that when i click on submit of the dialog, the values of the field got saved for example or displaying those informations as message in that channel?

Hello eliot_jo,

Welcome to the Mattermost community! Great to have you here.

Your question is a common one, so don’t worry. To handle form submissions from an interactive dialog, you generally want to set up a separate endpoint in your Django application to process the POST request that’s sent when the dialog is submitted.

The submit_dialog request, upon interactions like a user submitting the dialog or dropdown select, sends a POST request to the callback_url that you specified when opening the dialog.

Here’s a short example in pseudo code -

When you open the dialog, you define a dialog object like so:

Python

1 2 3 4 5 6 7 8 9 10 11 12 13 14

dialog = { "callback_id": "somecallbackid", "url": "https://your-django-app.com/process-dialog", "title": "Dialog Title", "elements": [ { "type": "text", "name": "input_field_1", "label": "Input Field 1", }, ], "submit_label": "Submit", "notify_on_cancel": False, }

Notice the url field in dialog, that’s where you’d put the endpoint in your Django application that you want Mattermost to send the POST request to when the dialog is submitted.

Then in your Django app, handle this at the endpoint process-dialog:

Python

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

`@require_POST
def process_dialog(request):
payload = json.loads(request.body)
submitted_data = payload[‘submission’]

# Now you have access to the submitted data. 
# i.e - If your dialog had a field with the name "input_field_1", 
# you can access its submitted value by - field_1_value = submitted_data['input_field_1']

# You can then do any processing you need with the submitted data.

# To send a message back to the channel you can use the 'response_url' provided in the payload.
# This will execute a POST request to the response_url and send the specified message to the channel.

response_message = {
    "response_type": "in_channel",
    "text": f"You submitted: {field_1_value}"
}

headers = {'Content-Type': 'application/json'}
requests.post(payload['response_url'], headers=headers, data=json.dumps(response_message))

return HttpResponse(status=200) # Respond with a 200 OK.`

That’s it! Before integrating it, please replace the placeholder URL with your actual application’s endpoint. I hope the above example helps you to move forward.

Just remember, the values you’re capturing in the dialog can be accessed in the payload of the POST request sent to the callback URL that you’ve defined.

If you have more questions or run into any issues, feel free to ask. We’re happy to help!

Best regards,
~Mattermost AI Assistant :robot:

[Disclaimer: This was an AI assisted response powered by Mattermost AI. Were any of the above suggestions inaccurate? Let us know by replying to this comment!]