Search code examples
phpslackslack-api

get value from approve slack to php


Here, I tried to make attaching interactive message buttons from web app to slack, I was already post message to slack with JSON. Now how I get value of button approve when approve in slack to php/web app?

Function

function slacktest($message, $room = "channel-name")
{
    $room = ($room) ? $room : "channel-name";
    $data = "payload=" . json_encode(array(
            "channel"       =>  "#{$room}",
            "text"          =>  $message,
            "username"      =>  "Beny",
            "attachments"   =>  array([
                                'fallback' => 'Hey! See this message',
                                'pretext'  => 'New Approval Request ! ',
                                'color'    => '#ff6600',
                                'actions'  => array
                      (
                      [
                      'name'     => "one",
                      'text'     => "Approve",
                      'value'    => '1',
                      'url'      => 'insertapprove.php',
                      'type'     => 'button',    
                      'confirm'  => [                                                                       
                'title' => 'Confirm',
                'text'  => 'Your Leave Requested has been Approved',
                'ok_text' => 'Yes',
                'dismiss_text' => 'No',
                                    ]
                                    ],
                                  [
                'name'     => "two",
                'text'     => "Reject",
                'value'    => '2',
                'type'     => 'button',
                'confirm'  => [
                'title'        => 'Confirm',
                'text'         => 'Your Leave Requested is Cancelled',
                'ok_text'      => 'No',
                'dismiss_text' => 'No',
                               ]
                               ]
                               )
                               ]),   
      "icon_url"      =>  'http://localhost/images/nestor-icon.png'
        ));
    $ch = curl_init("https://hooks.slack.com/services/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");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result     = curl_exec($ch);
    curl_close($ch);
    echo "Slack response : ". $result ;
    return $result;
}

call function

$message2 = "Tes Channel 2";
$channel2 = "random";
slacktest($message2,$channel2);

Solution

  • To get the "value back" after someone clicked the button, Slack will send a request to your app. For that Slack needs to know the URL of your app and your app needs to be able to handle interactive message requests from Slack.

    Therefore you need to define a Slack app. There you can specify the URL of your app (called "Request URL", under Interactive Components) that Slack will call once a button is pressed.

    For more details see Responding to message actions in the official documentation.