Modern PHP Enterprise Systems Applications require the ability to notify the users about things like a process done, a report available, etc. through standard popular communication channels like email, text messages, slack etc. Follow this recipe if you want to send user notifications to their Slack channels:
Prerequisites:
- How to install Composer 2 in a Virtual Machine in 3 simple steps
- How to install Symfony Framework using composer in 2 easy steps
Step 1. Install the "symfony/slack-notifier" package using composer:
composer require symfony/slack-notifier
Step 2. Create a Slack bot by following the instructions on Slack's website: https://api.slack.com/bot-users#create-a-bot
Step 3. Obtain the Slack bot token.
Step 4. Create a Slack client instance in your Symfony application. Add the following lines to your Symfony application's configuration file (e.g. config/services.yaml):
services:
slack_client:
class: 'GuzzleHttp\Client'
Notice you need to have installed Guzzle Client. You can do that by
composer require guzzlehttp/guzzle
Step 5. Create a service for sending messages to Slack. Add the following lines to your Symfony application's configuration file:
services:
slack_notifier:
class: 'Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory'
arguments:
- '%env(SLACK_BOT_TOKEN)%'
- '%env(SLACK_DEFAULT_CHANNEL)%'
- '@slack_client'
Note that you should replace %env(SLACK_BOT_TOKEN)% and %env(SLACK_DEFAULT_CHANNEL)% with the values for your Slack bot token and default channel, respectively. You can store these values in your Symfony application's .env file or in your secrets vault.
Step 6. Create a function to send the message to Slack. Add the following code to a service:
<?php
namespace App\Services;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
class DefaultService
{
/**
* @var NotifierInterface
*/
private NotifierInterface $notifier;
/**
* @param NotifierInterface $notifier
*/
public function __construct(NotifierInterface $notifier)
{
$this->notifier = $notifier;
}
/**
* @param string $recipient
* @param string $subject
* @param array $urlLinks
* @return void
*/
public function sendMessageToSlack(string $recipient, string $subject, array $urlLinks)
{
// $urLinks has this structure
// [
// ['title' => 'Title 1', 'value' => 'https://example.com', 'short' => false],
// ['title' => 'Title 2', 'value' => 'https://example.com', 'short' => false]
// ]
//$recipient is the @slack_user or #slack_channel
$notification = (new Notification($subject, ['chat']))
->options(
(new SlackOptions())
->iconEmoji(':wave:')
->username('Symfony')
->recipient('#general')
->attachmentFields($urlLinks)
);
$this->notifier->send($notification);
}
}
Notice that you may have auto-wiring enabled.
Step 7. From any other function just call the above function:
//...
$this->sendMessageToSlack($recipient, $subject, $urlLinks);
Further reading:
Related Videos
- Create a Microservice with Symfony: Exception Response Content
- SlackLogger - Remote Exception Logging with Slack Channels in PHP