koishi-plugin-wordpress-notifier 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,8 +45,13 @@ plugins:
45
45
  targets:
46
46
  - '2801323326' # 推送目标(群号或 QQ 号)
47
47
  enableAutoPush: true # 是否启用自动推送
48
+ enableUpdatePush: false # 是否启用文章更新推送
49
+ enableUserPush: false # 是否启用新用户注册推送
48
50
  mentionAll: false # 是否 @全体成员
49
51
  maxArticles: 5 # 每次最多推送的文章数量
52
+ # 可选:WordPress API 认证配置(用于访问受保护的端点,如 users 端点)
53
+ # username: 'your-wordpress-username'
54
+ # password: 'your-wordpress-password'
50
55
  ```
51
56
 
52
57
  ### 配置参数说明
@@ -61,6 +66,8 @@ plugins:
61
66
  | `enableUserPush` | boolean | false | 是否启用新用户注册推送 |
62
67
  | `mentionAll` | boolean | false | 是否在推送时 @全体成员 |
63
68
  | `maxArticles` | number | 5 | 每次最多推送的文章数量 |
69
+ | `username` | string | 可选 | WordPress 用户名(用于访问受保护的 API 端点,如 users 端点) |
70
+ | `password` | string | 可选 | WordPress 密码(用于访问受保护的 API 端点,如 users 端点) |
64
71
 
65
72
  ## 使用命令
66
73
 
package/lib/index.d.ts CHANGED
@@ -18,6 +18,8 @@ export interface Config {
18
18
  enableUserPush: boolean;
19
19
  mentionAll: boolean;
20
20
  maxArticles: number;
21
+ username?: string;
22
+ password?: string;
21
23
  }
22
24
  export interface WordPressPost {
23
25
  id: number;
package/lib/index.js CHANGED
@@ -59,7 +59,15 @@ function apply(ctx, config) {
59
59
  try {
60
60
  const url = `${config.wordpressUrl}/wp-json/wp/v2/posts?per_page=${config.maxArticles}&orderby=date&order=desc`;
61
61
  ctx.logger.info(`正在获取文章: ${url}`);
62
- const response = await ctx.http.get(url);
62
+ // 准备请求配置,添加认证头(如果配置了用户名和密码)
63
+ const requestConfig = {};
64
+ if (config.username && config.password) {
65
+ const auth = Buffer.from(`${config.username}:${config.password}`).toString('base64');
66
+ requestConfig.headers = {
67
+ Authorization: `Basic ${auth}`
68
+ };
69
+ }
70
+ const response = await ctx.http.get(url, requestConfig);
63
71
  ctx.logger.info(`成功获取 ${response.length} 篇文章`);
64
72
  return response;
65
73
  }
@@ -72,13 +80,21 @@ function apply(ctx, config) {
72
80
  try {
73
81
  const url = `${config.wordpressUrl}/wp-json/wp/v2/users?per_page=${config.maxArticles}&orderby=registered_date&order=desc`;
74
82
  ctx.logger.info(`正在获取用户: ${url}`);
75
- const response = await ctx.http.get(url);
83
+ // 准备请求配置,添加认证头(如果配置了用户名和密码)
84
+ const requestConfig = {};
85
+ if (config.username && config.password) {
86
+ const auth = Buffer.from(`${config.username}:${config.password}`).toString('base64');
87
+ requestConfig.headers = {
88
+ Authorization: `Basic ${auth}`
89
+ };
90
+ }
91
+ const response = await ctx.http.get(url, requestConfig);
76
92
  ctx.logger.info(`成功获取 ${response.length} 位用户`);
77
93
  return response;
78
94
  }
79
95
  catch (error) {
80
96
  ctx.logger.error(`获取 WordPress 用户失败: ${error}`);
81
- ctx.logger.error(`WordPress REST API 的 users 端点可能需要认证才能访问,当前版本暂不支持认证配置`);
97
+ ctx.logger.error(`WordPress REST API 的 users 端点需要认证才能访问,请在插件配置中添加 WordPress 用户名和密码`);
82
98
  // 返回空数组,确保插件继续运行
83
99
  return [];
84
100
  }
@@ -87,7 +103,15 @@ function apply(ctx, config) {
87
103
  try {
88
104
  const url = `${config.wordpressUrl}/wp-json/wp/v2/posts?per_page=${config.maxArticles}&orderby=modified&order=desc`;
89
105
  ctx.logger.info(`正在获取更新文章: ${url}`);
90
- const response = await ctx.http.get(url);
106
+ // 准备请求配置,添加认证头(如果配置了用户名和密码)
107
+ const requestConfig = {};
108
+ if (config.username && config.password) {
109
+ const auth = Buffer.from(`${config.username}:${config.password}`).toString('base64');
110
+ requestConfig.headers = {
111
+ Authorization: `Basic ${auth}`
112
+ };
113
+ }
114
+ const response = await ctx.http.get(url, requestConfig);
91
115
  ctx.logger.info(`成功获取 ${response.length} 篇更新文章`);
92
116
  return response;
93
117
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-wordpress-notifier",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "WordPress 文章自动推送到 QQ",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",