@onboardmobility/whatsapp
Version:
Utility class to connect to WhatsApp APIs
143 lines (121 loc) • 4.02 kB
JavaScript
;
const request = require('request-promise');
const fs = require('fs');
const axios = require('axios');
const { get } = axios;
/**
* Envs vars
*/
const { WA_BASE_URL, WA_TOKEN, } = process.env;
async function processIncomingMessages(message) {
const { text, waId, owner, senderName, type, data, fileName, listReply, interactiveButtonReply } = message;
let finalType = type;
let finalText = text;
if (!owner) {
const attachments = [];
if (data) {
const parts = data.split('fileName=');
const fileName = parts[1];
const fileNameParts = fileName.split('/');
attachments.push({
authorization: `Bearer ${WA_TOKEN}`,
contentUrl: `${WA_BASE_URL}api/v1/getMedia?fileName=${fileName}`,
name: fileNameParts[fileNameParts.length - 1],
contentType: type
});
}
if (listReply) {
finalType = 'list_reply';
finalText = listReply.id;
}
if (interactiveButtonReply) {
finalType = 'button_reply';
finalText = interactiveButtonReply.id;
}
return {
body: finalText, author: waId, fromMe: owner,
senderName, type: finalType, result: true, attachments
};
} else {
return { result: false };
}
}
async function sendMessage({ caption, text, url, to }) {
return request.post({
url: `${WA_BASE_URL}api/v1/sendSessionMessage/${to}?messageText=${encodeURI(text)}`,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${WA_TOKEN}`
}
});
}
async function sendFile({ to, url, filename, caption }) {
const file = await get(url, { responseType: 'arraybuffer' });
const req = request.post({
url: `${WA_BASE_URL}api/v1/sendSessionFile/${to}`,
headers: {
'Authorization': `Bearer ${WA_TOKEN}`
},
formData: {
'file': {
'value': file.data,
'options': { 'filename': filename, 'contentType': null }
}
}
});
return req;
}
/**
* Send a list or button message to the user as of https://developers.facebook.com/docs/whatsapp/guides/interactive-messages
* @param {Object} params the data needed:
* to: destination
* header: optional header text
* body: the message body
* footer: option footer text
* actions: the list of actions
* type: button or list
*/
async function sendInteractive({ to, header, body, footer, actions, type }) {
try {
if (!to || !body) {
return;
}
const sendBody = {};
if (header) {
if (type === 'button') {
sendBody.header = {
'type': 'Text',
'text': header
};
} else {
sendBody.header = header;
}
}
if (footer) {
sendBody.footer = footer;
}
if (body) {
sendBody.body = body;
}
if (actions && actions.buttons) {
sendBody.buttons = actions.buttons.map((e) => ({
text: e.reply.title,
}));
}
if (actions && actions.sections) {
sendBody.buttonText = actions.button;
actions.sections[0].title = actions.button;
sendBody.sections = actions.sections;
}
return axios.post(type === 'button' ? `${WA_BASE_URL}api/v1/sendInteractiveButtonsMessage?whatsappNumber=${to}` :
`${WA_BASE_URL}api/v1/sendInteractiveListMessage?whatsappNumber=${to}`, sendBody, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${WA_TOKEN}`
}
});
} catch (e) {
return false;
}
}
module.exports = { processIncomingMessages, sendMessage, sendFile, sendInteractive };