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

Having a general Endpoint for sending media files #36

Open
dhyeythumar opened this issue Aug 1, 2021 · 1 comment
Open

Having a general Endpoint for sending media files #36

dhyeythumar opened this issue Aug 1, 2021 · 1 comment

Comments

@dhyeythumar
Copy link

dhyeythumar commented Aug 1, 2021

Currently, according to my understanding, there is only 2 type of endpoint available (image & pdf) to send the media files.
But I think there should be a general endpoint for sending all sorts of media files & that endpoint might take media type as an query parameter or even find the media type on its own.

So I think this should solve any further issues in sending different type files, such as this issue here Send contact card (vcard)


For example: (roughly this both endpoints are doing the same thing)

router.post('/sendimage/:phone', async (req,res) => {
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
let phone = req.params.phone;
let image = req.body.image;
let caption = req.body.caption;
if (phone == undefined || image == undefined) {
res.send({ status: "error", message: "please enter valid phone and base64/url of image" })
} else {
if (base64regex.test(image)) {
let media = new MessageMedia('image/png',image);
client.sendMessage(`${phone}@c.us`, media, { caption: caption || '' }).then((response) => {
if (response.id.fromMe) {
res.send({ status: 'success', message: `MediaMessage successfully sent to ${phone}` })
}
});
} else if (vuri.isWebUri(image)) {
if (!fs.existsSync('./temp')) {
await fs.mkdirSync('./temp');
}
var path = './temp/' + image.split("/").slice(-1)[0]
mediadownloader(image, path, () => {
let media = MessageMedia.fromFilePath(path);
client.sendMessage(`${phone}@c.us`, media, { caption: caption || '' }).then((response) => {
if (response.id.fromMe) {
res.send({ status: 'success', message: `MediaMessage successfully sent to ${phone}` })
fs.unlinkSync(path)
}
});
})
} else {
res.send({ status:'error', message: 'Invalid URL/Base64 Encoded Media' })
}
}
});
router.post('/sendpdf/:phone', async (req,res) => {
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
let phone = req.params.phone;
let pdf = req.body.pdf;
if (phone == undefined || pdf == undefined) {
res.send({ status: "error", message: "please enter valid phone and base64/url of pdf" })
} else {
if (base64regex.test(pdf)) {
let media = new MessageMedia('application/pdf', pdf);
client.sendMessage(`${phone}@c.us`, media).then((response) => {
if (response.id.fromMe) {
res.send({ status: 'success', message: `MediaMessage successfully sent to ${phone}` })
}
});
} else if (vuri.isWebUri(pdf)) {
if (!fs.existsSync('./temp')) {
await fs.mkdirSync('./temp');
}
var path = './temp/' + pdf.split("/").slice(-1)[0]
mediadownloader(pdf, path, () => {
let media = MessageMedia.fromFilePath(path);
client.sendMessage(`${phone}@c.us`, media).then((response) => {
if (response.id.fromMe) {
res.send({ status: 'success', message: `MediaMessage successfully sent to ${phone}` })
fs.unlinkSync(path)
}
});
})
} else {
res.send({ status: 'error', message: 'Invalid URL/Base64 Encoded Media' })
}
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@dhyeythumar and others