foliko 1.0.16 → 1.0.18
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/package.json +1 -1
- package/plugins/weixin-plugin.js +56 -0
package/package.json
CHANGED
package/plugins/weixin-plugin.js
CHANGED
|
@@ -48,6 +48,12 @@ module.exports = function(Plugin) {
|
|
|
48
48
|
// 获取 SessionPlugin 引用
|
|
49
49
|
this._sessionPlugin = framework.pluginManager.get('session')
|
|
50
50
|
|
|
51
|
+
// 监听定时提醒事件(放在这里,即使登录失败也要能接收事件)
|
|
52
|
+
framework.on('scheduler:reminder', async (data) => {
|
|
53
|
+
console.log('[WeChat] Received scheduler reminder:', data)
|
|
54
|
+
await this._handleScheduledReminder(data)
|
|
55
|
+
})
|
|
56
|
+
|
|
51
57
|
// 异步初始化 Bot
|
|
52
58
|
this._initBotAsync().catch(err => {
|
|
53
59
|
console.error('[WeChat] Failed to initialize bot:', err.message)
|
|
@@ -101,6 +107,56 @@ module.exports = function(Plugin) {
|
|
|
101
107
|
console.log('[WeChat] 开始接收微信消息')
|
|
102
108
|
}
|
|
103
109
|
|
|
110
|
+
/**
|
|
111
|
+
* 处理定时提醒
|
|
112
|
+
*/
|
|
113
|
+
async _handleScheduledReminder(data) {
|
|
114
|
+
// 检查 bot 是否可用
|
|
115
|
+
if (!this._bot) {
|
|
116
|
+
console.log('[WeChat] Bot not connected, cannot send reminder')
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const { taskName, message, sessionId } = data
|
|
121
|
+
|
|
122
|
+
// 构建提醒消息
|
|
123
|
+
const reminderText = `🔔 [${taskName}]\n\n${message}`
|
|
124
|
+
|
|
125
|
+
// 如果有 sessionId 是 weixin 类型的,发送到对应用户
|
|
126
|
+
if (sessionId && sessionId.startsWith('weixin_')) {
|
|
127
|
+
const userId = sessionId.replace('weixin_', '')
|
|
128
|
+
try {
|
|
129
|
+
await this._bot.send(userId, reminderText)
|
|
130
|
+
console.log(`[WeChat] Reminder sent to user ${userId}`)
|
|
131
|
+
} catch (err) {
|
|
132
|
+
console.error(`[WeChat] Failed to send reminder:`, err.message)
|
|
133
|
+
}
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 其他情况(包括 null 或其他 sessionId),发送到最近的 Weixin 会话
|
|
138
|
+
if (this._sessionPlugin) {
|
|
139
|
+
const allSessions = this._sessionPlugin.listSessions()
|
|
140
|
+
const weixinSessions = allSessions
|
|
141
|
+
.filter(s => s.id.startsWith('weixin_'))
|
|
142
|
+
.sort((a, b) => new Date(b.lastActive).getTime() - new Date(a.lastActive).getTime())
|
|
143
|
+
|
|
144
|
+
if (weixinSessions.length > 0) {
|
|
145
|
+
const userId = weixinSessions[0].id.replace('weixin_', '')
|
|
146
|
+
try {
|
|
147
|
+
await this._bot.send(userId, reminderText)
|
|
148
|
+
console.log(`[WeChat] Reminder sent to user ${userId}`)
|
|
149
|
+
} catch (err) {
|
|
150
|
+
console.error(`[WeChat] Failed to send reminder to ${userId}:`, err.message)
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
console.log('[WeChat] No active WeChat sessions to send reminder')
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
console.log('[WeChat] No active WeChat sessions to send reminder')
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
104
160
|
/**
|
|
105
161
|
* 拦截 SDK 的 stderr 输出,渲染二维码到终端
|
|
106
162
|
*/
|