Skip to content

[Notifier] [Slack] Add button block element and emoji/verbatim options to section block #54737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,9 @@ public function button(string $text, string $url, ?string $style = null): static
throw new \LogicException('Maximum number of buttons should not exceed 25.');
}

$element = [
'type' => 'button',
'text' => [
'type' => 'plain_text',
'text' => $text,
],
'url' => $url,
];
$element = new SlackButtonBlockElement($text, $url, $style);

if ($style) {
// primary or danger
$element['style'] = $style;
}

$this->options['elements'][] = $element;
$this->options['elements'][] = $element->toArray();

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Slack\Block;

/**
* @author Christophe Vergne <christophe.vergne@gmail.com>
*/
final class SlackButtonBlockElement extends AbstractSlackBlockElement
{
public function __construct(string $text, string $url, ?string $style = null)
{
$this->options = [
'type' => 'button',
'text' => [
'type' => 'plain_text',
'text' => $text,
],
'url' => $url,
];

if ($style) {
// primary or danger
$this->options['style'] = $style;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,46 @@ public function __construct()
/**
* @return $this
*/
public function text(string $text, bool $markdown = true): static
public function text(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): static
{
$this->options['text'] = [
'type' => $markdown ? 'mrkdwn' : 'plain_text',
'text' => $text,
];

// verbatim is only available for markdown
if ($markdown) {
$this->options['text']['verbatim'] = $verbatim;
} else {
$this->options['text']['emoji'] = $emoji;
}
Comment on lines +35 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's 3 correlated options, that makes it hard to reason about them.
Would it make sense to add only one option, either verbatim or emoji and send the appropriate setting to slack automatically?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well in term of behavior on Slack, verbatim == !emoji) (except verbatim is for auto-URL parsing, emoji for emoji escaping).
So maybe we can keep only verbatim option then having the following code if it's what you have in mind ? :)

        if ($markdown) {
            $this->options['text']['verbatim'] = $verbatim;
        } else {
            $this->options['text']['emoji'] = !$verbatim
        }

But what about SlackContextBlock already having these two options and same condition ?
Removing emoji would be a BC (or we need to deprecate it ?), but keeping only emoji has less sense that verbatim in the meaning of the option in Slack :/


return $this;
}

/**
* @return $this
*/
public function field(string $text, bool $markdown = true): static
public function field(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): static
{
if (10 === \count($this->options['fields'] ?? [])) {
throw new \LogicException('Maximum number of fields should not exceed 10.');
}

$this->options['fields'][] = [
$field = [
'type' => $markdown ? 'mrkdwn' : 'plain_text',
'text' => $text,
];

// verbatim is only available for markdown
if ($markdown) {
$field['verbatim'] = $verbatim;
} else {
$field['emoji'] = $emoji;
}

$this->options['fields'][] = $field;

return $this;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

7.2
---

* Add `SlackButtonBlockElement` to add button as accessory to a section block
* Add `emoji` and `verbatim` options to `text` and `field` methods in `SlackSectionBlock`

6.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@ public function testCanBeInstantiated()
$section = new SlackSectionBlock();
$section->text('section text');
$section->field('section field');
$section->field('section field with verbatim', true, true, false);
$section->field('section field in plain text with verbatim', false, true, true);
$section->accessory(new SlackImageBlockElement('https://example.com/image.jpg', 'an image'));

$this->assertSame([
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'section text',
'verbatim' => false,
],
'fields' => [
[
'type' => 'mrkdwn',
'text' => 'section field',
'verbatim' => false,
],
[
'type' => 'mrkdwn',
'text' => 'section field with verbatim',
'verbatim' => false,
],
[
'type' => 'plain_text',
'text' => 'section field in plain text with verbatim',
'emoji' => true,
],
],
'accessory' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public static function fromNotificationProvider(): iterable
'text' => [
'type' => 'mrkdwn',
'text' => $subject,
'verbatim' => false,
],
],
],
Expand All @@ -162,13 +163,15 @@ public static function fromNotificationProvider(): iterable
'text' => [
'type' => 'mrkdwn',
'text' => $subject,
'verbatim' => false,
],
],
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $content,
'verbatim' => false,
],
],
],
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy