@onboardmobility/whatsapp
Version:
Utility class to connect to WhatsApp APIs
60 lines (49 loc) • 1.34 kB
JavaScript
;
const request = require('request-promise');
/**
* Envs vars
*/
const { WA_BASE_URL, WA_TOKEN, } = process.env;
async function processIncomingMessages(data) {
let { message, user, type } = data;
if (type === 'message') {
let { text, fromMe } = message;
if (fromMe) return;
const body = text;
const author = user.phone;
const senderName = user.name;
return { body, author, fromMe, senderName, type, result: true };
}
return { result: false }
}
async function sendMessage({ caption, text, url, to }) {
const body = {
type: 'text',
to_number: to,
message: text,
};
return request.post({
url: `${WA_BASE_URL}`,
headers: {
'Content-Type': 'application/json',
'x-maytapi-key': WA_TOKEN,
},
body: JSON.stringify(body),
});
}
async function sendFile({ to, url, filename, caption }) {
const body = {
type: 'media',
to_number: to,
message: url,
};
return request.post({
url: `${WA_BASE_URL}`,
headers: {
'Content-Type': 'application/json',
'x-maytapi-key': WA_TOKEN,
},
body: JSON.stringify(body),
});
}
module.exports = { processIncomingMessages, sendMessage, sendFile };