4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SymfonyAdvent Calendar 2022

Day 14

symfony/slack-notifier使ってみた

Last updated at Posted at 2022-12-13

Symfony Advent Calendar 2022 14日目の記事です✨

symfony/slack-notifierを使ってみたよーって記事です。

symfony/slack-notifierはどんなもの?

まず、symfony/notifierという通知するためのコンポーネントがもあります。
このコンポーネントは、

  • SMS Channel
  • Chat Channel 👈Slackはココ
  • Email Channel
  • Browser Channel
  • Push Channel

をサポートしており、SlackなどはChat Channelに該当します。
これらはChatterクラスを使うことでメッセージを送信できるとのことです。
https://symfony.com/doc/current/notifier.html#chat-channel

そこで送信するSlackメッセージを組み立てるために提供されているのが今回使うsymfony/slack-notifierです🙌🔥

では、実際に使ってみた手順をご紹介します。

composer require symfony/slack-notifier

symfony/slack-notifierをrequireすると、symfony/notifierもinstallされます。

$ composer require symfony/slack-notifier
Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been updated
Running composer update symfony/slack-notifier
Loading composer repositories with package information
Info from https://repo.packagist.org: #StandWithUkraine
Restricting packages listed in "symfony/symfony" to "6.2.*"
Updating dependencies
Lock file operations: 4 installs, 0 updates, 0 removals
  - Locking symfony/http-client (v6.2.0)
  - Locking symfony/http-client-contracts (v3.1.1)
  - Locking symfony/notifier (v6.2.0)
  - Locking symfony/slack-notifier (v6.2.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Installing symfony/http-client-contracts (v3.1.1): Extracting archive
  - Installing symfony/notifier (v6.2.0): Extracting archive
  - Installing symfony/http-client (v6.2.0): Extracting archive
  - Installing symfony/slack-notifier (v6.2.0): Extracting archive
Generating autoload files
30 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

Symfony operations: 2 recipes (abfafcc985784e830f69f608b281401d)
  - Configuring symfony/notifier (>=5.0): From github.com/symfony/recipes:main
  - Configuring symfony/slack-notifier (>=5.2): From github.com/symfony/recipes:main
Executing script cache:clear [OK]
Executing script assets:install public [OK]
              
...

.envを調整

###> symfony/slack-notifier ###
SLACK_DSN=slack://xoxb-xxxx@default?channel=xxxx
###< symfony/slack-notifier ###

notifier.yamlのコメントアウトをはずす

yaml
framework:
    notifier:
        chatter_transports:
- #            slack: '%env(SLACK_DSN)%'
+             slack: '%env(SLACK_DSN)%'

実際にSlack通知してみる

<?php
declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackContextBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackImageBlockElement;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Routing\Annotation\Route;

class SlackNotifierController extends AbstractController
{
    /**
     * @throws TransportExceptionInterface
     */
    #[Route(path: '/send-chat', name: 'app_send_chat')]
    public function sendChat(ChatterInterface $chatter): Response
    {
        $slackSectionBlock = new SlackSectionBlock();
        $slackSectionBlock->text('A message *with some bold text* and _some italicized text_.');

        $slackContextBlock = new SlackContextBlock();
        $slackContextBlock->text('*This* is :smile: markdown');
        $slackContextBlock->image(
            'https://pbs.twimg.com/profile_images/625633822235693056/lNGUneLX_400x400.jpg',
            'cute cat'
        );
        $slackContextBlock->image(
            'https://pbs.twimg.com/profile_images/625633822235693056/lNGUneLX_400x400.jpg',
            'cute cat'
        );
        $slackContextBlock->text(
            'Author: K A Applegate',
            false,
        );

        $slackOptions = new SlackOptions();
        $slackOptions->block($slackSectionBlock);
        $slackOptions->block($slackContextBlock);

        $chatMessage = new ChatMessage('subject');
        $chatMessage->options($slackOptions);

        $chatter->send($chatMessage);

        return new Response('slack done', 200, []);
    }
}

以下のメッセージ通知ができました🎉

スクリーンショット 2022-12-13 14.53.16.png

[おまけ] section block with an accessory image

スクリーンショット 2022-12-13 14.53.00.png

$slackImageBlockElement = new SlackImageBlockElement(
    'https://pbs.twimg.com/profile_images/625633822235693056/lNGUneLX_400x400.jpg',
    'cute cat'
);

$slackSectionBlock = new SlackSectionBlock();
$slackSectionBlock->text('This is a section block with an accessory image.');
$slackSectionBlock->accessory($slackImageBlockElement);

$slackOptions = new SlackOptions();
$slackOptions->block($slackSectionBlock);

$chatMessage = new ChatMessage('subject image section');
$chatMessage->options($slackOptions);

$chatter->send($chatMessage);

感想

配列を書くのは大変なので、Symfony公式であるの助かります :bow:
buttonとimageはあるのですが、input系はまだないので自分で作る必要がありそうでした。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?