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

PDF file sent as unknown file #175

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 PDF file as test.pdf in current directory
  2. Run this code and login
  3. Sent a text message pdf from WeChat client to anywhere.
  4. [BUG]Your WeChat client will receive a PDF file from bot, but the file type is unknown 无标题
huan commented 2 years ago

I'm not sure whether the .PDF file type is sealed correctly.

Please refer to the below code to see if you can figure something out:

https://github.com/wechaty/puppet-wechat/blob/c93ce9366671aee264716ac151ebea5ed037ad2a/src/puppet-wechat.ts#L1245-L1258