UNPKG

@onboardmobility/whatsapp

Version:

Utility class to connect to WhatsApp APIs

154 lines (137 loc) 4.73 kB
'use strict'; 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) { let { text, waId, owner, senderName, type, data, fileName, listReply, conversationId } = message; 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 (type === 'interactive') { if (listReply) { type = 'list_reply'; text = listReply.title; } } return { body: text, author: waId, fromMe: owner, senderName, type, result: true, attachments, conversationId }; } 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; } if (type === 'list') { const sendBody = { 'to': to, 'header': header, 'body': body, 'footer': footer, 'buttonText': actions.button, 'sections': actions.sections // 'type': 'interactive', // 'recipient_type': 'individual', // 'interactive': { // 'type': type, // 'body': { // 'text': body // }, // 'action': actions // }, }; return axios.post(`${WA_BASE_URL}api/v1/sendInteractiveListMessage?whatsappNumber=${to}`, sendBody, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${WA_TOKEN}` }}); } else { for (const button of actions.buttons) { if (!button.text) { button.text = button.reply ? button.reply.title : 'sem título'; } } const sendBody = { 'to': to, 'header': { "type": "Text", "text": header, }, 'body': body, 'footer': footer, 'buttons': actions.buttons // 'type': 'interactive', // 'recipient_type': 'individual', // 'interactive': { // 'type': type, // 'body': { // 'text': body // }, // 'action': actions // }, }; return axios.post(`${WA_BASE_URL}api/v1/sendInteractiveButtonsMessage?whatsappNumber=${to}`, sendBody, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${WA_TOKEN}` }}); } } catch (e) { return false; } } module.exports = { processIncomingMessages, sendMessage, sendFile, sendInteractive };