0
votes

I work at an MSP and I'm trying to integrate our ticketing system (Autotask) with Slack. So far, I have set up an HTTP POST request to some PHP code when a new ticket is created, which posts a chat message in a certain channel ("New Ticket Created! [Ticket Title] - [Ticket Description]" etc.) using Incoming Webhooks. Below is the important bits of that code:

<?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);

?>

This works and posts a message in the correct channel that looks like this:

enter image description here

The problem I'm having is with responding to those button clicks. I have my Request URL set up, pointing to another PHP script. My understanding is that the button click sends another HTTP POST request to this page with a JSON object, which has a payload item. My hunch is I'm accessing this incorrectly. My code is below:

<?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);

?>

You can see right now I'm just trying a simple response message for testing purposes, but when I click the 'Accept' button I get the following response:

enter image description here

I've scoured the web and cannot find a solid tutorial on Interactive Messages which includes "receiving" button clicks. Any help would be greatly appreciated.

1
I found out that I was being dumb and accessing the array element with the wrong syntax. I switched $responseURL = $data -> response_url; to $responseURL = $data["response_url"]; and made some headway. Now I'm just getting string(11) "{"ok":true}" as a response in Slack, but that's a different issue. - Graham Bewley

1 Answers

0
votes

PHP syntax was wrong for accessing array element.