A simple PHP package for sending messages to Slack with incoming webhooks, focused on ease-of-use and elegant syntax.
Slack for PHP alternatives and similar libraries
Based on the "API" category.
Alternatively, view Slack for PHP alternatives based on common mentions on social networks and blogs.
9.2 6.5 Slack for PHP VS API PlatformCreate REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.
Unofficial Firebase Admin SDK for PHP
7.3 3.7 L1 Slack for PHP VS RestlerSimple and effective multi-format Web API Server to host your PHP API as Pragmatic REST and/or RESTful API
6.5 0.0 L3 Slack for PHP VS wsdl2phpgeneratorSimple utility and class library for generating php classes from a wsdl file.
This bundle provides tools to build a complete GraphQL server in your Symfony App.
Pure PHP implementation of GraphQL Server – Symfony Bundle
DataTables bundle for Symfony
A minimal, highly performant middleware PSR-15 microframework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.
PSR-15/PSR-7 compliant middleware using ivanakimov/hashids.php
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Slack for PHP or a related project?
Slack for PHP
guzzlehttp/guzzle any of versions
This is the fork of popular, great, but abandoned package
- create an incoming webhook & copy
composer require alek13/slack
add the following code
use Maknz\Slack\Client; require(__DIR__ .'/vendor/autoload.php'); $client = new Client('https://hook_url'); $client->to('#general')->send('Good morning');
You can install the package using the Composer package manager by running 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).
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);
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.
||string||The default channel that messages will be sent to|
||string||The default username for your bot|
||string||The default icon that messages will be sent with, either
||string||Whether to show the response in the channel to all members or privately ('ephemeral'|
||bool||Whether names like
||bool||Whether Slack should unfurl text-based URLs (defaults to false)|
||bool||Whether Slack should unfurl media-based URLs, like tweets or Youtube videos (defaults to true)|
||bool||Whether markdown should be parsed in messages, or left as plain text (defaults to true)|
||array||Which attachment fields should have markdown parsed (defaults to none)|
Sending a basic message (preview)
Sending a message to a non-default channel
$client->to('#accounting')->send('Are we rich yet?');
Sending a message to a user
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');
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') ; $client->send($message);
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'); $client->send($message);
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);
There is the
php-slack/playground simple console script to test messaging and to see how messages looks really.
If you have any questions how to use or contribute,
you are welcome in our Slack Workspace.
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.