There are two ways to upload PDFs to WhatsApp using their Business API. You either have to host the PDF in a public location, or you have to upload the PDF to the WhatsApp servers first, and then use that. If the POST message you send to WhatsApp uses a link type, that must be a public facing server location. If you want to follow the upload message route, behind the scenes, you will have to
upload the message to WhatsApp[
^], and then use the ID you get back from the upload in your POST message.
The WhatsApp documentation about this[
^].
If you have to use Twilio, then your code is probably going to have to look something like this:
const express = require('express');
const twilio = require('twilio');
const app = express();
const accountSid = 'YOUR_TWILIO_ACCOUNT_SID';
const authToken = 'YOUR_TWILIO_AUTH_TOKEN';
const client = new twilio.TwilioClient({
accountSid,
authToken
});
app.post('/upload-pdf', async (req, res) => {
const pdfFile = req.file('pdf');
const pdfMedia = await client.media.create({
content: pdfFile
});
const pdfUrl = pdfMedia.uri;
await client.messages.create({
body: 'Here is your PDF file:',
mediaUrl: pdfUrl,
to: req.body.toNumber
});
res.sendStatus(200);
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Note: Code sourced from bard.google.com query.