-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
196 lines (178 loc) · 6.87 KB
/
Plugin.php
File metadata and controls
196 lines (178 loc) · 6.87 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* 企业微信评论通知插件 - 修正时间版本
*
* @package WeComCommentNotify
* @author 桦哲
* @version 1.1.0
* @link https://web.zyhmifan.top/
*/
class WeComCommentNotify_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件
*/
public static function activate()
{
// 设置默认时区(如果服务器未正确配置)
if (function_exists('date_default_timezone_set') && !ini_get('date.timezone')) {
date_default_timezone_set('Asia/Shanghai');
}
Typecho_Plugin::factory('Widget_Feedback')->comment = array(__CLASS__, 'sendWeComNotify');
Typecho_Plugin::factory('Widget_Feedback')->trackback = array(__CLASS__, 'sendWeComNotify');
return _t('企业微信评论通知插件已激活,时间问题已修正');
}
/**
* 禁用插件
*/
public static function deactivate()
{
return _t('企业微信评论通知插件已禁用');
}
/**
* 插件配置面板
*
* @param Typecho_Widget_Helper_Form $form
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
// Webhook地址
$webhook = new Typecho_Widget_Helper_Form_Element_Text(
'webhook',
NULL,
NULL,
_t('企业微信机器人Webhook地址'),
_t('格式:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx')
);
$webhook->addRule('required', _t('Webhook地址不能为空'));
$webhook->addRule('url', _t('请输入有效的URL地址'));
$form->addInput($webhook);
// 时区设置
$timezone = new Typecho_Widget_Helper_Form_Element_Select(
'timezone',
array(
'Asia/Shanghai' => '中国标准时间 (UTC+8)',
'Asia/Tokyo' => '日本时间 (UTC+9)',
'America/New_York' => '美国东部时间 (UTC-5)',
'Europe/London' => '伦敦时间 (UTC+0)'
),
'Asia/Shanghai',
_t('时区设置'),
_t('请选择您的服务器所在时区')
);
$form->addInput($timezone);
// @成员设置
$mentioned_list = new Typecho_Widget_Helper_Form_Element_Text(
'mentioned_list',
NULL,
NULL,
_t('需要@的成员ID'),
_t('多个成员用英文逗号分隔,如:zhangsan,lisi')
);
$form->addInput($mentioned_list);
// @手机号设置
$mentioned_mobile_list = new Typecho_Widget_Helper_Form_Element_Text(
'mentioned_mobile_list',
NULL,
NULL,
_t('需要@的手机号'),
_t('多个手机号用英文逗号分隔,如:13800138000,13900139000')
);
$form->addInput($mentioned_mobile_list);
// 消息格式选择
$msg_type = new Typecho_Widget_Helper_Form_Element_Radio(
'msg_type',
array(
'text' => '纯文本',
'markdown' => 'Markdown格式'
),
'text',
_t('消息格式'),
_t('选择发送到企业微信的消息格式')
);
$form->addInput($msg_type);
}
/**
* 个人用户的配置面板
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 发送企业微信通知
*/
public static function sendWeComNotify($comment, $post)
{
$options = Typecho_Widget::widget('Widget_Options');
$pluginOptions = $options->plugin('WeComCommentNotify');
// 获取配置
$webhook = $pluginOptions->webhook;
$timezone = $pluginOptions->timezone;
$msgType = $pluginOptions->msg_type ?: 'text';
// 处理时间显示
try {
$dt = new DateTime('@' . $comment['created']);
$dt->setTimezone(new DateTimeZone($timezone));
$formattedTime = $dt->format('Y-m-d H:i:s');
} catch (Exception $e) {
$formattedTime = date('Y-m-d H:i:s', $comment['created']);
}
// 构建@信息
$at = array();
if (!empty($pluginOptions->mentioned_list)) {
$at['mentioned_list'] = array_map('trim',
explode(',', str_replace(',', ',', $pluginOptions->mentioned_list)));
}
if (!empty($pluginOptions->mentioned_mobile_list)) {
$at['mentioned_mobile_list'] = array_map('trim',
explode(',', str_replace(',', ',', $pluginOptions->mentioned_mobile_list)));
}
// 根据消息类型构建不同内容
if ($msgType === 'markdown') {
$data = array(
'msgtype' => 'markdown',
'markdown' => array(
'content' => "### 博客有新评论\n\n" .
"**评论人**: {$comment['author']}\n\n" .
"**评论文章**: [{$post->title}]({$post->permalink})\n\n" .
"**评论内容**: \n> {$comment['text']}\n\n" .
"**评论时间**: {$formattedTime}"
),
'at' => $at
);
} else {
$data = array(
'msgtype' => 'text',
'text' => array(
'content' => "博客有新评论\n\n" .
"评论人: {$comment['author']}\n\n" .
"评论文章: {$post->title}\n" .
"文章链接: {$post->permalink}\n\n" .
"评论内容: \n{$comment['text']}\n\n" .
"评论时间: {$formattedTime}"
),
'at' => $at
);
}
// 发送请求
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $webhook,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 记录错误日志(可选)
if ($httpCode != 200) {
file_put_contents(__DIR__ . '/notify_error.log',
date('Y-m-d H:i:s') . " - HTTP {$httpCode} - Response: {$response}\n",
FILE_APPEND);
}
return $comment;
}
}