PHP code example of simonbackx / slack-php-webhook

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

    

simonbackx / slack-php-webhook example snippets


// Use the url you got earlier
$slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX');

// Create a new message
$message = new SlackMessage($slack);
$message->setText("Hello world!");

// Send it!
if ($message->send()) {
    echo "Hurray 😄";
} else {
    echo "Failed 😢";
}

// Use the url you got earlier
$slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX');

// Create a new message
$message = new SlackMessage($slack);
$message->setText("Hello world!")->setChannel("#general");

// Send it!
$message->send();

// Use the url you got earlier
$slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX');

// Create a new message
$message = new SlackMessage($slack);
$message->setText("Hello world!")->setChannel("@simonbackx");

// Send it!
$message->send();

$slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX');
$slack->setDefaultUsername("SlackPHP robot");
$slack->setDefaultChannel("#general");

// Unfurl links: automatically fetch and create attachments for detected URLs
$slack->setDefaultUnfurlLinks(true);

// Set the default icon for messages to a custom image
$slack->setDefaultIcon("http://www.domain.com/robot.png"); 

// Use a 👻 emoji as default icon for messages if it is not overwritten in messages
$slack->setDefaultEmoji(":ghost:");

// Create a new message
$message = new SlackMessage($slack);
$message->setText("Hello world!");
$message->setChannel("#general");

// Unfurl links: automatically fetch and create attachments for detected URLs
$message->setUnfurlLinks(false);

// Set the icon for the message to a custom image
$message->setIcon("http://www.domain.com/robot2.png");

// Overwrite the default Emoji (if any) with 😊
$message->setEmoji(":simple_smile:");

// Send it!
$message->send();


// Use the url you got earlier
$slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX');
$slack->setDefaultUsername('Fly company');

// Create a new message
$message = new SlackMessage($slack);

$attachment = new SlackAttachment("Required plain-text summary of the attachment.");
$attachment->setColor("#36a64f");
$attachment->setText("*Optional text* that appears within the attachment");
$attachment->setPretext("Optional text that appears above the attachment block");
$attachment->setAuthor(
    "Author name", 
    "http://flickr.com/bobby/", //Optional author link
    "http://flickr.com/bobby/picture.jpg" // Optional author icon
);
$attachment->setTitle("Title", "Optional link e.g. http://www.google.com/");
$attachment->setImage("http://www.domain.com/picture.jpg");

/*
Slack messages may be formatted using a simple markup language similar to Markdown. Supported 
formatting 

// Use the url you got earlier
$slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX');
$slack->setDefaultUsername('Fly company');

// Create a new message
$message = new SlackMessage($slack);
$message->setText("<@W1A2BC3DD> approved your travel request. Book any airline you like by continuing below.");

// Create a new Attachment with fallback text, a plain-text summary of the attachment. 
// This text will be used in clients that don't show formatted text (eg. IRC, mobile 
// notifications) and should not contain any markup.
$attachment = new \SlackAttachment('Book your flights at https://flights.example.com/book/r123456');
$attachment->addButton('Book flights 🛫', 'https://flights.example.com/book/r123456');
$attachment->addButton('Unsubscribe', 'https://flights.example.com/unsubscribe', 'danger');

$message->addAttachment($attachment);

$message->send();

$message = new SlackMessage($slack);
$message->addAttachment($attachment1);
$message->addAttachment($attachment2);
$message->send();

(new SlackMessage($slack))
    ->addAttachment($attachment1)
    ->addAttachment($attachment2)
    ->send();
json
{
  "onbackx/slack-php-webhook": "~1.0"
  }
}