PHP code example of ryantxr / slack-webhook

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

    

ryantxr / slack-webhook example snippets




use Ryantxr\Slack\Webhook\Client as Webhook;

$webhook = new Webhook( 'YOUR_SLACK_WEBHOOK_URL' );
$webhook->message('This is a message');
// That's it!



use Ryantxr\Slack\Webhook\Client as Webhook;

$config = [
    'channel1' => 'YOUR_SLACK_CHANNEL1_WEBHOOK_URL',
    'channel2' => 'YOUR_SLACK_CHANNEL2_WEBHOOK_URL'
];
$webhook = new Webhook( $config );
$webhook->channel('channel1')->message('This is a message');

$slackToken = 'YOUR_SLACK_TOKEN';
$channelDescriptor = '{"channel_id": "C0000000008", "name": "random"}';
$channelDescriptor2 = '{"channel_id": "C1110000008", "name": "general"}';
use Ryantxr\Slack\Communicator\Client as SlackCommunicator;
$comm = new SlackCommunicator($slackToken);
// Set a default channel
$comm->channel($channelDescriptor);
// Send message to default channel
$comm->message('Some message');
// That's it!!

// Send to a different channel
$comm->message('Some message', $channelDescriptor2);

$icon = ':blue_car:';
$comm->icon($icon); // set default

// Send message to default channel with a specific icon.
$comm->message('Some message', null, $icon);

// Send message to a specific channel with a specific icon.
$comm->message('Some message', $channelDescriptor2, $icon);

function getObject() {
    // Assume there is some way to get the configuration
    $comm = (new SlackCommunicator(config('slack_token')))
        ->channel(config('slack_channel'))
        ->setIcon(config('slack_icon'));
    return $comm;
}