Skip to content

Commit

Permalink
wip LarkGroupBot
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Jan 18, 2024
1 parent bcd3699 commit 247813d
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 41 deletions.
39 changes: 39 additions & 0 deletions src/Foundation/HttpMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

Expand All @@ -34,11 +36,48 @@ public function __construct(array $options = [])
$this->uriFactory = Psr17FactoryDiscovery::findUriFactory();
}

public function toRequest(): RequestInterface
{
$request = $this->requestFactory->createRequest($this->method(), $this->uri());
$protocolVersion = $this->protocolVersion();
if ($protocolVersion) {
$request->withProtocolVersion($protocolVersion);
}

$body = $this->body();
if ($body) {
$request = $request->withBody($this->body());
}

foreach ($this->headers() as $key => $value) {
$request = $request->withHeader($key, $value);
}

return $request;
}

public function toQuery(): string
{
return http_build_query($this->options);
}

protected function protocolVersion(): string
{
return '1.1';
}

protected function headers(): array
{
return [
'Content-Type' => 'application/json',
];
}

protected function body(): ?StreamInterface
{
return $this->streamFactory->createStream((string) $this);
}

abstract protected function method(): string;

abstract protected function uri(): string;
Expand Down
24 changes: 24 additions & 0 deletions src/LarkGroupBot/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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\LarkGroupBot;

use Guanguans\Notify\Foundation\HttpClient;
use Psr\Http\Client\ClientInterface;

class Client extends HttpClient
{
public function __construct(Credential $credential, ClientInterface $httpClient = null)
{
parent::__construct($credential, $httpClient);
}
}
61 changes: 61 additions & 0 deletions src/LarkGroupBot/Credential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\LarkGroupBot;

use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamFactoryInterface;

class Credential implements \Guanguans\Notify\Foundation\Contracts\Credential
{
private string $accessToken;
private ?string $secret;
private StreamFactoryInterface $streamFactory;

public function __construct(string $accessToken, ?string $secret = null)
{
$this->accessToken = $accessToken;
$this->secret = $secret;
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
}

/**
* @noinspection JsonEncodingApiUsageInspection
*/
public function applyToRequest(RequestInterface $request): RequestInterface
{
$request = $request->withUri(
$request->getUri()->withPath(str_replace('token', $this->accessToken, $request->getUri()->getPath()))
);

if ($this->secret) {
$body = [
'timestamp' => $timestamp = time(),
'sign' => $this->sign($this->secret, $timestamp),
] + json_decode($request->getBody()->getContents(), true);

$request = $request->withBody($this->streamFactory->createStream(json_encode($body)));
}

return $request;
}

private function sign(string $secret, int $timestamp): string
{
$key = sprintf("%s\n%s", $timestamp, $secret);

$hash = hash_hmac('sha256', '', $key, true);

return base64_encode($hash);
}
}
50 changes: 50 additions & 0 deletions src/LarkGroupBot/Messages/TextMessage.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\LarkGroupBot\Messages;

use Guanguans\Notify\Foundation\HttpMessage;

class TextMessage extends HttpMessage
{
protected string $type = 'text';

protected array $defined = [
'text',
];

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

/**
* @noinspection JsonEncodingApiUsageInspection
*/
public function __toString(): string
{
return json_encode([
'msg_type' => $this->type,
'content' => $this->options,
]);
}

protected function method(): string
{
return 'POST';
}

protected function uri(): string
{
return 'https://open.feishu.cn/open-apis/bot/v2/hook/token';
}
}
43 changes: 2 additions & 41 deletions src/XiZhi/Messages/SingleMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
namespace Guanguans\Notify\XiZhi\Messages;

use Guanguans\Notify\Foundation\HttpMessage;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;

class SingleMessage extends HttpMessage
{
Expand All @@ -31,50 +29,13 @@ public function __construct(string $title, ?string $content = null)
]);
}

public function toRequest(): RequestInterface
{
$request = $this->requestFactory->createRequest($this->method(), $this->uri());
$protocolVersion = $this->protocolVersion();
if ($protocolVersion) {
$request->withProtocolVersion($protocolVersion);
}

$body = $this->body();
if ($body) {
$request = $request->withBody($this->body());
}

foreach ($this->headers() as $key => $value) {
$request = $request->withHeader($key, $value);
}

return $request;
}

protected function protocolVersion(): string
{
return '1.1';
}

protected function uri(): string
{
return 'https://xizhi.qqoq.net/token.send';
}

protected function method(): string
{
return 'POST';
}

protected function headers(): array
{
return [
'Content-Type' => 'application/json',
];
}

protected function body(): ?StreamInterface
protected function uri(): string
{
return $this->streamFactory->createStream((string) $this);
return 'https://xizhi.qqoq.net/token.send';
}
}

0 comments on commit 247813d

Please sign in to comment.