symfony/slack-notifier

Symfony Slack Notifier 桥接器

安装次数: 3,034,784

依赖关系: 8

建议者: 0

安全性: 0

星标: 36

关注者: 6

分支: 14

类型:symfony-notifier-bridge


README

为 Symfony Notifier 提供 Slack 集成。

DSN 示例

SLACK_DSN=slack://TOKEN@default?channel=CHANNEL

其中

  • TOKEN 是您的 Bot 用户 OAuth 访问令牌(以 xoxb- 开头)
  • CHANNEL 是要发送消息到的频道、私有组或即时消息频道,它可以是一个编码 ID 或名称。

有效的 DSN 为

SLACK_DSN=slack://xoxb-......@default?channel=my-channel-name
SLACK_DSN=slack://xoxb-......@default?channel=@fabien

无效的 DSN 为

SLACK_DSN=slack://xoxb-......@default?channel=#my-channel-name
SLACK_DSN=slack://xoxb-......@default?channel=fabien

向消息添加交互

使用 Slack 消息,您可以使用 SlackOptions 类添加一些名为 Block elements 的交互选项。

use Symfony\Component\Notifier\Bridge\Slack\Block\SlackActionsBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
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\Message\ChatMessage;

$chatMessage = new ChatMessage('Contribute To Symfony');

// Create Slack Actions Block and add some buttons
$contributeToSymfonyBlocks = (new SlackActionsBlock())
    ->button(
        'Improve Documentation',
        'https://symfony.ac.cn/doc/current/contributing/documentation/standards.html',
        'primary'
    )
    ->button(
        'Report bugs',
        'https://symfony.ac.cn/doc/current/contributing/code/bugs.html',
        'danger'
    );

$slackOptions = (new SlackOptions())
    ->block((new SlackSectionBlock())
        ->text('The Symfony Community')
        ->accessory(
            new SlackImageBlockElement(
                'https://symfony.ac.cn/favicons/apple-touch-icon.png',
                'Symfony'
            )
        )
    )
    ->block(new SlackDividerBlock())
    ->block($contributeToSymfonyBlocks);

// Add the custom options to the chat message and send the message
$chatMessage->options($slackOptions);

$chatter->send($chatMessage);

向消息添加字段和值

要向您的消息添加字段和值,您可以使用 field() 方法。

use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('Symfony Feature');

$options = (new SlackOptions())
    ->block((new SlackSectionBlock())->text('My message'))
    ->block(new SlackDividerBlock())
    ->block(
        (new SlackSectionBlock())
            ->field('*Max Rating*')
            ->field('5.0')
            ->field('*Min Rating*')
            ->field('1.0')
    );

// Add the custom options to the chat message and send the message
$chatMessage->options($options);

$chatter->send($chatMessage);

向消息添加标题

要向您的消息添加标题,请使用 SlackHeaderBlock 类。

use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackHeaderBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('Symfony Feature');

$options = (new SlackOptions())
    ->block((new SlackHeaderBlock('My Header')))
    ->block((new SlackSectionBlock())->text('My message'))
    ->block(new SlackDividerBlock())
    ->block(
        (new SlackSectionBlock())
            ->field('*Max Rating*')
            ->field('5.0')
            ->field('*Min Rating*')
            ->field('1.0')
    );

// Add the custom options to the chat message and send the message
$chatMessage->options($options);

$chatter->send($chatMessage);

向消息添加页脚

要向您的消息添加页脚,请使用 SlackContextBlock 类。

use Symfony\Component\Notifier\Bridge\Slack\Block\SlackContextBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('Symfony Feature');

$contextBlock = (new SlackContextBlock())
    ->text('My Context')
    ->image('https://symfony.ac.cn/logos/symfony_white_03.png', 'Symfony Logo')
;

$options = (new SlackOptions())
    ->block((new SlackSectionBlock())->text('My message'))
    ->block(new SlackDividerBlock())
    ->block(
        (new SlackSectionBlock())
            ->field('*Max Rating*')
            ->field('5.0')
            ->field('*Min Rating*')
            ->field('1.0')
    )
    ->block($contextBlock)
;

// Add the custom options to the chat message and send the message
$chatMessage->options($options);

$chatter->send($chatMessage);

以回复形式发送消息

要作为线程中的回复发送您的 Slack 消息,请使用 threadTs() 方法。

use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('Symfony Feature');

$options = (new SlackOptions())
    ->block((new SlackSectionBlock())->text('My reply'))
    ->threadTs('1621592155.003100')
;

// Add the custom options to the chat message and send the message
$chatMessage->options($options);

$chatter->send($chatMessage);

更新 Slack 消息

首先,在发送消息时保存消息 ID 和频道 ID

use Symfony\Component\Notifier\Bridge\Slack\SlackSentMessage;
use Symfony\Component\Notifier\Message\ChatMessage;

$sentMessage = $chatter->send(new ChatMessage('Original message'));

// Make sure that Slack transport was used
if ($sentMessage instanceOf SlackSentMessage) {
    $messageId = $sentMessage->getMessageId();
    $channelId = $sentMessage->getChannelId();
}

然后,使用该消息 ID 和频道 ID 创建一个新的 UpdateMessageSlackOptions

use Symfony\Component\Notifier\Bridge\Slack\UpdateMessageSlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$options = new UpdateMessageSlackOptions($channelId, $messageId);
$chatter->send(new ChatMessage('Updated message', $options));

安排 Slack 消息

要安排在稍后时间发送消息,请使用 postAt() 方法

use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$options = (new SlackOptions())->postAt(new \DateTime('+1 day'));

$chatMessage = new ChatMessage('Symfony Feature');
$chatMessage->options($options);

$chatter->send($chatMessage);

资源