phpslack-apislack-commandsslack-dialog

Slack sends invalid, mal-formed JSON data after Dialog interaction


I am working on a slash command that'll invoke a dialog.

 $dialog = [
        'callback_id' => 'ryde-46e2b0',
        'title' => 'Request a Ride',
        'submit_label' => 'Request',
        'elements' => [
            [
                'type' => 'text',
                'label' => 'Pickup Location',
                'name' => 'loc_origin'
            ],
            [
                'type' => 'text',
                'label' => 'Dropoff Location',
                'name' => 'loc_destination'
            ]
        ]
    ];

    // get trigger ID from incoming slash request
    $trigger = filter_input(INPUT_POST, "trigger_id");

    // define POST query parameters
    $query = [
        'token' => 'XXXXXXXXX MY TOKEN XXXXXXXXX',
        'dialog' => json_encode($dialog),
        'trigger_id' => $trigger
    ];

    // define the curl request
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/dialog.open');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/x-www-form-urlencoded'
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    // set the POST query parameters
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));

    // execute curl request
    $response = curl_exec($ch);

    // close
    curl_close($ch);

    var_export($response);

When I issue the slash command, my test dialog opens successfully

enter image description here

I then fill two test values test1 and test2 in the fields and submit the request. My endpoint is being hit with the dialog data payload correctly, but the data sent is not valid JSON:

The value of $_POST is: (I've masked all identifying tokens/IDs with xxx)

{"payload":"{\\\"type\\\":\\\"dialog_submission\\\",\\\"token\\\":\\\"XXX\\\",\\\"action_ts\\\":\\\"1536603864.688426\\\",\\\"team\\\":{\\\"id\\\":\\\"xxx\\\",\\\"domain\\\":\\\"ourdomain\\\"},\\\"user\\\":{\\\"id\\\":\\\"xxx\\\",\\\"name\\\":\\\"my_name\\\"},\\\"channel\\\":{\\\"id\\\":\\\"xxx\\\",\\\"name\\\":\\\"directmessage\\\"},\\\"submission\\\":{\\\"loc_origin\\\":\\\"test1\\\",\\\"loc_destination\\\":\\\"test2\\\"},\\\"callback_id\\\":\\\"ryde-46e2b0\\\",\\\"response_url\\\":\\\"https:\\\\/\\\\/hooks.slack.com\\\\/app\\\\/XXX\\\\/XXX\\\\/XXX\\\",\\\"state\\\":\\\"\\\"}"}

This is an invalid JSON, even when the "\\" instances are removed. Why is this happening?

Here is the code that handles the POST from Slack:

error_log(" -- dialog response: " . json_encode($_POST) . "\n", 3, './runtime.log');

Which results in the output above.


Solution

  • I'm not sure why you are calling json_encode($_POST). The documentation is very clear on the format that will be sent:

    $payload = filter_input(INPUT_POST, 'payload');
    $decoded = json_decode($payload);
    
    var_dump($decoded);