PHP code example of testmonitor / slack-client

1. Go to this page and download the library: Download testmonitor/slack-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

testmonitor / slack-client example snippets




$oauth = [
    'clientId' => '12345',
    'clientSecret' => 'abcdef',
    'redirectUri' => 'https://redirect.myapp.com/',
];

$slack = new \TestMonitor\Slack\Client($oauth);

header('Location: ' . $slack->authorizationUrl('incoming-webhook', 'state'));
exit();

$oauth = [
    'clientId' => '12345',
    'clientSecret' => 'abcdef',
    'redirectUri' => 'https://redirect.myapp.com/',
];

$slack = new \TestMonitor\Slack\Client($oauth);

$token = $slack->fetchToken($_REQUEST['code']);

var_dump ($token->getValues());

array() {
    ["ok"] => true
    ["app_id"] => "APPID"
    ["authed_user"] => array(1) {}
    ["scope"] => "incoming-webhook"
    ["token_type"] => "bot"
    ["bot_user_id"] => "USERID"
    ["team"] => array(2) {}
    ["enterprise"] => null
    ["is_enterprise_install"] => false
    ["incoming_webhook"] => array(4) {
      ["channel"] => "#testmonitor"
      ["channel_id"] => "CHANNELID"
      ["configuration_url"] => "https://domain.slack.com/services/B123456USA"
      ["url"] => "https://hooks.slack.com/services/T123456/B123456USA/tEsTm0n1t0r"
    }

$oauth = ['clientId' => '12345', 'clientSecret' => 'abcdef', 'redirectUri' => 'https://redirect.myapp.com/'];
$token = new \TestMonitor\Slack\Token('eyJ0...', '0/34ccc...', 1574600877); // the token you got last time

$slack = new \TestMonitor\Slack\Client($oauth, $token);

if ($token->expired()) {
    $newToken = $slack->refreshToken();
}

$message = Kit::newMessage()->text('Hello world!');

$slack->postMessage('https://webhook.url/', $message);

$user = (object) ['name' => 'John Doe'];

$message = Kit::newMessage()
    ->tap(function (Message $message) {
        $message->newSection()
            ->mrkdwnText("*{$user->name}* created a new issue");
    })
    ->divider()
    ->tap(function (Message $message) {
        $message->newContext()
            ->mrkdwnText('Status: *Open*')
            ->mrkdwnText('Priority: *High*')
            ->mrkdwnText('Resolution: *Unresolved*');
    })

$slack->postMessage('https://webhook.url/', $message);