diff --git a/src/Zulip/Client.php b/src/Zulip/Client.php index 272301fd..de3362d7 100644 --- a/src/Zulip/Client.php +++ b/src/Zulip/Client.php @@ -12,4 +12,20 @@ namespace Guanguans\Notify\Zulip; -class Client extends \Guanguans\Notify\Foundation\Client {} +use Guanguans\Notify\Foundation\Contracts\Credential; + +/** + * @see https://chat.zulip.org/accounts/login + * @see https://zulip.com/api/send-message + */ +class Client extends \Guanguans\Notify\Foundation\Client +{ + public function __construct(?Credential $credential = null) + { + parent::__construct($credential); + + $this->setHttpOptions([ + 'base_uri' => 'https://chat.zulip.org', + ]); + } +} diff --git a/src/Zulip/Credential.php b/src/Zulip/Credential.php index 775768eb..3ffa7015 100644 --- a/src/Zulip/Credential.php +++ b/src/Zulip/Credential.php @@ -16,8 +16,8 @@ class Credential extends BasicAuthCredential { - public function __construct(string $email, string $token) + public function __construct(string $botEmail, string $botApiKey) { - parent::__construct($email, $token); + parent::__construct($botEmail, $botApiKey); } } diff --git a/src/Zulip/Messages/DirectMessage.php b/src/Zulip/Messages/DirectMessage.php new file mode 100644 index 00000000..4a972c50 --- /dev/null +++ b/src/Zulip/Messages/DirectMessage.php @@ -0,0 +1,49 @@ + + * + * This source file is subject to the MIT license that is bundled. + */ + +namespace Guanguans\Notify\Zulip\Messages; + +/** + * @method \Guanguans\Notify\Zulip\Messages\DirectMessage type($type) + * @method \Guanguans\Notify\Zulip\Messages\DirectMessage to($to) + * @method \Guanguans\Notify\Zulip\Messages\DirectMessage content($content) + */ +class DirectMessage extends Message +{ + protected array $defined = [ + 'type', + 'to', + 'content', + ]; + + protected array $required = [ + 'type', + 'to', + 'content', + ]; + + protected array $defaults = [ + 'type' => 'direct', + ]; + + protected array $allowedTypes = [ + 'to' => ['string', 'int', 'array'], + ]; + + protected array $allowedValues = [ + 'type' => ['direct'], + ]; + + protected array $options = [ + 'type' => 'direct', + ]; +} diff --git a/src/Zulip/Messages/Message.php b/src/Zulip/Messages/Message.php index 58c66b96..1240c420 100644 --- a/src/Zulip/Messages/Message.php +++ b/src/Zulip/Messages/Message.php @@ -12,16 +12,16 @@ namespace Guanguans\Notify\Zulip\Messages; -use Guanguans\Notify\Foundation\Concerns\AsJson; +use Guanguans\Notify\Foundation\Concerns\AsFormParams; use Guanguans\Notify\Foundation\Concerns\AsPost; class Message extends \Guanguans\Notify\Foundation\Message { - use AsJson; + use AsFormParams; use AsPost; - public function toHttpUri() + public function toHttpUri(): string { - return '%s/api/v1/messages'; + return 'api/v1/messages'; } } diff --git a/src/Zulip/Messages/PrivateMessage.php b/src/Zulip/Messages/PrivateMessage.php deleted file mode 100644 index e2c70ba1..00000000 --- a/src/Zulip/Messages/PrivateMessage.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled. - */ - -namespace Guanguans\Notify\Zulip\Messages; - -/** - * @method \Guanguans\Notify\Zulip\Messages\PrivateMessage type($type) - * @method \Guanguans\Notify\Zulip\Messages\PrivateMessage to($to) - * @method \Guanguans\Notify\Zulip\Messages\PrivateMessage content($content) - */ -class PrivateMessage extends Message -{ - /** - * @var array - */ - protected array $defined = [ - 'type', - 'to', - 'content', - ]; - - /** - * @var array - */ - protected array $required = [ - 'type', - 'to', - 'content', - ]; - - /** - * @var array - */ - protected array $defaults = [ - 'type' => 'private', - ]; - - /** - * @var array> - */ - protected array $allowedValues = [ - 'type' => ['private'], - ]; - - public function __construct(string $to, string $content) - { - parent::__construct([ - 'to' => $to, - 'content' => $content, - ]); - } -} diff --git a/src/Zulip/Messages/StreamMessage.php b/src/Zulip/Messages/StreamMessage.php index 76584a7c..1cd2cdd3 100644 --- a/src/Zulip/Messages/StreamMessage.php +++ b/src/Zulip/Messages/StreamMessage.php @@ -22,9 +22,6 @@ */ class StreamMessage extends Message { - /** - * @var array - */ protected array $defined = [ 'type', 'to', @@ -34,9 +31,6 @@ class StreamMessage extends Message 'local_id', ]; - /** - * @var array - */ protected array $required = [ 'type', 'to', @@ -45,23 +39,17 @@ class StreamMessage extends Message ]; protected array $allowedTypes = [ - 'to' => ['string', 'int'], + 'to' => ['string', 'int', 'array'], 'content' => 'string', 'topic' => 'string', 'queue_id' => 'string', 'local_id' => 'string', ]; - /** - * @var array - */ protected array $defaults = [ 'type' => 'stream', ]; - /** - * @var array> - */ protected array $allowedValues = [ 'type' => ['stream'], ];