Slack 消息对话框的 Curl 调用

Slack Message Dialog's Curl Call

任何人都可以给我一个 PHP 中的 slack 消息对话框的例子吗?如何对 dialog.open 方法进行 curl 调用?请提出建议。

这里是 PHP 中的一个简单示例,说明如何基于斜杠命令创建对话框。

请注意,您需要创建并安装一个 Slack App first. You also need to add a slash command 到您的 Slack 应用程序,它需要调用此脚本。最后,您需要手动将 OAuth 访问令牌 从您的 Slack 应用程序添加到此脚本(替换 YOUR_TOKEN)。

该脚本将在 Slack 通道中打印 API 响应,因此您可以查看响应以及是否有任何错误。

对运行对话框执行斜杠命令。

// define the dialog for the user (from Slack documentation example)
$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' => 'YOUR_TOKEN',
    '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);