Slack 在对话交互后发送无效的、格式错误的 JSON 数据

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

我正在研究将调用对话框的斜线命令。

 $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);

当我发出斜杠命令时,我的测试对话框成功打开

然后我在字段中填写两个测试值test1test2并提交请求。我的端点被对话数据负载正确命中,但发送的数据无效 JSON:

$_POST 的值是:(我用 xxx 屏蔽了所有识别 tokens/IDs 的)

{"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\\":\\"\\"}"}

这是无效的 JSON,即使删除了“\\”实例也是如此。为什么会这样?

这是处理来自 Slack 的 POST 的代码:

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

这导致上面的输出。

我不确定你为什么打电话给 json_encode($_POST)The documentation发送的格式很清楚:

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

var_dump($decoded);