|
4 | 4 | description: 提取快递短信取件码 |
5 | 5 | icon: message |
6 | 6 | category: 工具 |
7 | | - version: 1.0.2 |
| 7 | + version: 1.0.3 |
8 | 8 | */ |
9 | 9 |
|
10 | 10 | function main() { |
11 | | - const messages = sms.read(20); |
12 | | - if (!messages || messages.length === 0) { |
13 | | - console.log("未读取到短信"); |
14 | | - return; |
15 | | - } |
16 | | - |
17 | | - const patterns = [ |
18 | | - { |
19 | | - regex: /(?:【丰巢】|\[丰巢\]).*?取件码\s*(\d+).*?至\s*(.+?)(?:取件|$)/, |
20 | | - codeIndex: 1, |
21 | | - locationIndex: 2 |
22 | | - }, |
23 | | - { |
24 | | - regex: /(?:【(.*?)】|\[(.*?)\]).*?(?:已到|至)\s*(.+?)(?:,|。|、|请).*?凭\s*([A-Za-z0-9-]+?)取件/, |
25 | | - codeIndex: 4, |
26 | | - locationIndex: 3 |
| 11 | + try { |
| 12 | + const messages = sms.read(20); |
| 13 | + if (!messages || messages.length === 0) { |
| 14 | + console.log("未读取到短信"); |
| 15 | + return "未读取到短信"; |
27 | 16 | } |
28 | | - ]; |
29 | 17 |
|
30 | | - const upcomingReminders = reminder.getUpcoming(7) || []; |
| 18 | + const patterns = [ |
| 19 | + { |
| 20 | + // 丰巢类 |
| 21 | + regex: /(?:【丰巢】|\[丰巢\]).*?取件码\s*(\d+).*?至\s*(.+?)(?:取件|$)/, |
| 22 | + codeIndex: 1, |
| 23 | + locationIndex: 2 |
| 24 | + }, |
| 25 | + { |
| 26 | + // 通用快递类:支持换行、支持"凭xx取件"分两行 |
| 27 | + regex: /(?:【(.*?)】|\[(.*?)\]).*?(?:已到|到达|至)\s*(.+?)(?:,|。|、|请)[\s\S]*?(?:凭|取件码|提取码)\s*([A-Za-z0-9-]+?)\s*(?:取件|$)/, |
| 28 | + codeIndex: 4, |
| 29 | + locationIndex: 3 |
| 30 | + } |
| 31 | + ]; |
31 | 32 |
|
32 | | - const STORAGE_KEY = 'sms_processed_codes'; |
33 | | - let processedCodes = storage.get(STORAGE_KEY) || []; |
| 33 | + const upcomingReminders = reminder.getUpcoming(7) || []; |
34 | 34 |
|
35 | | - let processedCount = 0; |
36 | | - let hasNew = false; |
| 35 | + // 获取"取件码"列表的 ID(如果存在) |
| 36 | + const lists = reminder.getLists() || []; |
| 37 | + const pickupList = lists.find(list => list.title === "取件码"); |
| 38 | + const pickupListId = pickupList ? pickupList.id : null; |
37 | 39 |
|
38 | | - for (let i = 0; i < messages.length; i++) { |
39 | | - const msg = messages[i]; |
40 | | - let match = null; |
41 | | - let code = null; |
42 | | - let location = null; |
| 40 | + const STORAGE_KEY = 'sms_processed_codes'; |
| 41 | + let processedCodes = storage.get(STORAGE_KEY) || []; |
43 | 42 |
|
44 | | - for (const pattern of patterns) { |
45 | | - match = msg.text.match(pattern.regex); |
46 | | - if (match) { |
47 | | - code = match[pattern.codeIndex]; |
48 | | - location = match[pattern.locationIndex]; |
49 | | - break; |
50 | | - } |
51 | | - } |
| 43 | + let processedCount = 0; |
| 44 | + let hasNew = false; |
52 | 45 |
|
53 | | - if (match && code && location) { |
54 | | - // 唯一标识 (仅使用取件码,防止位置描述不同导致重复) |
55 | | - const uniqueKey = code; |
| 46 | + for (let i = 0; i < messages.length; i++) { |
| 47 | + const msg = messages[i]; |
56 | 48 |
|
57 | | - // 查重逻辑 1: 检查持久化缓存 (以及本次运行已处理的) |
58 | | - // 兼容旧的缓存格式 "code|location" |
59 | | - if (processedCodes.some(item => item === uniqueKey || item.startsWith(`${code}|`))) { |
60 | | - console.log(`跳过已缓存的取件码: ${code}`); |
61 | | - continue; |
62 | | - } |
| 49 | + // 统一处理文本:把换行、多个空白压成一个空格,增强正则稳定性 |
| 50 | + const rawText = msg.text || msg.body || ""; |
| 51 | + const text = rawText.replace(/\s+/g, " ").trim(); |
| 52 | + |
| 53 | + let match = null; |
| 54 | + let code = null; |
| 55 | + let location = null; |
63 | 56 |
|
64 | | - const isDuplicateReminder = upcomingReminders.some(r => r.title.includes(code)); |
65 | | - if (isDuplicateReminder) { |
66 | | - console.log(`跳过现有提醒事项: ${code}`); |
67 | | - // 即使缓存没有,如果有提醒了,也加入缓存 |
68 | | - if (!processedCodes.includes(uniqueKey)) { |
69 | | - processedCodes.push(uniqueKey); |
70 | | - hasNew = true; |
| 57 | + for (const pattern of patterns) { |
| 58 | + match = text.match(pattern.regex); |
| 59 | + if (match) { |
| 60 | + code = match[pattern.codeIndex]; |
| 61 | + location = match[pattern.locationIndex]; |
| 62 | + break; |
71 | 63 | } |
72 | | - continue; |
73 | 64 | } |
74 | 65 |
|
75 | | - reminder.createSystemReminder(`取件码: ${code}`, { |
76 | | - notes: `位置: ${location}\n原文: ${msg.text || msg.body}`, |
77 | | - priority: 5, |
78 | | - listTitle: "取件码" |
79 | | - }); |
| 66 | + if (match && code && location) { |
| 67 | + code = String(code).trim(); |
| 68 | + location = String(location).trim(); |
80 | 69 |
|
81 | | - notification.send("快递取件提醒", `凭取件码 ${code} 至 ${location} 取件`, {}); |
| 70 | + // 唯一标识:仅使用取件码,避免位置变化导致重复 |
| 71 | + const uniqueKey = code; |
82 | 72 |
|
83 | | - console.log(`已提取取件码: ${code} (${location})`); |
| 73 | + // 查重逻辑:检查缓存 / 兼容旧格式 code|location |
| 74 | + if (processedCodes.some(item => item === uniqueKey || (typeof item === 'string' && item.startsWith(`${code}|`)))) { |
| 75 | + console.log(`跳过已缓存的取件码: ${code}`); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + const isDuplicateReminder = upcomingReminders.some(r => { |
| 80 | + const title = r && r.title ? String(r.title) : ""; |
| 81 | + return title.includes(code); |
| 82 | + }); |
| 83 | + |
| 84 | + if (isDuplicateReminder) { |
| 85 | + console.log(`跳过现有提醒事项: ${code}`); |
| 86 | + if (!processedCodes.includes(uniqueKey)) { |
| 87 | + processedCodes.push(uniqueKey); |
| 88 | + storage.set(STORAGE_KEY, processedCodes); |
| 89 | + hasNew = true; |
| 90 | + } |
| 91 | + continue; |
| 92 | + } |
84 | 93 |
|
85 | | - // 更新缓存 |
86 | | - processedCodes.push(uniqueKey); |
87 | | - storage.set(STORAGE_KEY, processedCodes); |
88 | | - hasNew = true; |
89 | | - processedCount++; |
| 94 | + // 创建提醒:使用 listId(如果找到"取件码"列表) |
| 95 | + const reminderOptions = { |
| 96 | + notes: `位置: ${location}\n原文: ${rawText}`, |
| 97 | + priority: 5 |
| 98 | + }; |
| 99 | + if (pickupListId) { |
| 100 | + reminderOptions.listId = pickupListId; |
| 101 | + } |
| 102 | + |
| 103 | + const reminderResult = reminder.create(`取件码: ${code}`, reminderOptions); |
| 104 | + |
| 105 | + const notifyResult = notification.send( |
| 106 | + "快递取件提醒", |
| 107 | + `凭取件码 ${code} 至 ${location} 取件`, |
| 108 | + {} |
| 109 | + ); |
| 110 | + |
| 111 | + console.log(`已提取取件码: ${code} (${location})`); |
| 112 | + console.log("提醒创建结果:", reminderResult); |
| 113 | + console.log("通知发送结果:", notifyResult); |
| 114 | + |
| 115 | + // 更新缓存 |
| 116 | + processedCodes.push(uniqueKey); |
| 117 | + storage.set(STORAGE_KEY, processedCodes); |
| 118 | + hasNew = true; |
| 119 | + processedCount++; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (processedCount === 0) { |
| 124 | + console.log("未找到新的取件码短信"); |
| 125 | + return "未找到新的取件码短信"; |
90 | 126 | } |
91 | | - } |
92 | 127 |
|
93 | | - if (processedCount === 0) { |
94 | | - console.log("未找到新的取件码短信"); |
| 128 | + return `处理完成:${processedCount} 条新取件码`; |
| 129 | + } catch (error) { |
| 130 | + console.error("脚本执行失败:", error); |
| 131 | + return `脚本执行失败:${error}`; |
95 | 132 | } |
96 | 133 | } |
97 | 134 |
|
98 | | -main(); |
| 135 | +return main(); |
0 commit comments