Closed suntong closed 3 years ago
Found that my two images are actually failing on two different type of images.
The second image failed on the line of
let name = saveDir + '/' + file.name
after getting the file
above, and the error is:
(node:103479) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined
at msgMediaFile (/.../on-message.js:481:35)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async Wechaty.onMessage (/.../on-message.js:315:7)
You can test my two images with
https://github.com/wechaty/wechaty-getting-started/blob/master/examples/advanced/media-file-bot.js
(OK that the first one not fixed, but the 2nd image should be easy to fix)
after getting the
file
above, and the error is:(node:103479) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined
I.e., the msg.toFileBox()
is returning undefined
when it is an image.
Just to make it clear what the second case is for toFileBox()
.
(as I misread the message and thought let name = saveDir + '/' + (file.name || 'default')
should have fixed the problem)
Here is another case:
Error: Can not get file for message: 6872153355253203246 caught at msg.toFileBox() in House.
WechatifiedMessage {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
id: '6872153355253203246',
payload: {
id: '68721...253203246',
timestamp: 1620145211,
type: 6,
fromId: 'ge...gsz',
mentionIdList: [],
roomId: '60...884@chatroom',
text: '<?xml version="1.0"?>\n' +
'<msg>\n' +
'\t<img aeskey="2dd56b19aa84a2ae73752c953c1431cd" encryver="0" cdnthumbaeskey="2dd56b19aa84a2ae73752c953c1431cd" cdnthumburl="30580201000451304f0201000204fe09633a02030f5259020425dccdcb02046091743a042a777875706c6f61645f363032333133343838344063686174726f6f6d3833355f313632303134353231300204010d0a020201000400" cdnthumblength="3882" cdnthumbheight="120" cdnthumbwidth="67" cdnmidheight="0" cdnmidwidth="0" cdnhdheight="0" cdnhdwidth="0" cdnmidimgurl="30580201000451304f0201000204fe09633a02030f5259020425dccdcb02046091743a042a777875706c6f61645f363032333133343838344063686174726f6f6d3833355f313632303134353231300204010d0a020201000400" length="1" md5="e2c83d68a055ce661a1f184a8c0f25cb" hevc_mid_size="73562" />\n' +
'</msg>\n',
toId: undefined
},
[Symbol(kCapture)]: false
}
For image messages, use await message.toImage()
to get the image.
If allowing toFileBox
to return the image, but which type of image should it be?
export enum ImageType {
Unknown = 0,
Thumbnail,
HD,
Artwork,
}
So, we recommend to get image by toImage
only.
Hi bro, sorry I have to disagree with you at this issue,
message.toImage()
is not in the Wechaty common API standard https://wechaty.js.org/docs/api/message. if (msg.type() !== Message.Type.Text) {
const file = await msg.toFileBox()
const name = file.name
console.log('Save file to: ' + name)
file.toFile(name)
}
This is how Wechaty's standard way of saving Media Files, be it Audio, Video, Attachment or Image. The code is exactly the same. I've used basically all Wechaty puppets that ever existed, and they all behave the same and the above code always works.
I think padlocal
doing it differently than anybody else is not a good practice and should not be encouraged, right @huan ?
Will get hd image for image message with toFileBox()
That works like a charm my dear brother. Thanks a lot!!!
I think padlocal doing it differently than anybody else is not a good practice and should not be encouraged, right @huan ?
I agree with you that we should keep all the behavior be the same as possible as we can for the whole Wechaty ecosystem so that our developers can get consistent experiences crossing the puppet provider/services.
For this case, I agree that the toFileBox()
should be supported for the image type message and return the HD size image. If users want to specify the different sizes for the image, then they should use toImage
for maximum flexibility.
Thanks for your support @huan.
Our brother had already fixed that according to Wechaty ecosystem's common behavior. Thanks @padlocal.
I've got the following error:
on my code (on-message.js:461:16) of:
which is used to get the media file from WX.
It used to be working properly with other puppets, but
padlocal
is failing on images at least:I'll send you some sample images for you to test...