Slack API交互式消息,接收按钮单击

时间:2017-06-23 15:03:13

标签: php slack slack-api

我在MSP工作,我试图将我们的票务系统(Autotask)与Slack集成。到目前为止,我在创建新故障单时为某些PHP代码设置了一个HTTP POST请求,该故障单在某个频道中发布了聊天消息(" New Ticket Created![Ticket Title] - [Ticket Description]& #34;等)使用Incoming Webhooks。以下是该代码的重要部分:

<?php 

require_once __DIR__ . '/src/autoload.php';

//Gets ticket number from HTTP POST
$ticketNum = $_POST['subject'];

//Generated at api.slack.com/apps > Incoming webhooks (for the channel I'm posting in)
$responseURL = 'https://hooks.slack.com/services/XXXXXXXXXXXX';

class Response {
    public $response_type = "";
    public $text = "";
    public $attachments = array();
}



//*****
//QUERYING TICKETING SYSTEM API FOR TICKET INFORMATION
//THIS CODE WORKS, OMITTED FOR EASIER READING
//*****




$r = new Response();
$r->response_type = "in_channel";
$r->text = "New Ticket Created: \n*".$ticketName."* (".$ticketNum.") \n".$ticketDescription;
$r->attachments = array(
    0 => array('callback_id' => 'newTicketAction', 'text' => 'Select an action:',
        'color' => '95baa9', 'actions' => array(
        0 => array('name' => 'newTicketAction', 'text' => 'Accept', 'type' => 'button', 
            'value' => 'accept', 'style' => 'primary'),
        1 => array('name' => 'newTicketAction', 'text' => 'View', 'type' => 'button', 
            'value' => 'view')
        )
    )
);


header('Content-Type: application/json');

//Using CURL
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $responseURL,
    CURLOPT_POST => 1,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => json_encode($r)
));

$resp = curl_exec($curl);
var_dump($resp);
curl_close($curl);

?>

这可以在正确的频道中发布消息,如下所示:

enter image description here

我遇到的问题是响应这些按钮点击。我设置了我的请求网址,指向另一个PHP脚本。我的理解是,按钮单击会向该页面发送另一个HTTP POST请求,其中包含一个JSON对象,该对象具有payload项。我的预感是我错误地访问了这个。我的代码如下:

<?php 

require_once __DIR__ . '/src/autoload.php';

//get information from HTTP POST, sent at button click
$payload = $_POST['payload'];
$data = json_decode($payload,true);

$responseURL = $data -> response_url;

class Response {
    public $response_type = "";
    public $text = "";
    public $attachments = array();
}

$r = new Response();
$r->response_type = "in_channel";
$r->text = "Ticket Accepted";

header('Content-Type: application/json');

//Using CURL
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $responseURL,
    CURLOPT_POST => 1,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => json_encode($r)
));

$resp = curl_exec($curl);
var_dump($resp);
curl_close($curl);

?>

您现在可以看到我只是尝试使用简单的响应消息进行测试,但是当我点击“接受”时按钮我收到以下回复:

enter image description here

我已经在网上搜索过,无法在交互式消息上找到一个可靠的教程,其中包括&#34;接收&#34;按钮点击。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

PHP语法访问数组元素是错误的。