PHP code example of hampel / slack-message
1. Go to this page and download the library: Download hampel/slack-message 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/ */
hampel / slack-message example snippets
$slack = new SlackWebhook(new GuzzleHttp\Client());
$slack = new SlackWebhook($client, $requestFactory, $streamFactory);
use GuzzleHttp\Client;
use Hampel\SlackMessage\SlackMessage;
use Hampel\SlackMessage\SlackWebhook;
$url = 'https://hooks.slack.com/services/<Slack incoming webhook url>';
$slack = new SlackWebhook(new Client());
$message = $slack->message(function ($message) {
$message
->content('Content')
->attachment(function ($attachment) {
$attachment
->title('Laravel', 'https://laravel.com')
->content('Attachment Content')
->fallback('Attachment Fallback')
->fields([
'Project' => 'Laravel',
])
->footer('Laravel')
->footerIcon('https://laravel.com/fake.png')
->markdown(['text'])
->author('Author', 'https://laravel.com/fake_author', 'https://laravel.com/fake_author.png')
->timestamp(new DateTimeImmutable());
});
});
$slack->send($url, $message);
use GuzzleHttp\Client;
use Hampel\SlackMessage\SlackWebhook;
$slack = new SlackWebhook(new Client());
$message = $slack->message(function ($message) use ($token) {
$message
->from('Deploy Bot', ':rocket:')
->to('#alerts')
->success()
->content('Deployed successfully.')
->http(['headers' => ['Authorization' => 'Bearer ' . $token]]);
});
$response = $slack->send('https://slack.com/api/chat.postMessage', $message);
if (! $slack->accepted($response)) {
throw new RuntimeException('Slack refused the message: ' . $slack->error($response));
}
$response = $slack->send($url, $message);
if (! $slack->accepted($response)) {
// channel_not_found, invalid_auth, not_in_channel, invalid_payload ...
throw new RuntimeException('Slack refused the message: ' . $slack->error($response));
}
$payload = $slack->buildPayload($message);
// ... store $payload, hand it to a job queue, come back later ...
$slack->sendPayload($url, $payload);
$slack->sendPayload($url, $payload, ['headers' => ['Authorization' => 'Bearer ' . $token]]);