symfony/slack-notifier
Composer 安装命令:
composer require symfony/slack-notifier
包简介
Symfony Slack Notifier Bridge
README 文档
README
Provides Slack integration for Symfony Notifier.
DSN example
SLACK_DSN=slack://TOKEN@default?channel=CHANNEL
where:
TOKENis your Bot User OAuth Access Token (they begin withxoxb-)CHANNELis a channel, private group, or IM channel to send message to, it can be an encoded ID, or a name.
valid DSN's are:
SLACK_DSN=slack://xoxb-......@default?channel=my-channel-name
SLACK_DSN=slack://xoxb-......@default?channel=@fabien
invalid DSN's are:
SLACK_DSN=slack://xoxb-......@default?channel=#my-channel-name
SLACK_DSN=slack://xoxb-......@default?channel=fabien
Adding Interactions to a Message
With a Slack message, you can use the SlackOptions class to add some
interactive options called 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.com/doc/current/contributing/documentation/standards.html', 'primary' ) ->button( 'Report bugs', 'https://symfony.com/doc/current/contributing/code/bugs.html', 'danger', 'reportBugs' [ 'title' => ['type' => 'plain_text', 'text' => 'Report a bug'], 'text' => ['type' => 'plain_text', 'text' => 'By proceeding I confirm I\'ve read the guidelines.'], 'confirm' => ['type' => 'plain_text', 'text' => 'Proceed'], 'deny' => ['type' => 'plain_text', 'text' => 'Go back to reading'] ] ) ->id('contribute_block); $slackOptions = (new SlackOptions()) ->block((new SlackSectionBlock()) ->text('The Symfony Community') ->accessory( new SlackImageBlockElement( 'https://symfony.com/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);
Alternatively, a single button can be added to a section using the accessory() method and the SlackButtonBlockElement class.
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackButtonBlockElement; 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('Contribute To Symfony'); $slackOptions = (new SlackOptions()) ->block((new SlackSectionBlock()) ->text('Symfony Framework') ->accessory( new SlackButtonBlockElement( 'Report bugs', 'https://symfony.com/doc/current/contributing/code/bugs.html', 'danger' ) ) ) ->block(new SlackDividerBlock()) ->block((new SlackSectionBlock()) ->text('Symfony Documentation') ->accessory( new SlackButtonBlockElement( 'Improve Documentation', 'https://symfony.com/doc/current/contributing/documentation/standards.html', 'primary' ) ) ); // Add the custom options to the chat message and send the message $chatMessage->options($slackOptions); $chatter->send($chatMessage);
Adding Fields and Values to a Message
To add fields and values to your message you can use the field() method.
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);
Define text objects properties
Text objects properties can be set on any text() or field() method :
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock; use Symfony\Component\Notifier\Bridge\Slack\SlackOptions; use Symfony\Component\Notifier\Message\ChatMessage; $chatMessage = new ChatMessage('Slack Notifier'); $options = (new SlackOptions()) ->block( (new SlackSectionBlock()) ->field('My **Markdown** content with clickable URL : symfony.com') // Markdown content (default) ->field('*Plain text content*', markdown: false) // Plain text content ->field('Not clickable URL : symfony.com', verbatim: true) // Only for markdown content ->field('Thumbs up emoji code is :thumbsup: ', emoji: false) // Only for plain text content ); // Add the custom options to the chat message and send the message $chatMessage->options($options); $chatter->send($chatMessage);
Adding a Header to a Message
To add a header to your message use the SlackHeaderBlock class.
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);
Adding a Footer to a Message
To add a header to your message use the SlackContextBlock class.
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.com/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);
Sending a Message as a Reply
To send your Slack message as a reply in a thread use the threadTs() method.
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);
Updating a Slack Message
First, save the message ID and channel ID when sending a message:
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(); }
Then, use that message ID and channel ID to create a new
UpdateMessageSlackOptions class:
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));
Scheduling a Slack Message
To schedule a message to be sent at a later time, use the postAt() method:
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);
Sponsor
This package is looking for a backer.
Help Symfony by sponsoring its development!
Resources
symfony/slack-notifier 适用场景与选型建议
symfony/slack-notifier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.77M 次下载、GitHub Stars 达 42, 最近一次更新时间为 2019 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「notifier」 「slack」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 symfony/slack-notifier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 symfony/slack-notifier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 symfony/slack-notifier 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony JustCall Notifier Bridge
A Highly Opinionated Symfony3/4 Deployer Recipe
This bundle provides integration with the Slack API library, allowing you to interact with the Slack API from within your Symfony projects
Extensible library for building notifications and sending them via different delivery channels.
NO LIBRARIES socket per page bridge for your Laravel application.
Symfony SMS Line Notifier Bridge
统计信息
- 总下载量: 6.77M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 42
- 点击次数: 26
- 依赖项目数: 10
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-05