Appearance
This guide outlines Slack API setup, and installation and configuration of the Slack service and channel.
Installation
From a project with a pre-installed version of Notifier and Notifier Chat Channel, bring in the Slack notifier dependency:
sh
composer require symfony/slack-notifierCreating a Slack App
A Slack app must be created, and a xoxb- bot api key issued, in order to use this integration.
Create app on Slack @ https://api.slack.com/apps
In the sidebar select Oauth & Permissions.
Under Bot Token Scopes, click Add an OAuth Scope and add a scope like
chat:write.
In the sidebar, go to Install App.
Select a Slack instance. Click "Allow" when prompted.
A "Bot User OAuth Token" will be presented which starts with
xoxb-. Copy this value for use as theTOKENbelow.
In your Slack instance, invite the App by going to a channel and typing
/invite. Then click Add apps to this channelSelect the App you just created.
Configuration
In the site directory, usually sites/default/, create a services.yml file.
Add to settings.php:
php
$settings['container_yamls'][] = __DIR__ . '/services.yml';Add to services.yml:
yaml
parameters:
chatter.transports:
my_slack_transport: 'slack://TOKEN@default?channel=CHANNEL'Modify the DSN by replacing the TOKEN and CHANNEL parts.
TOKEN is the Bot User OAuth Access Token set up above, which begins with xoxb-
The channel is the default channel messages are sent to. To send to a channel, use the channel name (do not prefix with # hash). To send to a user, prefix the username with a @ character.
Then clear Drupal caches:
sh
drush crSending
To send a message to a Slack channel or user, create a notification then send to the notifier service.
Pass the name of the Slack transport as configured in chatter.transports to the Notification::channels() method, prefixed with chat/.
Message body
Chat notifications use subject() for message contents, not contents().
php
namespace Drupal\my_module;
final class MyService {
public function __construct(
private readonly \Symfony\Component\Notifier\NotifierInterface $notifier,
) {}
public function myMethod(){
$notification = (new \Symfony\Component\Notifier\Notification\Notification())
->subject('Test message!')
# Specify the name of the Slack transport as configured in `chatter.transports`.
->channels(['chat/my_slack_transport']),
$this->notifier->send($notification);
}
}yaml
services:
_defaults:
autowire: true
my_service:
class: Drupal\my_module\MyServiceSlack Options
The Slack transport supports a variety of rich UI elements, like buttons. The list of supported extras can be found in the Slack transport README. To make use of these, create a custom Notification object which extends ChatNotificationInterface, and return a ChatMessage object with a \Symfony\Component\Notifier\Bridge\Slack\SlackOptions options object passed along.
php
namespace Drupal\my_module;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackActionsBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Recipient\RecipientInterface;
final class MySlackNotification extends Notification implements ChatNotificationInterface {
public function asChatMessage(RecipientInterface $recipient, ?string $transport = null): ?ChatMessage {
$slackOptions = new SlackOptions();
$slackOptions->block((new SlackSectionBlock())->text('A message!'));
$slackOptions->block((new SlackActionsBlock())
->button(
'Button 1',
'http://example.com/link1',
'primary',
)
->button(
'Button 2',
'http://example.com/link2',
'danger'
));
return new ChatMessage('', options: $slackOptions);
}
}php
namespace Drupal\my_module;
final class MyService {
public function __construct(
private readonly \Symfony\Component\Notifier\NotifierInterface $notifier,
) {}
public function myMethod(){
# Use notification class:
$this->notifier->send(new \Drupal\my_module\MySlackNotification());
}
}Resources
For more information about the Slack integration, go to Slack transport in the Symfony monorepo.
Slack API links:
This documentation is not provided or endorsed by Slack.