From 10ac6706364bab68c70861f3117359db6174c8a6 Mon Sep 17 00:00:00 2001 From: yaozm Date: Tue, 23 Jan 2024 15:58:35 +0800 Subject: [PATCH] wip Slack --- src/Slack/Client.php | 17 ++++++ src/Slack/Credential.php | 19 +++++++ src/Slack/Messages/Message.php | 100 +++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/Slack/Client.php create mode 100644 src/Slack/Credential.php create mode 100644 src/Slack/Messages/Message.php diff --git a/src/Slack/Client.php b/src/Slack/Client.php new file mode 100644 index 00000000..1fc7d273 --- /dev/null +++ b/src/Slack/Client.php @@ -0,0 +1,17 @@ + + * + * This source file is subject to the MIT license that is bundled. + */ + +namespace Guanguans\Notify\Slack; + +class Client extends \Guanguans\Notify\Foundation\Client +{ +} diff --git a/src/Slack/Credential.php b/src/Slack/Credential.php new file mode 100644 index 00000000..7b130bc8 --- /dev/null +++ b/src/Slack/Credential.php @@ -0,0 +1,19 @@ + + * + * This source file is subject to the MIT license that is bundled. + */ + +namespace Guanguans\Notify\Slack; + +use Guanguans\Notify\Foundation\NullCredential; + +class Credential extends NullCredential +{ +} diff --git a/src/Slack/Messages/Message.php b/src/Slack/Messages/Message.php new file mode 100644 index 00000000..6bd73265 --- /dev/null +++ b/src/Slack/Messages/Message.php @@ -0,0 +1,100 @@ + + * + * This source file is subject to the MIT license that is bundled. + */ + +namespace Guanguans\Notify\Slack\Messages; + +use Guanguans\Notify\Foundation\Concerns\AsJson; +use Guanguans\Notify\Foundation\Concerns\AsPost; +use Symfony\Component\OptionsResolver\OptionsResolver; + +class Message extends \Guanguans\Notify\Foundation\Message +{ + use AsPost; + use AsJson; + + /** + * @var string[] + */ + protected $defined = [ + 'text', + 'channel', + 'username', + 'icon_emoji', + 'icon_url', + 'unfurl_links', + 'attachments', + ]; + + /** + * @var array + */ + protected array $options = [ + 'unfurl_links' => false, + 'attachments' => [], + ]; + + /** + * @var array + */ + protected $allowedTypes = [ + 'unfurl_links' => 'bool', + 'attachments' => 'array', + ]; + + protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver + { + return tap(parent::configureOptionsResolver($optionsResolver), static function (OptionsResolver $resolver): void { + $resolver->setNormalizer('attachments', static function (OptionsResolver $optionsResolver, array $value): array { + return isset($value[0]) ? $value : [$value]; + }); + }); + } + + public function setAttachments(array $attachments): self + { + return $this->addAttachments($attachments); + } + + public function addAttachments(array $attachments): self + { + foreach ($attachments as $attachment) { + $this->addAttachment($attachment); + } + + return $this; + } + + public function setAttachment(array $attachment): self + { + return $this->addAttachment($attachment); + } + + public function addAttachment(array $attachment): self + { + $this->options['attachments'][] = configure_options($attachment, static function (OptionsResolver $optionsResolver): void { + $optionsResolver->setDefined([ + 'fallback', + 'text', + 'pretext', + 'color', + 'fields', + ]); + }); + + return $this; + } + + public function httpUri() + { + return 'webhook_url'; + } +}