Skip to content

Commit

Permalink
fix: 修复企微转发图片失效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Aug 13, 2024
1 parent a3d135c commit 5029348
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-web-panel",
"version": "1.6.60",
"version": "1.6.61",
"description": "智能微秘书插件",
"exports": {
".": {
Expand Down Expand Up @@ -52,19 +52,15 @@
"@chatie/semver": "^0.4.7",
"@chatie/tsconfig": "^4.6.3",
"@grpc/grpc-js": "1.9.14",
"@juzi/wechaty": "^1.0.87",
"@juzi/wechaty-puppet": "^1.0.78",
"@juzi/wechaty-puppet-service": "^1.0.87",
"@juzi/wechaty": "^1.0.99",
"@juzi/wechaty-puppet": "^1.0.90",
"@juzi/wechaty-puppet-service": "^1.0.100",
"babel-eslint": "^10.1.0",
"eslint": "^7.4.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"wechaty": "^1.20.2",
"wechaty-puppet": "^1.21.1",
"wechaty-puppet-padlocal": "^1.20.1",
"wechaty-puppet-service": "^1.18.2",
"wechaty-puppet-wechat4u": "^1.14.12"
},
"readme": "README.md",
Expand All @@ -73,7 +69,6 @@
"npm": ">=7"
},
"dependencies": {
"@coze/coze-js": "^0.1.2",
"@dqbd/tiktoken": "^1.0.2",
"axios": "^1.6.6",
"baidu-aip-sdk": "^4.16.10",
Expand Down
5 changes: 5 additions & 0 deletions src/common/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export async function privateForward({ that, msg, name, config }) {
if(item.type === 1 && msg.type() === 7) {
room && msg && msg.forward(room)
} else {
if(msg.type() === 6) {
const file = await msg.toFileBox()
room.say(file)
} else {
room && msg && msg.forward(room)
}
}
}
for(let contactName of item.contacts) {
Expand Down
35 changes: 30 additions & 5 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,15 +458,40 @@ export function isCDNFileUrl(url) {
* @param text
* @returns {*[]}
*/

function extractMarkdownLinks(text) {
// 正则表达式匹配所有 Markdown 链接格式
const linkRegex = /\[([^\]]*)\]\(([^)]+)\)/g
const links = []
let cleanedText = text

let match
while ((match = linkRegex.exec(text)) !== null) {
let url = match[2]

// 移除 URL 两端可能存在的引号
url = url.replace(/^["']|["']$/g, '')

if (url) {
links.push(url)
}

cleanedText = cleanedText.replace(match[0], '')
}

return {
links: links,
cleanedText: cleanedText.trim()
}
}
export function extractImageLinks(text) {
// const httpRegex = /(http:\/\/\S+\.(?:jpg|png|gif|webp|jpeg|mp4|doc|docx|xls|xlsx|ppt|pptx|avi|zip|wav|rar|pdf|txt|log))/g;
// const httpsRegex = /(https:\/\/\S+\.(?:jpg|png|gif|webp|jpeg|mp4|doc|docx|xls|xlsx|ppt|pptx|avi|zip|wav|rar|pdf|txt|log))/g;
const mdRegexHttps = /!\[[^\]]*\]\((https?:\/\/\S+)\)/g;
const mdRegexHttp = /!\[[^\]]*\]\((http?:\/\/\S+)\)/g;

const fileRegexHttp = /\[[^\]]*\]\((http?:\/\/\S+)\)/g;
const filesRegexHttp = /\[[^\]]*\]\((https?:\/\/\S+)\)/g;

let imageLinks = [];
const { links, cleanedText } = extractMarkdownLinks(text)
let imageLinks = links || [];
let match;

// while ((match = httpRegex.exec(text)) !== null) {
Expand All @@ -477,7 +502,7 @@ export function extractImageLinks(text) {
// imageLinks.push(match[0]);
// }

while ((match = mdRegexHttp.exec(text)) !== null || (match = mdRegexHttps.exec(text)) !== null || (match = fileRegexHttp.exec(text)) !== null || (match = filesRegexHttp.exec(text)) !== null) {
while ((match = fileRegexHttp.exec(cleanedText)) !== null || (match = filesRegexHttp.exec(cleanedText)) !== null) {
imageLinks.push(match[1]);
}

Expand Down
2 changes: 1 addition & 1 deletion test/wechat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {WechatyBuilder} from 'wechaty'
import {WechatyBuilder} from '@juzi/wechaty'
import {WechatyWebPanelPlugin, WechatyMessageRecordPlugin} from '../src/index.js'

const name = 'wechat-assistant-pro';
Expand Down

0 comments on commit 5029348

Please sign in to comment.