0

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

 $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 掩盖了所有识别令牌/ID)

{"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');

这导致了上面的输出。

4

1 回答 1

1

我不知道你为什么打电话json_encode($_POST)文档非常清楚将要发送的格式:

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

var_dump($decoded);
于 2018-09-10T20:29:51.420 回答