-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdingtalk.php
executable file
·103 lines (91 loc) · 2.27 KB
/
dingtalk.php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env php
<?php
/**
* 获取和检查URL
*
* @param $arg
* @param null $default
* @return mixed
*/
function getArgument($arg , $default = null)
{
$argv = $GLOBALS['_SERVER']['argv'];
if (in_array($arg , $argv)) {
return $argv[array_search($arg , $argv) + 1] ?? $default;
}
return $default;
}
/**
* 请求API,获取一言
*
* @param $apiUrl
* @return mixed
* @copyright ©Alex (System.out.println.org and use TLS). All rights reserved.
*/
function getHitokoto($apiUrl)
{
return json_decode(file_get_contents($apiUrl));
}
/**
* 生成数据
*
* @param $source
* @param $creator
* @param $sentence
* @return string
* @copyright ©Alex (System.out.println.org and use TLS). All rights reserved.
*/
function makeData($source , $creator , $sentence)
{
return json_encode([
'msgtype' => 'markdown' ,
'markdown' => [
'title' => '一言' ,
'text' => "**{$sentence}**
> {$creator}「{$source}」" ,
] ,
]);
}
/**
* 请求钉钉
*
* @param $uri
* @param $data
* @return mixed
* @copyright ©Alex (System.out.println.org and use TLS). All rights reserved.
*/
function postToDingtalk($uri , $data)
{
$ch = curl_init();
curl_setopt($ch , CURLOPT_URL , $uri);
curl_setopt($ch , CURLOPT_POST , 1);
curl_setopt($ch , CURLOPT_CONNECTTIMEOUT , 5);
curl_setopt($ch , CURLOPT_HTTPHEADER , ['Content-Type: application/json;charset=utf-8']);
curl_setopt($ch , CURLOPT_POSTFIELDS , $data);
curl_setopt($ch , CURLOPT_RETURNTRANSFER , true);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data);
}
// 钉钉机器人 URL
$robotUrl = getArgument('--robot-url');
if (! preg_match('/^https?:\/\//' , $robotUrl)) {
exit('--robot-url must be url');
}
// 一言API地址
$apiUrl = getArgument('--api-url' , 'https://v1.hitokoto.cn/#');
if (! preg_match('/^https?:\/\//' , $apiUrl)) {
exit('--api-url must be url');
}
// 请求
$hitokoto = getHitokoto($apiUrl);
// 构造 markdown 和发送到钉钉
$result = postToDingtalk(
$robotUrl ,
makeData($hitokoto->from , $hitokoto->creator , $hitokoto->hitokoto)
);
// 判断响应
if ($result->errcode !== 0) {
exit($result->errmsg . PHP_EOL);
}
exit('success');