PHP Classes
elePHPant
Icontem

PHP Slack Message: Send messages to Slack users

Recommend this page to a friend!
  Info   View files Documentation   View files View files (22)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-01-23 (7 months ago) RSS 2.0 feedNot enough user ratingsTotal: 74 All time: 9,180 This week: 291Up
Version License PHP version Categories
slack 1.10.1Custom (specified...5.5PHP 5, Web services, Chat
Description Author

This package can send messages to Slack users.

It can send HTTP requests to the Slack API Web servers to perform several types of operations. Currently it can:

- Send simple text messages to the default channel or a specific channel
- Send a message from a specific user to another user
- Send messages with given icons
- Send attachments
- Send messages without Markdown parsing
- Etc..

  Performance   Level  
Name: Alexander Chibrikin <contact>
Classes: 1 package by
Country: Russian Federation Russian Federation
Age: 37
All time rank: 4208119 in Russian Federation Russian Federation
Week rank: 772 Up26 in Russian Federation Russian Federation Up

Details

maknz/slack

Build Status Scrutinizer Code Quality Dependency Status StyleCI Slack Welcome

A simple PHP package for sending messages to Slack with incoming webhooks, focused on ease-of-use and elegant syntax.

Requirements

  • PHP 5.5, 5.6, 7.0, 7.1, 7.2 or HHVM

Installation

You can install the package using the Composer package manager. You can install it by running this command in your project root:

composer require alek13/slack

Then create an incoming webhook on your Slack account for the package to use. You'll need the webhook URL to instantiate the client (or for the configuration file if using Laravel).

Laravel

We include service providers and a facade for easy integration and a nice syntax for Laravel.

Firstly, add the Maknz\Slack\SlackServiceProvider provider to the providers array in config/app.php (or app/config.php for Laravel 4)

'providers' => [
  ...
  'Maknz\Slack\SlackServiceProvider',
],

and then add the facade to your aliases array

'aliases' => [
  ...
  'Slack' => 'Maknz\Slack\Facades\Slack',
],

Configuration

Publish the configuration file with:

// Laravel 5, file will be at config/slack.php
php artisan vendor:publish --provider="Maknz\Slack\SlackServiceProviderLaravel5"

// Laravel 4, file will be at app/config/packages/maknz/slack/config.php
php artisan config:publish maknz/slack

Head into the file and configure the defaults you'd like the package to use. If null is set for any, the package will fall back on the default set on the webhook.

The configuration file is used to bypass the client instantiation process to make using the package easier. Therefore, you can skip the the Instantiate the client section below and dive right into using the package.

Basic Usage

Instantiate the client

// Instantiate without defaults
$client = new Maknz\Slack\Client('http://your.slack.endpoint');

// 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.
$settings = [
	'username' => 'Cyril',
	'channel' => '#accounting',
	'link_names' => true
];

$client = new Maknz\Slack\Client('http://your.slack.endpoint', $settings);

Settings

All settings are optional, but are a convenient way of specifying how the client should behave beyond the defaults.

  • channel: the default channel that messages will be sent to * string * default: the setting on the webhook
  • username: the default username that messages will be sent from * string * default: the setting on the webhook
  • icon: the default icon messages will be sent with, either :emoji: or a URL to an image * string * default: the setting on the webhook
  • link_names: whether names like @regan or #accounting should be linked * bool * default: false
  • unfurl_links: whether Slack should unfurl text-based URLs * bool * default: false
  • unfurl_media: whether Slack should unfurl media-based URLs * bool * default: true
  • allow_markdown: whether Markdown should be parsed in messages * bool * default: true
  • markdown_in_attachments: which attachment fields should have Markdown parsed * array * default: []

Sending messages

To send messages, you will call methods on your client instance, or use the Slack facade if you are using the package in Laravel.

Sending a basic message

// With an instantiated client
$client->send('Hello world!');

// or the Laravel facade
Slack::send('Hello world!');

Sending a message to a non-default channel

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

// or the Laravel facade
Slack::to('#accounting')->send('Are we rich yet?');

Sending a message to a user

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

Sending a message to a channel as a different username

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

Sending a message with a different icon

// 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');

Send an attachment

$client->to('@regan')->attach([
	'fallback' => 'It is all broken, man', // Fallback text for plaintext clients, like IRC
	'text' => 'It is all broken, man', // The text for inside the attachment
	'pretext' => 'From user: JimBob', // Optional text to appear above the attachment and below the actual message
	'color' => 'danger', // Change the color of the attachment, default is 'good'. May be a hex value or 'good', 'warning', or 'danger'
])->send('New alert from the monitoring system');

Send an attachment with fields

$client->to('#operations')->attach([
	'fallback' => 'It is all broken, man',
	'text' => 'It is all broken, man',
	'pretext' => 'From user: JimBob',
	'color' => 'danger',
	'fields' => [
		[
			'title' => 'Metric 1',
			'value' => 'Some value'
		],
		[
			'title' => 'Metric 2',
			'value' => 'Some value',
			'short' => true // whether the field is short enough to sit side-by-side other fields, defaults to false
		]
	]
])->send('New alert from the monitoring system');

Send a message modifying Markdown parsing on the fly

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

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

Send an attachment specifying Markdown parsing on the fly

$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');

Send an attachment with an author

$client->to('@regan')->attach([
	'fallback' => 'Things are looking good',
	'text' => 'Things are looking good',
	'author_name' => 'Bobby Tables',
	'author_link' => 'http://flickr.com/bobby/',
	'author_url' => 'http://flickr.com/icons/bobby.jpg'
])->send('New alert from the monitoring system');

Advanced usage

Explicit message creation

For convenience, message objects are created implicitly by calling message methods on the client. We can however do this explicitly to avoid hitting the magic method.

// 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();

Attachments

When using attachments, the easiest way is to provide an array of data as shown in the examples, which is actually converted to an Attachment object under the hood. You can also attach an Attachment object to the 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')->send();

Each attachment field is also an object, an AttachmentField. They can be used as well instead of their data in array form:

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

You can also set the attachments and fields directly if you have a whole lot of them:

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

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

$attachment = new Attachment([]);

$attachment->setFields($bigArrayOfFields);

Contributing

If you're having problems, spot a bug, or have a feature suggestion, please log and issue on Github. If you'd like to have a crack yourself, fork the package and make a pull request. Please include tests for any added or changed functionality. If it's a bug, include a regression test.

  Files folder image Files  
File Role Description
Files folder imagesrc (9 files, 2 directories)
Files folder imagetests (4 files)
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageconfig (1 file)
Files folder imageFacades (1 file)
  Plain text file ActionConfirmation.php Class Class source
  Plain text file Attachment.php Class Class source
  Plain text file AttachmentAction.php Class Class source
  Plain text file AttachmentField.php Class Class source
  Plain text file Client.php Class Class source
  Plain text file Message.php Class Class source
  Plain text file SlackServiceProvider.php Class Class source
  Plain text file SlackServiceProviderLaravel4.php Class Class source
  Plain text file SlackServiceProviderLaravel5.php Class Class source

  Files folder image Files  /  src  /  config  
File Role Description
  Accessible without login Plain text file config.php Aux. Auxiliary script

  Files folder image Files  /  src  /  Facades  
File Role Description
  Plain text file Slack.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file AttachmentUnitTest.php Class Class source
  Plain text file ClientFunctionalTest.php Class Class source
  Plain text file ClientUnitTest.php Class Class source
  Plain text file MessageUnitTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:74
This week:0
All time:9,180
This week:291Up