forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDingTalkBotService.php
67 lines (57 loc) · 2.32 KB
/
DingTalkBotService.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
<?php
/**
* @author stars_kim <stars_kim@163.com>
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
*/
require_once 'Service.php';
class DingTalkBotService extends Service
{
public static function create(){
static $instance ;
if (!$instance){
$instance = new DingTalkBotService();
}
return $instance;
}
public function __handler($active, $comment, $plugin)
{
try {
$isPushBlogger = $plugin->isPushBlogger;
if ($comment['authorId'] == 1 && $isPushBlogger == 1) return false;
$DingTalkWebhook = $plugin->DingTalkWebhook;
$DingTalkSecret = $plugin->DingTalkSecret;
if (empty($DingTalkWebhook) || empty($DingTalkSecret)) throw new \Exception('缺少钉钉机器人配置参数');
$title = $active->title;
$author = $comment['author'];
$link = $active->permalink;
$context = $comment['text'];
$template = '标题:' . $title . PHP_EOL
. '评论人:' . $author . " [{$comment['ip']}]" . PHP_EOL
. '评论内容:' . $context . PHP_EOL
. '链接:' . $link . '#comment-' . $comment['coid'];
$params = [
'msgtype' => 'text',
'text' => [
'content' => $template
]
];
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json;charset=utf-8',
'content' => json_encode($params)
]
]);
list($s1, $s2) = explode(' ', microtime());
$timestamp = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
$stringToSign = $timestamp . "\n" . $DingTalkSecret;
$signature = base64_encode(hash_hmac('sha256', $stringToSign, $DingTalkSecret, true));
$signature = utf8_encode(urlencode($signature));
$DingTalkWebhook .= "×tamp=$timestamp&sign=$signature";
$result = file_get_contents($DingTalkWebhook, null, $context);
self::logger(__CLASS__, '', $params, $result);
} catch (\Exception $exception) {
self::logger(__CLASS__, '', '', '', $exception->getMessage());
}
}
}