PHP code example of mcrumm / phlack

1. Go to this page and download the library: Download mcrumm/phlack 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/ */

    

mcrumm / phlack example snippets



$phlack = new Crummy\Phlack\Phlack('https://my.webhook.url');

$response = $phlack->send('Hello, from Phlack!');

if (200 === $response['status']) {
    echo 'Success!';
}


$phlack  = new Crummy\Phlack\Phlack([
    'username' => 'myteam',
    'token'    => 'my_webhook_token'
]);


$phlack = Crummy\Phlack\Phlack::factory($config);


$client = new Crummy\Phlack\Bridge\Guzzle\PhlackClient('https://my.webhook.url');
$phlack = new Crummy\Phlack\Phlack($client);


// ...
$messageBuilder = $phlack->getMessageBuilder();
$messageBuilder
  ->setText('I was created in the MessageBuilder')
  ->setChannel('testing')
  ->setIconEmoji('ghost');
$message = $messageBuilder->create();

$messageBuilder = $phlack->getMessageBuilder(); // Get the MessageBuilder

$messageBuilder
    ->setText('This message contains multiple attachments.') // Message text.
    ->createAttachment()  // Returns the AttachmentBuilder.
        ->setTitle($title)
        ->setTitleLink($link)
        ->setPretext($pretext)
        ->setText($body)
        ->setColor($color)
        ->setFallback($title . ' ' . $pretext)
    ->end() // Creates the first attachment and returns the MessageBuilder
    ->setUsername($username) // Sets username on the Message object.
    ->createAttachment() // Returns the AttachmentBuilder.
        ->setTitle('Attachment #2')
        ->setFallback('Attachment #2 for example purposes')
        ->setText('Add multiple attachments to a Phlack Message via method chaining.')
    ->end() // Creates the second attachment and returns the MessageBuilder.
;

$message = $messageBuilder->create();


// ...

// Get the AttachmentBuilder
$attachmentBuilder = $phlack->getAttachmentBuilder();

// Create the Attachment
$attachment =
    $attachmentBuilder
        ->setTitle('My Attachment Title')
        ->setTitleLink('http://www.example.com')
        ->setPretext('Some optional pretext')
        ->setText('This is the body of my attachment')
        ->setColor($color)
        ->addField('Field 1', 'Some Value', true)
        ->setFallback($title . ' ' . $pretext)
    ->create()
;

// Create a Message to contain the Attachment
$message = new \Crummy\Phlack\Message\Message('This message contains an attachment.');

// Add the Attachment to the Message
$message->addAttachment($attachment);


//...
use Crummy\Phlack\Message\Message;
$message = new Message('Hello, from phlack!');
echo 'The message payload: ' . PHP_EOL:
echo $message; // Output: {"text": "Hello, from phlack!"}


//...
use Crummy\Phlack\Message\Message;
$message = new Message('Hello, from phlack!', '#random');
echo 'The message payload: ' . PHP_EOL:
echo $message; // Output: {"text": "Hello, from phlack!", "channel": "#random"}


// ...
$response = $phlack->send($message);

if (200 != $response['status']) {
  die('FAIL! - ' . $response['text']);
}

echo 'The message was sent: ' . $message;


$phlack->send([
    'channel'      => '#random',
    'icon_emoji'   => ':taco:',
    'username'     => 'Phlack',
    'unfurl_links' => true,
    'text'         => 'I :heart: the <http://api.slack.com|Slack API>!',
]);



use Crummy\Phlack\Bridge\Guzzle\ApiClient;

$slack = ApiClient::factory([ 'token' => 'my_bearer_token' ]);



use Crummy\Phlack\Bridge\Guzzle\ApiClient;

$slack = new ApiClient([ 'token' => 'my_bearer_token' ]);



use Crummy\Phlack\Bridge\Guzzle\ApiClient;

$config = [ 'token' => 'my_bearer_token' ];
$slack = new ApiClient($config);

echo 'Fetching Channels List...' . PHP_EOL;
$result = $slack->ListChannels();

if (!$result['ok']) {
    die('FAIL! Error was: ' . $result['error'] . PHP_EOL);
}

foreach ($result['channels'] as $channel) {
    printf('%s: %s' . PHP_EOL, $channel['name'], $channel['purpose']['value']);
}


//...
$iterator = $slack->getIterator('ListFiles');

$i = 0;
foreach ($iterator as $file) {
    $i++;
    echo $file['title'] . PHP_EOL;
}

echo PHP_EOL . 'Retrieved ' . $i . ' files.' . PHP_EOL;