@onboardmobility/whatsapp
Version:
Utility class to connect to WhatsApp APIs
52 lines (44 loc) • 1.22 kB
JavaScript
;
const request = require('request-promise');
/**
* Envs vars
*/
const { WA_BASE_URL, WA_TOKEN, } = process.env;
async function processIncomingMessages({ messages }) {
for (let i = 0; i < messages.length; i++) {
const { body, author, fromMe, senderName, type } = messages[i];
if (!fromMe) {
return { body, author, fromMe, senderName, type, result: true };
}
}
return { result: false };
}
async function sendMessage({ caption, text, url, to }) {
const body = {
phone: to,
body: text,
};
return request.post({
url: `${WA_BASE_URL}sendMessage?token=${WA_TOKEN}`,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
}
async function sendFile({ to, url, filename, caption }) {
const body = {
phone: to,
body: url,
filename,
caption,
};
return request.post({
url: `${WA_BASE_URL}sendFile?token=${WA_TOKEN}`,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
}
module.exports = { processIncomingMessages, sendMessage, sendFile };