Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

发送本地图片失败 #281

Closed
4 tasks done
DrMuda opened this issue Dec 25, 2024 · 4 comments
Closed
4 tasks done

发送本地图片失败 #281

DrMuda opened this issue Dec 25, 2024 · 4 comments

Comments

@DrMuda
Copy link

DrMuda commented Dec 25, 2024

⚠️不按照以下格式创建的issue将不会获得回复⚠️

提bug前先检查以下是否已经执行

  • 我已更新了最新版本的代码
  • 我已经仔细地阅读了readme 文档
  • 我已在FAQ里查找了可能寻找的答案
  • 我已经尝试搜索过历史关闭issue寻找可能的答案

bug描述

我尝试发送我服务器里的本地图片到微信, 但是失败了

所以请按以下格式描述问题:

  • 场景:发送服务器本地图片到微信
  • 操作:我使用
axios.post('http://localhost:3001/webhook/msg/v2?token=YpIZOxT77sGR', {
    to,
    data: { type: 'fileUrl', content: picPath },
    isRoom,
  }); 

进行发送, 其中, 请求地址确保是正确的, picPath为:file:///wechat-bot-data/illust/1735117451274/94558684.png

  • 表现
    发送失败, 报错信息如下
[2024-12-25T09:04:11.933] [ERROR] - Error downloading file:file:///wechat-bot-data/illust/1735117451274/94558684.png TypeError: fetch failed
    at node:internal/deps/undici/undici:12618:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async downloadFile (/app/src/utils/index.js:13:22)
    at async Object.getMediaFromUrl (/app/src/utils/index.js:76:47)
    at async formatAndSendMsg (/app/src/service/msgSender.js:529:24)
    at async handleMsg2Single (/app/src/service/msgSender.js:421:27)
    at async handleSendV2MsgCollectInfo (/app/src/service/msgSender.js:186:7)
    at async Object.handleSendV2Msg (/app/src/service/msgSender.js:20:27)
    at async /app/src/route/msg.js:25:48
    at async dispatch (/app/node_modules/.pnpm/[email protected]/node_modules/hono/dist/cjs/compose.js:51:17) {
  cause: Error: not implemented... yet...
      at makeNetworkError (node:internal/deps/undici/undici:5840:35)
      at schemeFetch (node:internal/deps/undici/undici:10738:34)
      at node:internal/deps/undici/undici:10615:26
      at mainFetch (node:internal/deps/undici/undici:10634:11)
      at fetching (node:internal/deps/undici/undici:10582:7)
      at fetch (node:internal/deps/undici/undici:10446:20)
      at Object.fetch (node:internal/deps/undici/undici:12617:10)
      at fetch (node:internal/process/pre_execution:281:25)
      at global.fetch (/app/node_modules/.pnpm/@[email protected]/node_modules/@hono/node-server/dist/index.js:182:10)
      at downloadFile (/app/src/utils/index.js:13:28)
}

提供有用的信息

  • 目前使用的版本号:2.8.2
  • 我尝试过两种文件路径, 一种如上面报错的, file://{文件路径}, 一种是没有带上file://, 第二种也报错了[cause]: TypeError [ERR_INVALID_URL]: Invalid URL。 同时我确保该文件是能访问的
    image
    我没能在issue中找到发送本地图片的案例, 不清楚是不是我的发送方式有问题
@DrMuda
Copy link
Author

DrMuda commented Dec 25, 2024

https://github.com/danni-cool/wechatbot-webhook?tab=readme-ov-file#%E8%AF%BB%E6%96%87%E4%BB%B6%E5%8F%91%E9%80%81

仍然不行, 不知道究竟要什么类型,我试了 buffer 类型, 文件确实发出来了, 但是丢失了文件名和格式, 导致微信上面只显示一个 undefined 的文件, 应该是图片才对
image
然后我还试了 Blob和File类型, 都提示

/app/node_modules/.pnpm/[email protected]/node_modules/delayed-stream/lib/delayed_stream.js:33
  source.on('error', function() {});
         ^

TypeError: source.on is not a function
    at DelayedStream.create (/app/node_modules/.pnpm/[email protected]/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
    at CombinedStream.append (/app/node_modules/.pnpm/[email protected]/node_modules/combined-stream/lib/combined_stream.js:45:37)
    at FormData.append (/app/node_modules/.pnpm/[email protected]/node_modules/form-data/lib/form_data.js:75:3)
    at sendPicToWx (/app/dist/utils/index.js:48:14)
    at searchPic (/app/dist/pixiv/index.js:24:51)

我的代码是这样的

import * as fs from 'fs';
import { File, Blob } from 'buffer';
// form-data 是第三方库
import * as FormData from 'form-data';

// 读取文件为 Buffer
  const fileBuffer = fs.readFileSync(picPath);

  // // 转换为 Blob
  const blob = new Blob([fileBuffer], { type: 'image/png' });
  // const file = new File([fileBuffer], '图片.png');

  const formData = new FormData();
  formData.append('to', to);
  formData.append('isRoom', (isRoom ? 1 : 0).toString());
  formData.append('content', blob);

  return axios.post(
    'http://localhost:3001/webhook/msg?token=YpIZOxT77sGR',
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    },
  );

@danni-cool
Copy link
Owner

formdata 是麻烦的,你可以参考下我写的
https://github.com/danni-cool/wechatbot-webhook/blob/main/src/service/msgUploader.js

@DrMuda
Copy link
Author

DrMuda commented Dec 26, 2024

formdata 是麻烦的,你可以参考下我写的 https://github.com/danni-cool/wechatbot-webhook/blob/main/src/service/msgUploader.js

感谢, 在append formData时增加文件名、类型正常了。 一直用的原生formData, 不清楚这个库还有第三个参数

@DrMuda DrMuda closed this as completed Dec 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants