mingyoung /
dingtalk
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the mingyoung/dingtalk. |
||
| 5 | * |
||
| 6 | * (c) 张铭阳 <[email protected]> |
||
| 7 | * |
||
| 8 | * This source file is subject to the MIT license that is bundled |
||
| 9 | * with this source code in the file LICENSE. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace EasyDingTalk; |
||
| 13 | |||
| 14 | use Overtrue\Http\Traits\HasHttpRequests; |
||
| 15 | |||
| 16 | class Robot |
||
| 17 | { |
||
| 18 | use HasHttpRequests; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * 机器人 AccessToken |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $accessToken; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * 加签 没有勾选,不用填写 |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $secret; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param string $accessToken |
||
| 36 | * @param string|null $secret |
||
| 37 | */ |
||
| 38 | public function __construct($accessToken, $secret = null) |
||
| 39 | { |
||
| 40 | $this->accessToken = $accessToken; |
||
| 41 | $this->secret = $secret; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $accessToken |
||
| 46 | * @param string|null $secret |
||
| 47 | * |
||
| 48 | * @return self |
||
| 49 | */ |
||
| 50 | public static function create($accessToken, $secret = null) |
||
| 51 | { |
||
| 52 | return new static($accessToken, $secret); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * 发送消息 |
||
| 57 | * |
||
| 58 | * @param array $message |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | * |
||
| 62 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 63 | */ |
||
| 64 | public function send($message) |
||
| 65 | { |
||
| 66 | $url = 'https://oapi.dingtalk.com/robot/send?access_token='.$this->accessToken; |
||
| 67 | |||
| 68 | if ($this->secret) { |
||
| 69 | $timestamp = time().'000'; |
||
| 70 | $url .= sprintf( |
||
| 71 | '&sign=%s×tamp=%s', |
||
| 72 | urlencode(base64_encode(hash_hmac('sha256', $timestamp."\n".$this->secret, $this->secret, true))), $timestamp |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | $response = $this->getHttpClient()->request( |
||
| 77 | 'POST', $url, ['json' => $message] |
||
| 78 | ); |
||
| 79 | |||
| 80 | return $this->castResponseToType($response); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 81 | } |
||
| 82 | } |
||
| 83 |