wechaty / puppet-wechat

Wechaty Puppet Provider for WeChat
https://www.npmjs.com/package/wechaty-puppet-wechat
Apache License 2.0
408 stars 68 forks source link

Get an empty file when the image is sent from bot and saved #174

Closed System233 closed 2 years ago

System233 commented 2 years ago
import { FileBox } from "file-box";
import {WechatyBuilder,Message} from "wechaty"
import {generate} from "qrcode-terminal"
const bot=WechatyBuilder.build({puppet:'wechaty-puppet-wechat',name:'db'});

bot.on('scan',code=>generate(code,{small:true}))
.on('message',async(msg:Message)=>{
    const target=msg.room()||msg.listener();
    if(target){
        if(msg.type()==bot.Message.Type.Text){
            const text=msg.text();
            if(/pdf/i.test(text)){
                console.log(`正在发送PDF`);
                const file=FileBox.fromFile("test.pdf");
                await target.say(file)
                console.log(`PDF发送完成`);
            }else if(/image/.test(text)){
                console.log(`正在发送图片`);
                const file=FileBox.fromFile("test.png");
                await target.say(file);
                console.log(`图片发送完成`);
            }
        }else if(msg.type()==bot.Message.Type.Image){
            const file=await msg.toFileBox();
            console.log(`收到图片:${file.name}`)
            const path="received-"+file.name;
            await file.toFile(path);
            console.log(`保存图片:${path}`)
        }
    }
});
bot.start();

Steps to reproduce

0.npm install wechaty wechaty-puppet-wechat file-box qrcode-terminal

  1. Copy any image as test.png in current directory
  2. Run this code
  3. Sent a text message image from WeChat client to anywhere.
  4. Your WeChat client will receive an image from bot, and bot will save the image to received-test.png in current directory.
  5. [BUG] The received-test.png is a empty file
huan commented 2 years ago

Is there any other user who can confirm this problem?

I can not understand why this happens and I believe it can work before.

huan commented 2 years ago

One possible reason for this problem

With the web protocol, you might not be able to save images from the messages sent by yourself.

The reason for this is related to the Web WeChat angular implementation and the Wechaty implementation bug.

Solution

- if(msg.type()==bot.Message.Type.Image){
+ if(msg.type()==bot.Message.Type.Image && !msg.self()){
   const file=await msg.toFileBox();
   console.log(`收到图片:${file.name}`)
   const path="received-"+file.name;
   await file.toFile(path);
   console.log(`保存图片:${path}`)
}