PHP code example of alek13 / slack

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

    

alek13 / slack example snippets


// Instantiate without defaults
$client = new Maknz\Slack\Client('https://hooks.slack.com/...');

// Instantiate with defaults, so all messages created
// will be sent from 'Cyril' and to the #accounting channel
// by default. Any names like @regan or #channel will also be linked.
// use response_type (in_channel | ephemeral) to denote whether the message will be visible
// to others in the channel.
$settings = [
    'username'     => 'Cyril',
    'channel'      => '#accounting',
    'reponse_type' => 'in_channel',
    'link_names'   => true
];

$client = new Maknz\Slack\Client('https://hooks.slack.com/...', $settings);

$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([
    'fallback' => 'Server health: good',
    'text' => 'Server health: good',
    'color' => 'danger',
])->send('New alert from the monitoring system'); // no message, but can be provided if you'd like

$client->to('#operations')->attach([
    'fallback' => 'Current server stats',
    'text' => 'Current server stats',
    'color' => 'danger',
    'fields' => [
        [
            'title' => 'CPU usage',
            'value' => '90%',
            'short' => true // whether the field is short enough to sit side-by-side other fields, defaults to false
        ],
        [
            'title' => 'RAM usage',
            'value' => '2.5GB of 4GB',
            'short' => true
        ]
    ]
])->send('New alert from the monitoring system'); // no message, but can be provided if you'd like

$client->to('@regan')->attach([
    'fallback'    => 'Keep up the great work! I really love how the app works.',
    'text'        => 'Keep up the great work! I really love how the app works.',
    'author_name' => 'Jane Appleseed',
    'author_link' => 'https://yourapp.com/feedback/5874601',
    'author_icon' => 'https://static.pexels.com/photos/61120/pexels-photo-61120-large.jpeg'
])->send('New user feedback');

$client->to('@regan')
    ->withBlock([
        'type' => 'section',
        'text' => 'Do you love the app?'
    ])
    ->withBlock([
        'type' => 'actions',
        'elements' => [[
            'type'      => 'button',
            'text'      => 'Love it',
            'style'     => 'primary',
            'action_id' => 'love',
        ], [
            'type'      => 'button',
            'text'      => 'Hate it',
            'style'     => 'danger',
            'action_id' => 'hate',
        ],]
    ])
    ->send('Notification fallback message');

$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([
    'fallback'  => 'It is all broken, man',
    'text'      => 'It is _all_ broken, man',
    'pretext'   => 'From user: *JimBob*',
    'color'     => 'danger',
    'mrkdwn_in' => ['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')
;

$client->send($message);

$attachment = new Attachment([
    'fallback' => 'Some fallback text',
    'text'     => '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');

$client->send($message);

$attachment = new Attachment([
    'fallback' => 'Some fallback text',
    'text' => 'The attachment text',
    'fields' => [
        new AttachmentField([
            'title' => 'A title',
            'value' => 'A value',
            'short' => true
        ])
    ]
]);

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

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

$attachment = new Attachment([]);

$attachment->setFields($bigArrayOfFields);