Skip to content

Commit

Permalink
wip WeWorkGroupBot
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Jan 23, 2024
1 parent c51766e commit 98753b8
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/WeWorkGroupBot/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\WeWorkGroupBot;

class Client extends \Guanguans\Notify\Foundation\Client
{
}
19 changes: 19 additions & 0 deletions src/WeWorkGroupBot/Credential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\WeWorkGroupBot;

use Guanguans\Notify\Foundation\UriTemplateTokenCredential;

class Credential extends UriTemplateTokenCredential
{
}
50 changes: 50 additions & 0 deletions src/WeWorkGroupBot/Messages/ImageMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\WeWorkGroupBot\Messages;

use GuzzleHttp\RequestOptions;

class ImageMessage extends Message
{
/**
* @var string[]
*/
protected $defined = [
'imagePath',
];

public function __construct(string $imagePath)
{
parent::__construct([
'imagePath' => $imagePath,
]);
}

public function toHttpOptions(): array
{
return [
RequestOptions::JSON => [
'msgtype' => $this->type(),
$this->type() => [
'base64' => base64_file($this->options['imagePath']),
'md5' => md5_file($this->options['imagePath']),
],
],
];
}

protected function type(): string
{
return 'image';
}
}
35 changes: 35 additions & 0 deletions src/WeWorkGroupBot/Messages/MarkdownMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\WeWorkGroupBot\Messages;

class MarkdownMessage extends Message
{
/**
* @var string[]
*/
protected $defined = [
'content',
];

public function __construct(string $content)
{
parent::__construct([
'content' => $content,
]);
}

protected function type(): string
{
return 'markdown';
}
}
41 changes: 41 additions & 0 deletions src/WeWorkGroupBot/Messages/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\WeWorkGroupBot\Messages;

use Guanguans\Notify\Foundation\Concerns\AsJson;
use Guanguans\Notify\Foundation\Concerns\AsPost;
use Guanguans\Notify\WeWorkGroupBot\Credential;
use GuzzleHttp\RequestOptions;

abstract class Message extends \Guanguans\Notify\Foundation\Message
{
use AsPost;
use AsJson;

public function httpUri()
{
return sprintf('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s', Credential::TEMPLATE_TOKEN);
}

public function toHttpOptions(): array
{
return [
RequestOptions::JSON => [
'msgtype' => $this->type(),
$this->type() => $this->getOptions(),
],
];
}

abstract protected function type(): string;
}
93 changes: 93 additions & 0 deletions src/WeWorkGroupBot/Messages/NewsMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\WeWorkGroupBot\Messages;

use Symfony\Component\OptionsResolver\OptionsResolver;

class NewsMessage extends Message
{
/**
* @var string[]
*/
protected $defined = [
'articles',
];

/**
* @var array<string, string>
*/
protected $allowedTypes = [
'articles' => 'array',
];

/**
* @var array[]
*/
protected array $options = [
'articles' => [],
];

public function __construct(array $articles = [])
{
parent::__construct([
'articles' => $articles,
]);
}

protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver
{
return tap(parent::configureOptionsResolver($optionsResolver), static function (OptionsResolver $resolver): void {
$resolver->setNormalizer('articles', static function (OptionsResolver $optionsResolver, array $value): array {
return isset($value[0]) ? $value : [$value];
});
});
}

public function setArticles(array $articles): self
{
return $this->addArticles($articles);
}

public function addArticles(array $articles): self
{
foreach ($articles as $article) {
$this->addArticle($article);
}

return $this;
}

public function setArticle(array $article): self
{
return $this->addArticle($article);
}

public function addArticle(array $article): self
{
$this->options['articles'][] = configure_options($article, static function (OptionsResolver $optionsResolver): void {
$optionsResolver->setDefined([
'title',
'description',
'url',
'picurl',
]);
});

return $this;
}

protected function type(): string
{
return 'news';
}
}
82 changes: 82 additions & 0 deletions src/WeWorkGroupBot/Messages/TextMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\WeWorkGroupBot\Messages;

use Symfony\Component\OptionsResolver\OptionsResolver;

class TextMessage extends Message
{
/**
* @var string[]
*/
protected $defined = [
'content',
'mentioned_list',
'mentioned_mobile_list',
];

/**
* @var array<string, array<string>>
*/
protected $allowedTypes = [
'mentioned_list' => ['int', 'string', 'array'],
'mentioned_mobile_list' => ['int', 'string', 'array'],
];

protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver
{
return tap(parent::configureOptionsResolver($optionsResolver), static function (OptionsResolver $resolver): void {
$resolver->setNormalizer('mentioned_list', static function (OptionsResolver $optionsResolver, $value): array {
return (array) $value;
});
$resolver->setNormalizer('mentioned_mobile_list', static function (OptionsResolver $optionsResolver, $value): array {
return (array) $value;
});
});
}

/**
* @return $this
*/
public function setContent(string $content): self
{
$this->setOption('content', $content);

return $this;
}

/**
* @return $this
*/
public function setMentionedList(array $mentionedList): self
{
$this->setOption('mentioned_list', $mentionedList);

return $this;
}

/**
* @return $this
*/
public function setMentionedMobileList(array $mentionedMobileList): self
{
$this->setOption('mentioned_mobile_list', $mentionedMobileList);

return $this;
}

protected function type(): string
{
return 'text';
}
}

0 comments on commit 98753b8

Please sign in to comment.