PHP code example of nexylan / slack

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

    

nexylan / slack example snippets


use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;

use Nexy\Slack\Client;

$client = new Client(
    Psr18ClientDiscovery::find(),
    Psr17FactoryDiscovery::findRequestFactory(),
    Psr17FactoryDiscovery::findStreamFactory(),
    'https://hooks.slack.com/...',
    [
        'username' => 'Cyril', // Default messages are sent from 'Cyril'
        'channel' => '#accounting', // Default messages are sent to '#accounting'
        'link_names' => true
    ]
);

$client->send('Hello world!');

$client->to('#accounting')->send('Are we rich yet?');

$client->to('@regan')->send('Yo!');

$client->from('Jake the Dog')->to('@FinnTheHuman')->send('Adventure time!');

// Either with a Slack emoji
$client->to('@regan')->withIcon(':ghost:')->send('Boo!');

// or a URL
$client->to('#accounting')->withIcon('http://example.com/accounting.png')->send('Some accounting notification');

$client->to('#operations')->attach((new \Nexy\Slack\Attachment())
    ->setFallback('Server health: good')
    ->setText('Server health: good')
    ->setColor('danger')
)->send('New alert from the monitoring system'); // no message, but can be provided if you'd like

$client->to('#operations')->attach((new \Nexy\Slack\Attachment())
    ->setFallback('Current server stats')
    ->setText('Current server stats')
    ->setColor('danger')
    ->setFields([
        new \Nexy\Slack\AttachmentField(
            'Cpu usage',
            '90%',
            true // whether the field is short enough to sit side-by-side other fields, defaults to false
        ),
        new \Nexy\Slack\AttachmentField('RAM usage', '2.5GB of 4GB', true),
    ])
)->send('New alert from the monitoring system'); // no message, but can be provided if you'd like

$client->to('@regan')->attach((new \Nexy\Slack\Attachment())
    ->setFallback('Keep up the great work! I really love how the app works.')
    ->setText('Keep up the great work! I really love how the app works.')
    ->setAuthorName('Jan Appleseed')
    ->setAuthorLink('https://yourapp.com/feedback/5874601')
    ->setAuthorIcon('https://static.pexels.com/photos/61120/pexels-photo-61120-large.jpeg')
)->send('New user feedback');

$client->to('#weird')->disableMarkdown()->send('Disable *markdown* just for this message');

$client->to('#general')->enableMarkdown()->send('Enable _markdown_ just for this message');

$client->to('#operations')->attach((new \Nexy\Slack\Attachment())
    ->setFallback('It is all broken, man')
    ->setText('It is _all_ broken, man')
    ->setPretext('From user: *JimBob*')
    ->setColor('danger')
    ->setMarkdownFields(['pretext', 'text'])
)->send('New alert from the monitoring system');

// Implicitly
$client->to('@regan')->send('I am sending this implicitly');

// Explicitly
$message = $client->createMessage();

$message->to('@regan')->setText('I am sending this explicitly');

$message->send();

$attachment = (new Attachment())
    ->setFallback('Some fallback text')
    ->setText('The attachment text')
;

// Explicitly create a message from the client
// rather than using the magic passthrough methods
$message = $client->createMessage();

$message->attach($attachment);

// Explicitly set the message text rather than
// implicitly through the send method
$message->setText('Hello world')->send();

// implicitly create a message and set the attachments
$client->setAttachments($bigArrayOfAttachments);

// or explicitly
$client->createMessage()->setAttachments($bigArrayOfAttachments);

$attachment = new Attachment();

$attachment->setFields($bigArrayOfFields);