mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 17:25:15 +08:00
feat: 扩展中心支持多模态消息、HTML 渲染、一级导航与应用内通知
This commit is contained in:
1 parent
b20e514c90
commit
4ea73c3414
63 files changed
+8360
-644
No files matched your search
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
/** 群聊机器人后台入口壳:核心逻辑由 lib/bot-engine 承载,缺失时保持静默空操作且不报错。 */
|
||||
/** 群聊机器人后台入口壳:核心逻辑由 lib/bot-engine 承载,并内置按需图片/语音解析适配。 */
|
||||
|
||||
let engine = null
|
||||
try {
|
||||
@@ -9,6 +9,86 @@ try {
|
||||
engine = null
|
||||
}
|
||||
|
||||
function hasGrantedPermission(tm, permission) {
|
||||
const perms = tm && tm.extension && Array.isArray(tm.extension.permissions)
|
||||
? tm.extension.permissions
|
||||
: []
|
||||
return perms.includes(permission)
|
||||
}
|
||||
|
||||
/** 按需将图片或语音消息解析为可供机器人理解的纯文本摘要,缺权限或失败时安全降级为 null。 */
|
||||
async function resolveIncomingMediaText(tm, event, options = {}) {
|
||||
if (!event || typeof event !== 'object') return null
|
||||
if (event.type === 'text') {
|
||||
const text = String(event.text || '').trim()
|
||||
return text ? { text, source: 'text' } : null
|
||||
}
|
||||
const messageRef = String(event.messageRef || '').trim()
|
||||
if (!messageRef || !tm || !tm.media) return null
|
||||
|
||||
if (event.type === 'image') {
|
||||
const imageMode = options.imageMode || 'ocr'
|
||||
if (imageMode === 'off') return null
|
||||
|
||||
if (imageMode === 'vision' && hasGrantedPermission(tm, 'ai.vision')) {
|
||||
try {
|
||||
const analyzed = await tm.media.analyzeImage({ messageRef })
|
||||
const parts = [analyzed && analyzed.description, analyzed && analyzed.ocrText]
|
||||
.map((item) => String(item || '').trim())
|
||||
.filter(Boolean)
|
||||
if (parts.length > 0) {
|
||||
return { text: `[图片内容] ${parts.join(';')}`, source: 'image_vision' }
|
||||
}
|
||||
} catch {
|
||||
// 视觉理解失败时降级到本地 OCR。
|
||||
}
|
||||
}
|
||||
|
||||
if (hasGrantedPermission(tm, 'media.ocr')) {
|
||||
try {
|
||||
const ocr = await tm.media.ocrImage({ messageRef })
|
||||
const ocrText = String((ocr && ocr.text) || '').trim()
|
||||
if (ocrText) {
|
||||
return { text: `[图片文字] ${ocrText}`, source: 'image_ocr' }
|
||||
}
|
||||
} catch {
|
||||
// 本地 OCR 不可用时继续尝试可选视觉回退。
|
||||
}
|
||||
}
|
||||
|
||||
if (options.visionFallback === true && hasGrantedPermission(tm, 'ai.vision')) {
|
||||
try {
|
||||
const analyzed = await tm.media.analyzeImage({ messageRef })
|
||||
const desc = String((analyzed && analyzed.description) || '').trim()
|
||||
if (desc) return { text: `[图片内容] ${desc}`, source: 'image_vision' }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
if (event.type === 'voice') {
|
||||
if (options.voiceTranscribeEnabled === false) return null
|
||||
if (!hasGrantedPermission(tm, 'media.transcribe')) return null
|
||||
try {
|
||||
const result = await tm.media.transcribeVoice({
|
||||
messageRef,
|
||||
waitMs: Number(options.voiceWaitMs) > 0 ? Number(options.voiceWaitMs) : 12_000
|
||||
})
|
||||
if (result && result.state === 'transcribed' && String(result.transcript || '').trim()) {
|
||||
return {
|
||||
text: `[语音转文字] ${String(result.transcript).trim()}`,
|
||||
source: 'voice_transcript'
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function registerFallbackHandlers(tm) {
|
||||
if (!tm || !tm.page || typeof tm.page.onRequest !== 'function') return
|
||||
const unavailable = async () => {
|
||||
@@ -19,9 +99,9 @@ function registerFallbackHandlers(tm) {
|
||||
tm.page.onRequest('testSend', unavailable)
|
||||
}
|
||||
|
||||
module.exports = function activate(api) {
|
||||
function activate(api) {
|
||||
if (engine && typeof engine.activateBotRuntime === 'function') {
|
||||
engine.activateBotRuntime(api)
|
||||
engine.activateBotRuntime(api, { resolveIncomingMediaText })
|
||||
} else {
|
||||
registerFallbackHandlers(api)
|
||||
}
|
||||
@@ -29,3 +109,6 @@ module.exports = function activate(api) {
|
||||
api.log.info('群聊机器人已启动')
|
||||
}
|
||||
}
|
||||
|
||||
activate.resolveIncomingMediaText = resolveIncomingMediaText
|
||||
module.exports = activate
|
||||
Reference in new issue
Block a user