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

时间:2018-09-10 19:19:39

标签: php slack-api slack-commands slack-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);

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

enter image description here

然后,我在字段中填写两个测试值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');

这将导致上面的输出。

1 个答案:

答案 0 :(得分:1)

我不确定您为什么打mutateThe documentation在要发送的格式上非常清楚:

new.df %>%
    group_by(sensor) %>%
    mutate(hour_from_max_hour = hour_day - hour_day[which(value == max(value))[1]])
## A tibble: 10 x 4
## Groups:   sensor [2]
#   sensor hour_day value hour_from_max_hour
#   <fct>     <int> <dbl>              <int>
# 1 A             0    1.                 -2
# 2 A             1    1.                 -1
# 3 A             2    3.                  0
# 4 A             3    1.                  1
# 5 A             4    2.                  2
# 6 B             0    1.                 -3
# 7 B             1    3.                 -2
# 8 B             2    4.                 -1
# 9 B             3    5.                  0
#10 B             4    2.                  1