Skip to content

Commit

Permalink
wip DingTalkGroupBot
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Jan 30, 2024
1 parent 10d119b commit b690478
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 104 deletions.
27 changes: 10 additions & 17 deletions src/DingTalkGroupBot/Credential.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
28 changes: 0 additions & 28 deletions src/DingTalkGroupBot/Messages/BtnsActionCardMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 0 additions & 37 deletions src/DingTalkGroupBot/Messages/FeedCardMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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';
Expand Down
10 changes: 0 additions & 10 deletions src/DingTalkGroupBot/Messages/MarkdownMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/DingTalkGroupBot/Messages/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
];
}
Expand Down
10 changes: 0 additions & 10 deletions src/DingTalkGroupBot/Messages/TextMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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';
Expand Down

0 comments on commit b690478

Please sign in to comment.