PHP Classes

PHP Slack Message: Send messages to Slack users

Recommend this page to a friend!
  Info   View files Documentation   Demos   View files View files (72)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 142 This week: 1All time: 9,177 This week: 560Up
Version License PHP version Categories
slack 2.0.0Custom (specified...8PHP 5, Web services, Chat
Description 

Authors

maknz
Alexander Chibrikin


Contributor

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..

Picture of Alexander Chibrikin
  Performance   Level  
Name: Alexander Chibrikin <contact>
Classes: 1 package by
Country: Russian Federation Russian Federation
Age: 43
All time rank: 4293118 in Russian Federation Russian Federation
Week rank: 416 Up24 in Russian Federation Russian Federation Up

Documentation

Slack for PHP

Build Status Code Coverage Scrutinizer Code Quality StyleCI | Slack Welcome

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

supports: PHP 7.0, 7.1, 7.2 require: guzzlehttp/guzzle any of versions ~6.0|~5.0|~4.0

> This is the fork of popular, great, but abandoned package maknz/slack

Quick Tour

- composer require alek13/slack - create an incoming webhook & copy hook_url - $client = new Client('http://hook_url') - $client->to('#general')->send('Good morning');

Done!

Installation

You can install the package using the Composer package manager by running in your project root:

composer require alek13/slack

Incoming WebHook

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).

Basic Usage

Instantiate the client

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

Settings

The default settings are pretty good, but you may wish to set up default behaviour for your client to be used for all messages sent. All settings are optional and you don't need to provide any. Where not provided, we'll fallback to what is configured on the webhook integration, which are managed at Slack, or our sensible defaults.

Field | Type | Description ----- | ---- | ----------- channel | string | The default channel that messages will be sent to username | string | The default username for your bot icon | string | The default icon that messages will be sent with, either :emoji: or a URL to an image response_type | string | Whether to show the response in the channel to all members or privately ('ephemeral' | 'in_channel') link_names | bool | Whether names like @regan or #accounting should be linked in the message (defaults to false) unfurl_links | bool | Whether Slack should unfurl text-based URLs (defaults to false) unfurl_media | bool | Whether Slack should unfurl media-based URLs, like tweets or Youtube videos (defaults to true) allow_markdown | bool | Whether markdown should be parsed in messages, or left as plain text (defaults to true) markdown_in_attachments | array | Which attachment fields should have markdown parsed (defaults to none)

Sending messages

Sending a basic message (preview)

$client->send('Hello world!');

Sending a message to a non-default channel

$client->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 bot name (preview)

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

Sending a message with a different icon (preview)

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

$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

Send an attachment with fields (preview)

$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

Send an attachment with an author (preview)

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

Using blocks (Block Kit)

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

Advanced usage

Markdown

By default, Markdown is enabled for message text, but disabled for attachment fields. This behaviour can be configured in settings, or on the fly:

Send a message enabling or disabling Markdown

$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 which fields should have Markdown enabled

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

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

Playground

There is the php-slack/playground simple console script to test messaging and to see how messages looks really.

Questions

Slack Welcome

If you have any questions how to use or contribute, you are welcome in our Slack Workspace.

Contributing

Slack Welcome

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.


  [RU] How to send message to Slack with PHPExternal page  

Open in a separate window

  Files folder image Files  
File Role Description
Files folder image.github (1 file)
Files folder imagesrc (13 files, 3 directories)
Files folder imagetests (8 files, 3 directories)
Accessible without login Plain text file .editorconfig 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.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageBlock (8 files)
Files folder imageBlockElement (13 files)
Files folder imageObject (4 files)
  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 Block.php Class Class source
  Plain text file BlockElement.php Class Class source
  Plain text file Client.php Class Class source
  Plain text file Field.php Class Class source
  Plain text file FieldsTrait.php Class Class source
  Plain text file ImageTrait.php Class Class source
  Plain text file Message.php Class Class source
  Plain text file OptionsTrait.php Class Class source
  Plain text file Payload.php Class Class source

  Files folder image Files  /  src  /  Block  
File Role Description
  Plain text file Actions.php Class Class source
  Plain text file Context.php Class Class source
  Plain text file Divider.php Class Class source
  Plain text file ElementsBlock.php Class Class source
  Plain text file File.php Class Class source
  Plain text file Image.php Class Class source
  Plain text file Input.php Class Class source
  Plain text file Section.php Class Class source

  Files folder image Files  /  src  /  BlockElement  
File Role Description
  Plain text file AbstractSelect.php Class Class source
  Plain text file Button.php Class Class source
  Plain text file Checkboxes.php Class Class source
  Plain text file Confirmable.php Class Class source
  Plain text file DatePicker.php Class Class source
  Plain text file Image.php Class Class source
  Plain text file MultiSelect.php Class Class source
  Plain text file Options.php Class Class source
  Plain text file Overflow.php Class Class source
  Plain text file RadioButtons.php Class Class source
  Plain text file Select.php Class Class source
  Plain text file Text.php Class Class source
  Plain text file TextInput.php Class Class source

  Files folder image Files  /  src  /  Object  
File Role Description
  Plain text file CompositionObject.php Class Class source
  Plain text file Confirmation.php Class Class source
  Plain text file Option.php Class Class source
  Plain text file OptionGroup.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageBlock (7 files)
Files folder imageBlockElement (10 files)
Files folder imageObject (1 file)
  Plain text file AttachmentUnitTest.php Class Class source
  Plain text file BlockElementUnitTest.php Class Class source
  Plain text file BlockUnitTest.php Class Class source
  Plain text file ClientFunctionalTest.php Class Class source
  Plain text file ClientUnitTest.php Class Class source
  Plain text file FieldsTraitUnitTest.php Class Class source
  Plain text file MessageUnitTest.php Class Class source
  Plain text file TestCase.php Class Class source

  Files folder image Files  /  tests  /  Block  
File Role Description
  Plain text file ActionsUnitTest.php Class Class source
  Plain text file ContextUnitTest.php Class Class source
  Plain text file DividerUnitTest.php Class Class source
  Plain text file FileUnitTest.php Class Class source
  Plain text file ImageUnitTest.php Class Class source
  Plain text file InputUnitTest.php Class Class source
  Plain text file SectionUnitTest.php Class Class source

  Files folder image Files  /  tests  /  BlockElement  
File Role Description
  Plain text file ButtonUnitTest.php Class Class source
  Plain text file CheckboxesUnitTest.php Class Class source
  Plain text file DatePickerUnitTest.php Class Class source
  Plain text file ImageUnitTest.php Class Class source
  Plain text file MultiSelectUnitTest.php Class Class source
  Plain text file OverflowUnitTest.php Class Class source
  Plain text file RadioButtonsUnitTest.php Class Class source
  Plain text file SelectUnitTest.php Class Class source
  Plain text file TextInputTestCase.php Class Class source
  Plain text file TextUnitTest.php Class Class source

  Files folder image Files  /  tests  /  Object  
File Role Description
  Plain text file OptionUnitTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:142
This week:1
All time:9,177
This week:560Up