From e74574c60e3c86b703c88ee0e9c0c686e021ada1 Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Sat, 19 Aug 2023 10:57:44 +0100 Subject: [PATCH] feat: initial whatsapp api --- lib/plugins/sms/whatsapp/whatsapp.js | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/plugins/sms/whatsapp/whatsapp.js diff --git a/lib/plugins/sms/whatsapp/whatsapp.js b/lib/plugins/sms/whatsapp/whatsapp.js new file mode 100644 index 00000000..dee498ef --- /dev/null +++ b/lib/plugins/sms/whatsapp/whatsapp.js @@ -0,0 +1,47 @@ +const axios = require('axios') + +const NAME = 'Whatsapp' + +function sendMessage (account, rec) { + const phoneId = account.phoneId + const token = account.apiKey + + const to = rec.sms.toNumber || account.toNumber + const template = rec.sms.template + + const url = `https://graph.facebook.com/v17.0/${phoneId}/messages` + + const config = { + headers:{ + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json' + } + } + + const data = { + messaging_product: 'whatsapp', + recipient_type: 'individual', + type: 'template', + to, + template: { + name: template, + language: { code: 'en_US' } + } + } + + axios.post(url, data, config) + .catch(err => { + // console.log(err) + throw new Error(`Whatsapp error: ${err.message}`) + }) +} + +function getLookup () { + throw new Error('Whatsapp error: lookup not supported') +} + +module.exports = { + NAME, + sendMessage, + getLookup +}