Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot send picture from Base64 #2242

Closed
gekkedev opened this issue Jun 27, 2024 · 1 comment · Fixed by #2248
Closed

Cannot send picture from Base64 #2242

gekkedev opened this issue Jun 27, 2024 · 1 comment · Fixed by #2248
Labels
bug Something isn't working needs triage

Comments

@gekkedev
Copy link
Contributor

gekkedev commented Jun 27, 2024

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

  • WPPConnect version(s): 1.32.3
  • WA-JS version(s): 3.4.1
  • Browser: Chrome @ latest
  • OS: Windows 11
  • Node version: Node 18
  • WhatsApp version: 2.3000.1013991265
  • MultiDevice (BETA): no

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 gekkedev added bug Something isn't working needs triage labels Jun 27, 2024
@gekkedev
Copy link
Contributor Author

gekkedev commented Jun 30, 2024

Apparently, the function expects not just base64, but a data URI.

const mime = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);

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:
return await this.sendImageFromBase64(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant