Sign in
pedroslopez / whatsapp-web.js Public Sponsor Notifications Fork 3.4k Star 14.4k
Code Issues 91 Pull requests 30 Actions Security Insights
How to use MessageMedia.fromUrl() ? #1168 New issue
Closed aidulcandra opened this issue on Feb 3, 2022 · 11 comments
aidulcandra commented on Feb 3, 2022 Assignees
No one assigned
Hello. I still don't understand how to use MessageMedia.fromUrl() . Can anyone give me an example?
Labels
None yet
andrii-bohdan commented on Feb 3, 2022
Projects
Hi, simple example
None yet
let url = "https://wwebjs.dev/logo.png";
const media = await MessageMedia.fromUrl(url);
Milestone
media.mimetype = "image/png";
No milestone
media.filename = "CustomImageName.png";
client.sendMessage("[email protected]", media, {caption: "Image"});
Development
6 1 1 No branches or pull requests
6 participants
aidulcandra commented on Feb 4, 2022 • edited Author
Hi, simple example
let url = "https://wwebjs.dev/logo.png"; const media = await MessageMedia.fromUrl(url); media.mimetype = "image/png";
media.filename = "CustomImageName.png"; client.sendMessage("
[email protected]", media, {caption: "Image"});
I see... so do we still have to specify a filename? Can I just use a URL to create a media directly?
andrii-bohdan commented on Feb 4, 2022
Hi, simple example
let url = "https://wwebjs.dev/logo.png"; const media = await MessageMedia.fromUrl(url); media.mimetype = "image/png";
media.filename = "CustomImageName.png"; client.sendMessage("
[email protected]", media, {caption: "Image"});
I see... so do we still have to specify a filename? Can I just use a URL to create a media directly?
My advice is simple Be curious
so do we still have to specify a filename? - NO
Can I just use a URL to create a media directly? - Yes
Using media.filename = "CustomName.png" you change original name its can be useful an example for prevent duplicate media file if you
have intention to store then
All the best
itraderapp commented on Feb 4, 2022
Hi, thank you very much for the help!
when I put 'await' it doesn't start!
when i remove 'await' it starts but doesn't work!
andrii-bohdan commented on Feb 5, 2022 • edited
Hi, thank you very much for the help!
when I put 'await' it doesn't start!
when i remove 'await' it starts but doesn't work!
This is because this operation must be asynchronous.
The one of solution might be
Putting your code inside anonymous function
(async () => {
let url ="some url"
const media = await MessageMedia.fromUrl(url);
client.sendMessage("[email protected]", media, {caption: "some caption"}) ;
})();
Here is useful information about async/await operation in javascript
https://javascript.info/async-await
Update
If you calling media from client.on("ready" () => {}); try to add async inside this function
Example:
client.on("message", async(msg) => {
//now you have available await operation
const media = await MessageMedia.fromUrl(url);
})
pedroslopez commented on Feb 5, 2022 Owner
I think this is safe to close as @ItHustleCoder has answered pretty clearly.
I've also just added this to the guide at https://wwebjs.dev/guide/handling-attachments.html#sending-files-from-a-url
pedroslopez closed this as completed on Feb 5, 2022
itraderapp commented on Feb 7, 2022
Olá, muito obrigado pela ajuda!
quando coloco 'aguardar' ele não inicia!
quando eu removo 'await' ele inicia mas não funciona!
Isso ocorre porque essa operação deve ser assíncrona. A solução pode ser Colocar seu código dentro de uma função anônima
(async () => { let url ="some url" const media = await MessageMedia.fromUrl(url); client.sendMessage("[email protected]",
media, {caption: "some caption"}) ; })();
**Aqui estão informações úteis sobre a operação async/await em javascript ** https://javascript.info/async-await
Update Se você estiver chamando a mídia de **client.on("ready" () => {}); ** tente adicionar async dentro desta função Exemplo:
client.on("message", async(msg) => { //now you have available await operation const media = await MessageMedia.fromUrl(url);
})
Muito Obrigado! funcionou perfeitamente!
akangozan007 commented on Jul 5, 2023
i have an issue that i could'nt sending my pptx via MessageMedia.fromUrl(url)
akangozan007 commented on Jul 5, 2023
akangozan007 commented on Jul 5, 2023
is there any solutions?
KassioM commented on Mar 16 • edited
Hello, i have the solution, use the await method and don't use "new MessageMedia", see the example:
let url = media.url;
const body = await MessageMedia.fromUrl(url);
const query = media.caption ? { caption: media.caption } : {};
await client.sendMessage(to, body, query);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Terms Privacy Security Status Docs Contact Manage cookies Do not share my personal information
© 2024 GitHub, Inc.