代码之家  ›  专栏  ›  技术社区  ›  Pooja Nikam

Slack Message对话框的Curl调用

  •  0
  • Pooja Nikam  · 技术社区  · 7 年前

    有谁能给我一个PHP中slack消息对话框的例子吗?如何对对话框进行curl调用。打开方法?请提出建议。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Erik Kalkoken    7 年前

    下面是一个PHP中的简单示例,介绍如何基于斜杠命令创建对话框。

    请注意,您需要创建并安装 Slack App 第一您还需要添加 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);