foliko 1.0.16 → 1.0.17
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 +52 -0
package/package.json
CHANGED
package/plugins/weixin-plugin.js
CHANGED
|
@@ -96,11 +96,63 @@ module.exports = function(Plugin) {
|
|
|
96
96
|
await this._handleMessage(msg)
|
|
97
97
|
})
|
|
98
98
|
|
|
99
|
+
// 监听定时提醒事件
|
|
100
|
+
if (this._framework) {
|
|
101
|
+
this._framework.on('scheduler:reminder', async (data) => {
|
|
102
|
+
console.log('[WeChat] Received scheduler reminder:', data)
|
|
103
|
+
await this._handleScheduledReminder(data)
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
99
107
|
// 启动Bot
|
|
100
108
|
await this._bot.run()
|
|
101
109
|
console.log('[WeChat] 开始接收微信消息')
|
|
102
110
|
}
|
|
103
111
|
|
|
112
|
+
/**
|
|
113
|
+
* 处理定时提醒
|
|
114
|
+
*/
|
|
115
|
+
async _handleScheduledReminder(data) {
|
|
116
|
+
const { taskName, message, sessionId } = data
|
|
117
|
+
|
|
118
|
+
// 构建提醒消息
|
|
119
|
+
const reminderText = `🔔 [${taskName}]\n\n${message}`
|
|
120
|
+
|
|
121
|
+
// 如果有 sessionId 是 weixin 类型的,发送到对应用户
|
|
122
|
+
if (sessionId && sessionId.startsWith('weixin_')) {
|
|
123
|
+
const userId = sessionId.replace('weixin_', '')
|
|
124
|
+
try {
|
|
125
|
+
await this._bot.send(userId, reminderText)
|
|
126
|
+
console.log(`[WeChat] Reminder sent to user ${userId}`)
|
|
127
|
+
} catch (err) {
|
|
128
|
+
console.error(`[WeChat] Failed to send reminder:`, err.message)
|
|
129
|
+
}
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// 其他情况(包括 null 或其他 sessionId),发送到最近的 Weixin 会话
|
|
134
|
+
if (this._sessionPlugin) {
|
|
135
|
+
const allSessions = this._sessionPlugin.listSessions()
|
|
136
|
+
const weixinSessions = allSessions
|
|
137
|
+
.filter(s => s.id.startsWith('weixin_'))
|
|
138
|
+
.sort((a, b) => new Date(b.lastActive).getTime() - new Date(a.lastActive).getTime())
|
|
139
|
+
|
|
140
|
+
if (weixinSessions.length > 0) {
|
|
141
|
+
const userId = weixinSessions[0].id.replace('weixin_', '')
|
|
142
|
+
try {
|
|
143
|
+
await this._bot.send(userId, reminderText)
|
|
144
|
+
console.log(`[WeChat] Reminder sent to user ${userId}`)
|
|
145
|
+
} catch (err) {
|
|
146
|
+
console.error(`[WeChat] Failed to send reminder to ${userId}:`, err.message)
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
console.log('[WeChat] No active WeChat sessions to send reminder')
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
console.log('[WeChat] No active WeChat sessions to send reminder')
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
104
156
|
/**
|
|
105
157
|
* 拦截 SDK 的 stderr 输出,渲染二维码到终端
|
|
106
158
|
*/
|