UNPKG

business-whatsapp

Version:

WhatsApp Business API Client unofficial

241 lines (240 loc) 6.99 kB
export function onlyNumbers(waid) { return waid.replace(/[^0-9]/g, ''); } export function defaultHeaders(apikey) { return { Authorization: `Bearer ${apikey}`, 'Content-Type': 'application/json', 'Accept': 'application/json', }; } export function payload(apikey, json = null, method = 'POST') { if (json) { return { headers: defaultHeaders(apikey), method: method, body: JSON.stringify(json), }; } else { return { headers: defaultHeaders(apikey), method: 'GET', }; } } export function defaultUrlMsg(accid) { return `https://graph.facebook.com/v18.0/${accid}/messages`; } async function defaultFetch(auth, content) { return fetch(defaultUrlMsg(auth.accid), payload(auth.apikey, content)); } export async function pinRegister(auth, pin) { return fetch(`https://graph.facebook.com/v18.0/${auth.accid}/register`, payload(auth.apikey, { 'messaging_product': 'whatsapp', 'pin': pin, })); } export async function readMessage(auth, msgid) { let content = { 'messaging_product': 'whatsapp', 'status': 'read', 'message_id': msgid, }; return defaultFetch(auth, content); } export async function sendMessage(auth, message) { return defaultFetch(auth, message); } export async function sendMessageMultiPart(auth, waid, texto) { if (!texto) { return; } let msgFinal = []; if (texto.length > 600) { msgFinal = texto.match(/.{1,600}/g); } else { msgFinal.push(texto); } for (let k = 0; k < msgFinal.length; k = k + 1) { await sendMessage(auth, { messaging_product: 'whatsapp', recipient_type: 'individual', to: waid, type: 'text', text: { body: msgFinal[k], preview_url: true, }, }); } } export async function sendTemplate(auth, waid, namespace, param = null) { let msgTmpl = { name: namespace, language: { code: 'pt_BR' }, }; if (param) { msgTmpl['components'] = [ { type: 'body', parameters: [{ type: 'text', text: param, }], }, ]; } let content = { messaging_product: 'whatsapp', to: waid, type: 'template', template: msgTmpl, }; return defaultFetch(auth, content); } export function challenge(VERIFY_TOKEN, request) { if (request.method === 'GET') { let query = new URL(request.url).searchParams; if (query.get('hub.mode') === 'subscribe' && query.get('hub.verify_token').startsWith(VERIFY_TOKEN)) { return new Response(query.get('hub.challenge'), { status: 200 }); } } return new Response('404 Not Found', { status: 404 }); } export async function getMediaURL(apikey, id) { return await (await fetch(`https://graph.facebook.com/v18.0/${id}/`, payload(apikey))).json(); } export async function postMedia(auth, buffer) { let url = `https://graph.facebook.com/v18.0/${auth.accid}/media/`; let form = new FormData(); form.set('type', 'image/*'); form.set('messaging_product', 'whatsapp'); form.set('file', buffer); //, new Date().getMilliseconds() + ".webp;type=image/webp"); let imageStore = await fetch(url, { headers: { Authorization: `Bearer ${auth.apikey}`, }, method: 'POST', body: form, }); return await imageStore.json(); } export async function getMedia(apikey, url) { return await (await fetch(url, { headers: { Authorization: `Bearer ${apikey}`, 'User-Agent': 'PostmanRuntime/7.26.8', }, method: 'GET', })).blob(); } export async function sendOptions(auth, waid, body, ...options) { let content = { recipient_type: 'individual', messaging_product: 'whatsapp', to: waid, type: 'interactive', interactive: { type: 'button', body: { text: body, }, action: { buttons: options.map((item, index) => { return { type: 'reply', reply: { id: `${index}`, title: item, }, }; }), }, }, }; return defaultFetch(auth, content); } export async function sendMenu(auth, waid, menu) { let msgInteractive = { type: 'list', header: null, body: null, footer: null, action: { button: (menu.botao) ? menu.botao : 'Menu', sections: [{ rows: menu.itens.map((item, index) => { let prod = item; if (typeof item === 'string') { prod = { description: item, }; } const id = (prod.id) ? prod.id : `${index + 1}`; const title = (prod.title) ? prod.title : id; const description = (prod.description) ? prod.description : null; return { id, title, description, }; }), }], }, }; if (menu.mensagem) { msgInteractive.body = { text: menu.mensagem, }; } if (menu.rodape) { msgInteractive.footer = { text: menu.rodape, }; } let content = { recipient_type: 'individual', messaging_product: 'whatsapp', to: waid, type: 'interactive', interactive: msgInteractive, }; return defaultFetch(auth, content); } export function templateGeneric(telefone, templ, namespace = null, ...args) { const ret = { recipient_type: "individual", messaging_product: "whatsapp", to: telefone, type: "template", template: { namespace: namespace, name: templ, language: { code: "pt_BR" }, components: [ { type: 'body', parameters: [] }, ] } }; if (!namespace) { delete ret.template.namespace; } if (!args || args.length === 0) { delete ret.template.components; } else { ret.template.components[0].parameters = args.map(item => { return { type: 'text', text: item, }; }); } return ret; }