-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
176 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters