Ding.php
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Lackoxygen\ExceptionPush\Agents;
use GuzzleHttp\Psr7\Request;
use Lackoxygen\ExceptionPush\Attribute\Attribute;
use Lackoxygen\ExceptionPush\Client;
use Lackoxygen\ExceptionPush\Contracts\AgentInterface;
class Ding implements AgentInterface
{
use Attribute;
public string $token = '';
public string $secret = '';
public bool $enable = true;
public function report(array $content)
{
/**
* @var \GuzzleHttp\Client $client
*/
$client = Client::new('https://oapi.dingtalk.com');
$uri = sprintf('robot/send?access_token=%s', $this->token);
if (!empty($this->secret)) {
$timestamp = time() . sprintf('%03d', rand(1, 999));
$sign = hash_hmac('sha256', $timestamp . "\n" . $this->secret, $this->secret, true);
$uri .= '×tamp=' . $timestamp;
$uri .= '&sign=' . base64_encode($sign);
}
$request = new Request(
'POST',
$uri,
[
'Content-Type' => 'application/json;charset=utf-8'
],
$this->body($content),
'1.1'
);
$client->send($request);
}
/**
* @param array $content
* @return string
*/
protected function body(array $content): string
{
return json_encode([
'msgtype' => 'text',
'text' => [
'content' => join("\n", $content)
],
'isAtAll' => false
]);
}
/**
* @return bool
*/
public function disabled(): bool
{
return !$this->enable;
}
}