From b6904782bd8692c7637e401e12b1c79656f4889a Mon Sep 17 00:00:00 2001 From: yaozm Date: Tue, 30 Jan 2024 15:07:42 +0800 Subject: [PATCH] wip DingTalkGroupBot --- src/DingTalkGroupBot/Credential.php | 27 +++++--------- .../Messages/BtnsActionCardMessage.php | 28 -------------- .../Messages/FeedCardMessage.php | 37 ------------------- .../Messages/MarkdownMessage.php | 10 ----- src/DingTalkGroupBot/Messages/Message.php | 4 +- src/DingTalkGroupBot/Messages/TextMessage.php | 10 ----- 6 files changed, 12 insertions(+), 104 deletions(-) diff --git a/src/DingTalkGroupBot/Credential.php b/src/DingTalkGroupBot/Credential.php index fe641ff4..ed99aa55 100644 --- a/src/DingTalkGroupBot/Credential.php +++ b/src/DingTalkGroupBot/Credential.php @@ -12,33 +12,26 @@ namespace Guanguans\Notify\DingTalkGroupBot; -use Guanguans\Notify\Foundation\Credentials\NullCredential; +use Guanguans\Notify\Foundation\Credentials\AggregateCredential; +use Guanguans\Notify\Foundation\Credentials\QueryCredential; -class Credential extends NullCredential +class Credential extends AggregateCredential { - private string $token; - private ?string $secret; - public function __construct(string $token, ?string $secret = null) { - $this->secret = $secret; - $this->token = $token; - } + $credentials = [new QueryCredential('access_token', $token)]; - public function applyToOptions(array $options): array - { - $options['query']['access_token'] = $this->token; - - if ($this->secret) { + if ($secret) { [$microseconds, $seconds] = explode(' ', microtime()); - $options['query']['timestamp'] = $timestamp = sprintf('%d%d', $seconds, sprintf('%.3f', $microseconds) * 1000); - $options['query']['sign'] = $this->sign($this->secret, $timestamp); + + $credentials[] = new QueryCredential('timestamp', $timestamp = $seconds.sprintf('%.3f', $microseconds) * 1000); + $credentials[] = new QueryCredential('sign', $this->sign($secret, $timestamp)); } - return $options; + parent::__construct(...$credentials); } - protected function sign(string $secret, string $timestamp): string + private function sign(string $secret, string $timestamp): string { return urlencode(base64_encode(hash_hmac( 'sha256', diff --git a/src/DingTalkGroupBot/Messages/BtnsActionCardMessage.php b/src/DingTalkGroupBot/Messages/BtnsActionCardMessage.php index 4600db27..e30213db 100644 --- a/src/DingTalkGroupBot/Messages/BtnsActionCardMessage.php +++ b/src/DingTalkGroupBot/Messages/BtnsActionCardMessage.php @@ -41,34 +41,6 @@ class BtnsActionCardMessage extends Message 'btns' => [], ]; - public function __construct(array $options) - { - $btns = $options['btns'] ?? []; - unset($options['btns']); - parent::__construct($options); - - $this->addBtns($btns); - } - - public function setBtns(array $btns): self - { - return $this->addBtns($btns); - } - - public function addBtns(array $btns): self - { - foreach ($btns as $btn) { - $this->addBtn($btn); - } - - return $this; - } - - public function setBtn(array $btn): self - { - return $this->addBtn($btn); - } - public function addBtn(array $btn): self { $this->options['btns'][] = configure_options($btn, static function (OptionsResolver $optionsResolver): void { diff --git a/src/DingTalkGroupBot/Messages/FeedCardMessage.php b/src/DingTalkGroupBot/Messages/FeedCardMessage.php index a8508ea6..0c2a30b5 100644 --- a/src/DingTalkGroupBot/Messages/FeedCardMessage.php +++ b/src/DingTalkGroupBot/Messages/FeedCardMessage.php @@ -31,30 +31,6 @@ class FeedCardMessage extends Message 'links' => [], ]; - public function __construct(array $links = []) - { - parent::__construct(['links' => $links]); - } - - public function setLinks(array $Links): self - { - return $this->addLinks($Links); - } - - public function addLinks(array $Links): self - { - foreach ($Links as $Link) { - $this->addLink($Link); - } - - return $this; - } - - public function setLink(array $Link): self - { - return $this->addLink($Link); - } - public function addLink(array $Link): self { $this->options['links'][] = configure_options($Link, static function (OptionsResolver $optionsResolver): void { @@ -68,19 +44,6 @@ public function addLink(array $Link): self return $this; } - protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver - { - return tap( - parent::configureOptionsResolver($optionsResolver), - static function (OptionsResolver $resolver): void { - $resolver->setNormalizer( - 'links', - static fn (OptionsResolver $optionsResolver, array $value): array => isset($value[0]) ? $value : [$value] - ); - } - ); - } - protected function type(): string { return 'feedCard'; diff --git a/src/DingTalkGroupBot/Messages/MarkdownMessage.php b/src/DingTalkGroupBot/Messages/MarkdownMessage.php index 13a14be7..ae6d60f5 100644 --- a/src/DingTalkGroupBot/Messages/MarkdownMessage.php +++ b/src/DingTalkGroupBot/Messages/MarkdownMessage.php @@ -12,8 +12,6 @@ namespace Guanguans\Notify\DingTalkGroupBot\Messages; -use Symfony\Component\OptionsResolver\OptionsResolver; - /** * @method \Guanguans\Notify\DingTalkGroupBot\Messages\MarkdownMessage title($title) * @method \Guanguans\Notify\DingTalkGroupBot\Messages\MarkdownMessage text($text) @@ -37,14 +35,6 @@ class MarkdownMessage extends Message 'isAtAll' => 'bool', ]; - protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver - { - return tap(parent::configureOptionsResolver($optionsResolver), static function (OptionsResolver $resolver): void { - $resolver->setNormalizer('atMobiles', static fn (OptionsResolver $optionsResolver, $value): array => (array) $value); - $resolver->setNormalizer('atDingtalkIds', static fn (OptionsResolver $optionsResolver, $value): array => (array) $value); - }); - } - protected function type(): string { return 'markdown'; diff --git a/src/DingTalkGroupBot/Messages/Message.php b/src/DingTalkGroupBot/Messages/Message.php index 38a84ee0..4961c3ce 100644 --- a/src/DingTalkGroupBot/Messages/Message.php +++ b/src/DingTalkGroupBot/Messages/Message.php @@ -29,8 +29,8 @@ public function toHttpOptions(): array return [ RequestOptions::JSON => [ 'msgtype' => $this->type(), - $this->type() => $this->options, - 'at' => $this->options, + $this->type() => $this->getOptions(), + 'at' => $this->getOptions(), ], ]; } diff --git a/src/DingTalkGroupBot/Messages/TextMessage.php b/src/DingTalkGroupBot/Messages/TextMessage.php index 5853be6c..7b1d2415 100644 --- a/src/DingTalkGroupBot/Messages/TextMessage.php +++ b/src/DingTalkGroupBot/Messages/TextMessage.php @@ -12,8 +12,6 @@ namespace Guanguans\Notify\DingTalkGroupBot\Messages; -use Symfony\Component\OptionsResolver\OptionsResolver; - /** * @method \Guanguans\Notify\DingTalkGroupBot\Messages\TextMessage content($content) * @method \Guanguans\Notify\DingTalkGroupBot\Messages\TextMessage atMobiles($atMobiles) @@ -35,14 +33,6 @@ class TextMessage extends Message 'isAtAll' => 'bool', ]; - protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver - { - return tap(parent::configureOptionsResolver($optionsResolver), static function (OptionsResolver $resolver): void { - $resolver->setNormalizer('atMobiles', static fn (OptionsResolver $optionsResolver, $value): array => (array) $value); - $resolver->setNormalizer('atDingtalkIds', static fn (OptionsResolver $optionsResolver, $value): array => (array) $value); - }); - } - protected function type(): string { return 'text';