wppconnect-team / wppconnect

WPPConnect is an open source project developed by the JavaScript community with the aim of exporting functions from WhatsApp Web to the node, which can be used to support the creation of any interaction, such as customer service, media sending, intelligence recognition based on phrases artificial and many other things, use your imagination
https://wppconnect.io
Other
1.77k stars 304 forks source link

Cannot send picture from Base64 #2242

Open gekkedev opened 1 week ago

gekkedev commented 1 week ago

Description

Images cannot be sent as picture As reference image, I'm taking a screenshot:

const picture = await client.takeScreenshot()
console.log(picture) //logs valid and displayable base64
console.log(
  JSON.stringify(
    await client.sendImageFromBase64(message.chatId, picture, "Screenshot_20240627-153045_WhatsApp.jpg")
    //error:    [session:client] [object Object]
  )
)

console.log(
  JSON.stringify(
    await client.sendFileFromBase64(message.chatId, picture, "Screenshot_20240627-153045_WhatsApp.jpg")
    //[session:client] Error: Empty or invalid file or base64
  )
)

Environment

Steps to Reproduce

  1. use this code
  2. send a message
  3. try the file-sending method, same issue but a more detailed error

Log Output

sendImageFromBase64:

error:    [session:client] [object Object]

wrapping this in a try/catch structure yields this:

{ erro: true, to: '123456***90@c.us', text: 'Invalid base64!' }

sendFileFromBase64:

[session:client] Error: Empty or invalid file or base64

Additional context / Screenshot

did not try it with other pictures yet, so there could be an issue with the screenshot creation function's output (although it is displayable)

gekkedev commented 4 days ago

Apparently, the function expects not just base64, but a data URI. https://github.com/wppconnect-team/wppconnect/blob/135805324b04479f2ea7811620525d8a750ca05a/src/api/helpers/base64-mimetype.ts#L24 Prefixing "data:image/png;base64," to the screenshot works. IMHO, it would be better to prefix it automatically and verify the actual image, or not at all. I'll create a PR to update the documentation and describe the workaround in the JSDoc here: https://github.com/wppconnect-team/wppconnect/blob/135805324b04479f2ea7811620525d8a750ca05a/src/api/layers/sender.layer.ts#L224