koishi-plugin-stock 2.0.6 → 2.0.7
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 +4 -0
- package/lib/index.js +133 -114
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,6 +57,10 @@ npm install koishi-plugin-stock
|
|
|
57
57
|
|
|
58
58
|
## 更新日志
|
|
59
59
|
|
|
60
|
+
### v2.0.7
|
|
61
|
+
- 新增定时广播任务失败重试机制:当网络中断导致消息发送失败时,自动开启 3 次重试,每次间隔 1 分钟
|
|
62
|
+
- 引入任务时效性检查:重试过程中若达到该任务的下一个执行时间点,则自动取消当前重试,防止消息堆积
|
|
63
|
+
|
|
60
64
|
### v2.0.6
|
|
61
65
|
- 优化定时任务触发逻辑:改用 `Intl` API 获取确切的上海时区时间,解决部分环境下时区偏移导致的触发失败问题
|
|
62
66
|
- 增强时间格式容错:自动处理 `9:30` vs `09:30` 以及全角冒号等配置问题
|
package/lib/index.js
CHANGED
|
@@ -40,6 +40,7 @@ exports.Config = koishi_1.Schema.object({
|
|
|
40
40
|
});
|
|
41
41
|
function apply(ctx, config) {
|
|
42
42
|
const logger = ctx.logger('stock');
|
|
43
|
+
const retryQueue = [];
|
|
43
44
|
// 插件启动日志
|
|
44
45
|
logger.info('stock 插件已加载');
|
|
45
46
|
logger.info(`当前配置: broadcastTasks=${config.broadcastTasks?.length || 0} 个任务`);
|
|
@@ -48,167 +49,185 @@ function apply(ctx, config) {
|
|
|
48
49
|
logger.info(`任务 ${idx + 1}: times=${task.times}, type=${task.type}, targetIds=${task.targetIds}, content=${task.content}`);
|
|
49
50
|
});
|
|
50
51
|
}
|
|
52
|
+
// 核心广播执行函数
|
|
53
|
+
async function performBroadcast(task, isRetry = false) {
|
|
54
|
+
const label = isRetry ? '[重试广播]' : '[初始广播]';
|
|
55
|
+
try {
|
|
56
|
+
if (!task.targetIds || typeof task.targetIds !== 'string') {
|
|
57
|
+
logger.warn(`${label} 任务配置不完整: targetIds 字段无效`);
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const targetIds = task.targetIds.split(/[,,]/).map(id => id.trim()).filter(id => id);
|
|
61
|
+
if (targetIds.length === 0) {
|
|
62
|
+
logger.warn(`${label} 任务目标列表为空: ${task.content}`);
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
logger.info(`${label} 正在执行: ${task.content} -> ${targetIds.join(',')} (${task.type})`);
|
|
66
|
+
let message = '';
|
|
67
|
+
// 1. 获取内容
|
|
68
|
+
if (task.content === '活跃市值') {
|
|
69
|
+
try {
|
|
70
|
+
const responseText = await ctx.http.get('http://stock.svip886.com/api/indexes', { responseType: 'text' });
|
|
71
|
+
message = `📊 定时广播 - 指数看板:\n\n${responseText}`;
|
|
72
|
+
}
|
|
73
|
+
catch (apiErr) {
|
|
74
|
+
logger.error(`${label} 获取活跃市值 API 失败`, apiErr);
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else if (task.content === '涨停看板' || task.content === '跌停看板') {
|
|
79
|
+
try {
|
|
80
|
+
const apiType = task.content === '涨停看板' ? 'limit_up' : 'limit_down';
|
|
81
|
+
const imageUrl = `http://stock.svip886.com/api/${apiType}.png`;
|
|
82
|
+
const imageBuffer = await ctx.http.get(imageUrl, { responseType: 'arraybuffer' });
|
|
83
|
+
const base64Image = Buffer.from(imageBuffer).toString('base64');
|
|
84
|
+
message = `🔔 定时广播 - ${task.content}:\n<img src="data:image/png;base64,${base64Image}" />`;
|
|
85
|
+
}
|
|
86
|
+
catch (apiErr) {
|
|
87
|
+
logger.error(`${label} 获取${task.content}图片 API 失败`, apiErr);
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!message)
|
|
92
|
+
return false;
|
|
93
|
+
// 2. 发送消息
|
|
94
|
+
const bot = ctx.bots.find(b => b.status === 'online' || b.status === 1) || ctx.bots[0];
|
|
95
|
+
if (!bot) {
|
|
96
|
+
logger.error(`${label} 无可用机器人实例`);
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
let allSuccess = true;
|
|
100
|
+
for (const targetId of targetIds) {
|
|
101
|
+
try {
|
|
102
|
+
if (task.type === 'private') {
|
|
103
|
+
await bot.sendPrivateMessage(targetId, message);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
await bot.sendMessage(targetId, message);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
logger.error(`${label} 发送失败 (${targetId}): ${err.message}`);
|
|
111
|
+
allSuccess = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return allSuccess;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
logger.error(`${label} 执行过程发生严重错误`, error);
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// 获取任务的下一个执行时间点
|
|
122
|
+
function getNextScheduledTime(task, currentTime) {
|
|
123
|
+
const times = task.times.split(/[,,]/).map(s => {
|
|
124
|
+
let time = s.trim();
|
|
125
|
+
if (/^\d:\d{2}$/.test(time))
|
|
126
|
+
time = '0' + time;
|
|
127
|
+
return time;
|
|
128
|
+
}).sort();
|
|
129
|
+
const currentIndex = times.indexOf(currentTime);
|
|
130
|
+
if (currentIndex === -1)
|
|
131
|
+
return times[0]; // 如果当前时间不在列表里,假设下一个是第一个
|
|
132
|
+
return times[(currentIndex + 1) % times.length];
|
|
133
|
+
}
|
|
51
134
|
// 定时任务逻辑
|
|
52
135
|
let lastCheckedMinute = '';
|
|
53
136
|
ctx.setInterval(async () => {
|
|
54
137
|
const now = new Date();
|
|
55
|
-
|
|
138
|
+
const nowTimestamp = now.getTime();
|
|
56
139
|
const chinaTimeStr = now.toLocaleTimeString('zh-CN', {
|
|
57
140
|
timeZone: 'Asia/Shanghai',
|
|
58
141
|
hour12: false,
|
|
59
142
|
hour: '2-digit',
|
|
60
143
|
minute: '2-digit'
|
|
61
144
|
});
|
|
62
|
-
// 确保格式为 HH:mm,并处理可能的中文冒号
|
|
63
145
|
const currentTime = chinaTimeStr.replace(':', ':');
|
|
64
|
-
// 每分钟的第一次执行时打印当前时间
|
|
65
146
|
if (config.enableDebugLog && currentTime !== lastCheckedMinute) {
|
|
66
147
|
logger.info(`[定时任务检查] 当前时间: ${currentTime} (上海时区)`);
|
|
67
148
|
}
|
|
149
|
+
// --- 1. 处理重试队列 ---
|
|
150
|
+
for (let i = retryQueue.length - 1; i >= 0; i--) {
|
|
151
|
+
const item = retryQueue[i];
|
|
152
|
+
// 检查是否达到1分钟间隔
|
|
153
|
+
if (nowTimestamp - item.lastAttemptTimestamp < 60000)
|
|
154
|
+
continue;
|
|
155
|
+
// 检查是否超过了下次执行时间
|
|
156
|
+
const nextTime = getNextScheduledTime(item.task, item.originalTime);
|
|
157
|
+
// 如果当前时间已经达到了该任务的下一个预定时间,或者已经跨天了,则取消重试
|
|
158
|
+
if (nextTime && (currentTime === nextTime || (currentTime > nextTime && item.originalTime < nextTime))) {
|
|
159
|
+
logger.warn(`[重试取消] 任务 ${item.task.content} (${item.originalTime}) 已过期或达到下个周期`);
|
|
160
|
+
retryQueue.splice(i, 1);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
item.retryCount++;
|
|
164
|
+
item.lastAttemptTimestamp = nowTimestamp;
|
|
165
|
+
logger.info(`[开始重试] 任务 ${item.task.content} (原定于 ${item.originalTime}), 第 ${item.retryCount}/3 次重试`);
|
|
166
|
+
const success = await performBroadcast(item.task, true);
|
|
167
|
+
if (success) {
|
|
168
|
+
logger.info(`[重试成功] 任务 ${item.task.content} 已成功补发`);
|
|
169
|
+
retryQueue.splice(i, 1);
|
|
170
|
+
}
|
|
171
|
+
else if (item.retryCount >= 3) {
|
|
172
|
+
logger.error(`[重试失败] 任务 ${item.task.content} 已达到最大重试次数,放弃`);
|
|
173
|
+
retryQueue.splice(i, 1);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// --- 2. 处理初始任务 ---
|
|
68
177
|
if (currentTime === lastCheckedMinute)
|
|
69
178
|
return;
|
|
70
179
|
if (!config.broadcastTasks || config.broadcastTasks.length === 0)
|
|
71
180
|
return;
|
|
72
|
-
// 检查当前时间是否有任务
|
|
73
181
|
const activeTasks = config.broadcastTasks.filter(t => {
|
|
74
|
-
if (!t.times || typeof t.times !== 'string')
|
|
75
|
-
logger.warn(`跳过配置不正确的任务: times 字段无效`);
|
|
182
|
+
if (!t.times || typeof t.times !== 'string')
|
|
76
183
|
return false;
|
|
77
|
-
}
|
|
78
|
-
// 支持中英文逗号,并自动补全 9:30 这种格式为 09:30
|
|
79
184
|
const times = t.times.split(/[,,]/).map(s => {
|
|
80
185
|
let time = s.trim();
|
|
81
186
|
if (/^\d:\d{2}$/.test(time))
|
|
82
187
|
time = '0' + time;
|
|
83
188
|
return time;
|
|
84
189
|
}).filter(s => s);
|
|
85
|
-
|
|
86
|
-
if (config.enableDebugLog && isMatch) {
|
|
87
|
-
logger.info(`[时间匹配成功] 当前时间 ${currentTime} 命中任务设定列表 [${times.join(',')}]`);
|
|
88
|
-
}
|
|
89
|
-
return isMatch;
|
|
190
|
+
return times.includes(currentTime);
|
|
90
191
|
});
|
|
91
|
-
if (activeTasks.length === 0)
|
|
192
|
+
if (activeTasks.length === 0) {
|
|
193
|
+
if (currentTime !== lastCheckedMinute)
|
|
194
|
+
lastCheckedMinute = currentTime;
|
|
92
195
|
return;
|
|
196
|
+
}
|
|
93
197
|
lastCheckedMinute = currentTime;
|
|
94
|
-
|
|
95
|
-
logger.info(`[任务触发] 检测到 ${activeTasks.length} 个待执行的广播任务 (时间: ${currentTime})`);
|
|
198
|
+
logger.info(`[任务触发] 命中 ${activeTasks.length} 个广播任务 (时间: ${currentTime})`);
|
|
96
199
|
try {
|
|
97
|
-
// 检查是否为交易日(基本周末检查 + 节假日API)
|
|
98
|
-
// 使用 Intl API 获取上海日期的组件,确保逻辑一致
|
|
99
|
-
const formatter = new Intl.DateTimeFormat('zh-CN', {
|
|
100
|
-
timeZone: 'Asia/Shanghai',
|
|
101
|
-
year: 'numeric',
|
|
102
|
-
month: '2-digit',
|
|
103
|
-
day: '2-digit',
|
|
104
|
-
weekday: 'narrow'
|
|
105
|
-
});
|
|
106
|
-
const parts = formatter.formatToParts(now);
|
|
107
|
-
const getValue = (type) => parts.find(p => p.type === type)?.value || '';
|
|
108
|
-
const year = getValue('year');
|
|
109
|
-
const month = getValue('month');
|
|
110
|
-
const day = getValue('day');
|
|
111
200
|
const shanghaiDate = new Date(now.toLocaleString('en-US', { timeZone: 'Asia/Shanghai' }));
|
|
112
201
|
const isWeekend = (shanghaiDate.getDay() === 0 || shanghaiDate.getDay() === 6);
|
|
202
|
+
const year = shanghaiDate.getFullYear();
|
|
203
|
+
const month = (shanghaiDate.getMonth() + 1).toString().padStart(2, '0');
|
|
204
|
+
const day = shanghaiDate.getDate().toString().padStart(2, '0');
|
|
113
205
|
const dateStr = `${year}-${month}-${day}`;
|
|
114
206
|
let tradingDay = !isWeekend;
|
|
115
207
|
try {
|
|
116
|
-
if (config.enableDebugLog)
|
|
117
|
-
logger.debug(`正在检查交易日状态: ${dateStr}`);
|
|
118
208
|
const holidayData = await ctx.http.get(`https://timor.tech/api/holiday/info/${dateStr}`);
|
|
119
|
-
// 增加对 HTML 响应的防御(Cloudflare 拦截时会返回 HTML 字符串)
|
|
120
209
|
if (holidayData && typeof holidayData === 'object' && holidayData.type) {
|
|
121
|
-
// type: 0 工作日, 1 周末, 2 节日, 3 调休
|
|
122
210
|
const typeCode = holidayData.type.type;
|
|
123
211
|
tradingDay = (typeCode === 0 || typeCode === 3);
|
|
124
|
-
logger.info(`节假日API返回类型: ${typeCode} (${holidayData.type.name}), 交易日状态: ${tradingDay}`);
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
if (config.enableDebugLog)
|
|
128
|
-
logger.warn(`节假日API返回了非预期的格式或被拦截,将回退到周末检查`);
|
|
129
212
|
}
|
|
130
213
|
}
|
|
131
214
|
catch (e) {
|
|
132
|
-
logger.warn(`节假日API
|
|
215
|
+
logger.warn(`节假日API请求失败: ${e.message}`);
|
|
133
216
|
}
|
|
134
217
|
if (!tradingDay) {
|
|
135
|
-
logger.info(`[定时任务跳过]
|
|
218
|
+
logger.info(`[定时任务跳过] ${dateStr} 非交易日`);
|
|
136
219
|
return;
|
|
137
220
|
}
|
|
138
221
|
for (const task of activeTasks) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
logger.info(`正在执行广播任务: ${task.content} -> ${targetIds.join(',')} (${task.type})`);
|
|
151
|
-
let message = '';
|
|
152
|
-
if (task.content === '活跃市值') {
|
|
153
|
-
try {
|
|
154
|
-
logger.info(`开始获取活跃市值数据...`);
|
|
155
|
-
const responseText = await ctx.http.get('http://stock.svip886.com/api/indexes', { responseType: 'text' });
|
|
156
|
-
message = `📊 定时广播 - 指数看板:\n\n${responseText}`;
|
|
157
|
-
logger.info(`活跃市值数据获取成功, 信息长度: ${message.length}`);
|
|
158
|
-
}
|
|
159
|
-
catch (apiErr) {
|
|
160
|
-
logger.error('获取活跃市值 API 失败', apiErr);
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
else if (task.content === '涨停看板' || task.content === '跌停看板') {
|
|
165
|
-
try {
|
|
166
|
-
const apiType = task.content === '涨停看板' ? 'limit_up' : 'limit_down';
|
|
167
|
-
const imageUrl = `http://stock.svip886.com/api/${apiType}.png`;
|
|
168
|
-
logger.info(`开始下载图片: ${imageUrl}`);
|
|
169
|
-
const imageBuffer = await ctx.http.get(imageUrl, { responseType: 'arraybuffer' });
|
|
170
|
-
logger.info(`图片下载成功, 大小: ${imageBuffer.byteLength} bytes`);
|
|
171
|
-
const base64Image = Buffer.from(imageBuffer).toString('base64');
|
|
172
|
-
message = `🔔 定时广播 - ${task.content}:\n<img src="data:image/png;base64,${base64Image}" />`;
|
|
173
|
-
logger.info(`图片base64编码成功, 信息长度: ${message.length}`);
|
|
174
|
-
}
|
|
175
|
-
catch (apiErr) {
|
|
176
|
-
logger.error(`获取${task.content}图片 API 失败`, apiErr);
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
logger.warn(`不支持的广播内容类型: ${task.content}`);
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
if (!message) {
|
|
185
|
-
logger.warn(`未能生成消息内容: ${task.content}`);
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
const bot = ctx.bots.find(b => b.status === 'online' || b.status === 1) || ctx.bots[0];
|
|
189
|
-
if (!bot) {
|
|
190
|
-
logger.error(`无可用机器人实例,总有 ${ctx.bots.length} 个机器人`);
|
|
191
|
-
continue;
|
|
192
|
-
}
|
|
193
|
-
logger.info(`找到机器人: ${bot.platform}, 开始向 ${targetIds.length} 个目标发送`);
|
|
194
|
-
for (const targetId of targetIds) {
|
|
195
|
-
try {
|
|
196
|
-
logger.info(`将发送给 ${targetId}, 类型: ${task.type}`);
|
|
197
|
-
if (task.type === 'private') {
|
|
198
|
-
await bot.sendPrivateMessage(targetId, message);
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
await bot.sendMessage(targetId, message);
|
|
202
|
-
}
|
|
203
|
-
logger.info(`广播任务发送成功: ${task.content} -> ${targetId}`);
|
|
204
|
-
}
|
|
205
|
-
catch (err) {
|
|
206
|
-
logger.error(`广播任务发送失败 (${targetId}): ${task.content}`, err);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
catch (error) {
|
|
211
|
-
logger.error(`定时广播任务执行失败: ${task.content}`, error);
|
|
222
|
+
const success = await performBroadcast(task);
|
|
223
|
+
if (!success) {
|
|
224
|
+
logger.warn(`[任务入队] 任务 ${task.content} 执行失败,加入重试队列`);
|
|
225
|
+
retryQueue.push({
|
|
226
|
+
task,
|
|
227
|
+
originalTime: currentTime,
|
|
228
|
+
retryCount: 0,
|
|
229
|
+
lastAttemptTimestamp: nowTimestamp
|
|
230
|
+
});
|
|
212
231
|
}
|
|
213
232
|
}
|
|
214
233
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-stock",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7",
|
|
4
4
|
"description": "A Koishi plugin that fetches stock data and provides market analysis, including active market cap, stock alerts, limit-up board, and stock selection features with configurable blacklists for each command.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|