From bcf65625e4e017f474106e056a95f51a9ac419d3 Mon Sep 17 00:00:00 2001 From: DaviSM Date: Thu, 28 Dec 2023 22:57:30 -0300 Subject: [PATCH] avancando versao e fix --- package.json | 2 +- src/index.ts | 378 +++++++++++++++++++++++++-------------------------- 2 files changed, 190 insertions(+), 190 deletions(-) diff --git a/package.json b/package.json index 570c561..bcf102d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "business-whatsapp", "author": "frkr", "license": "MIT", - "version": "18.0.23", + "version": "18.1.1", "description": "WhatsApp Business API Client unofficial", "homepage": "https://ideias.casa/", "repository": { diff --git a/src/index.ts b/src/index.ts index 2cdfbdc..30a9df5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,243 +1,243 @@ export interface WAAuth { - accid: string; - apikey: string; + accid: string; + apikey: string; } export function onlyNumbers(waid: string): string { - return waid.replace(/[^0-9]/g, ''); + return waid.replace(/[^0-9]/g, ''); } export function defaultHeaders(apikey: string) { - return { - Authorization: `Bearer ${apikey}`, - 'Content-Type': 'application/json', - 'Accept': 'application/json', - }; + return { + Authorization: `Bearer ${apikey}`, + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }; } export function payload(apikey: string, json: object = null, method = 'POST'): any { - if (json) { - return { - headers: defaultHeaders(apikey), - method: method, - body: JSON.stringify(json), - }; - } else { - return { - headers: defaultHeaders(apikey), - method: 'GET', - }; - } + if (json) { + return { + headers: defaultHeaders(apikey), + method: method, + body: JSON.stringify(json), + }; + } else { + return { + headers: defaultHeaders(apikey), + method: 'GET', + }; + } } export function defaultUrlMsg(accid: string): string { - return `https://graph.facebook.com/v18.0/${accid}/messages`; + return `https://graph.facebook.com/v18.0/${accid}/messages`; } async function defaultFetch(auth: WAAuth, content: object): Promise { - return fetch(defaultUrlMsg(auth.accid), payload(auth.apikey, content)); + return fetch(defaultUrlMsg(auth.accid), payload(auth.apikey, content)); } export async function pinRegister(auth: WAAuth, pin: string): Promise { - return fetch( - `https://graph.facebook.com/v18.0/${auth.accid}/register`, - payload(auth.apikey, { - 'messaging_product': 'whatsapp', - 'pin': 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: WAAuth, msgid: string): Promise { - let content = { - 'messaging_product': 'whatsapp', - 'status': 'read', - 'message_id': msgid, - }; - return defaultFetch(auth, content); + let content = { + 'messaging_product': 'whatsapp', + 'status': 'read', + 'message_id': msgid, + }; + return defaultFetch(auth, content); } export async function sendMessage(auth: WAAuth, message: MessageObjectRequest) { - return defaultFetch(auth, message); + return defaultFetch(auth, message); } export async function sendMessageMultiPart(auth: WAAuth, waid: string, texto: string): Promise { - if (!texto) { - return; - } - let msgFinal = []; - if (texto.length > 600) { - msgFinal = texto.split(/.{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, - }, - }); - } + 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: WAAuth, waid: string, namespace: string, param: string = null): Promise { - 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, - } as MessageObjectRequest; - - return defaultFetch(auth, content); + 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, + } as MessageObjectRequest; + + return defaultFetch(auth, content); } export function challenge(VERIFY_TOKEN: string, request: Request): Response { - 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 }); + 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: string, id: string): Promise { - return await (await fetch(`https://graph.facebook.com/v18.0/${id}/`, payload(apikey))).json(); + return await (await fetch(`https://graph.facebook.com/v18.0/${id}/`, payload(apikey))).json(); } export async function postMedia(auth: WAAuth, buffer: Blob): Promise { - let url = `https://graph.facebook.com/v18.0/${auth.accid}/media/`; + 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 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, - }); + let imageStore = await fetch(url, { + headers: { + Authorization: `Bearer ${auth.apikey}`, + }, + method: 'POST', + body: form, + }); - return await imageStore.json(); + return await imageStore.json(); } export async function getMedia(apikey: string, url: string): Promise { - return await (await fetch(url, { - headers: { - Authorization: `Bearer ${apikey}`, - 'User-Agent': 'PostmanRuntime/7.26.8', - }, - method: 'GET', - }, - )).blob(); + return await (await fetch(url, { + headers: { + Authorization: `Bearer ${apikey}`, + 'User-Agent': 'PostmanRuntime/7.26.8', + }, + method: 'GET', + }, + )).blob(); } export async function sendOptions(auth: WAAuth, waid: string, body: string, ...options: string[]): Promise { - let content: MessageObjectRequest = { - 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 as string, - } as RowsEntity, - } as ButtonEntity; - }), - }, - }, - }; - - return defaultFetch(auth, content); + let content: MessageObjectRequest = { + 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 as string, + } as RowsEntity, + } as ButtonEntity; + }), + }, + }, + }; + + return defaultFetch(auth, content); } export async function sendMenu(auth: WAAuth, waid: string, menu: MenuRequest): Promise { - let msgInteractive: InteractiveMessage = { - type: 'list', - header: null, - body: null, - footer: null, - action: { - button: (menu.botao) ? menu.botao : 'Menu', - sections: [{ - rows: menu.itens.map((item, index) => { - - let prod: RowsEntity = item; - if (typeof item === 'string') { - prod = { - description: item as string, - } as RowsEntity; - } - - 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, - } as RowsEntity; - }), - }], - }, - }; - if (menu.mensagem) { - msgInteractive.body = { - text: menu.mensagem as string, - }; - } - if (menu.rodape) { - msgInteractive.footer = { - text: menu.rodape as string, - }; - } - - let content: MessageObjectRequest = { - recipient_type: 'individual', - messaging_product: 'whatsapp', - to: waid, - type: 'interactive', - interactive: msgInteractive, - }; - - return defaultFetch(auth, content); + let msgInteractive: InteractiveMessage = { + type: 'list', + header: null, + body: null, + footer: null, + action: { + button: (menu.botao) ? menu.botao : 'Menu', + sections: [{ + rows: menu.itens.map((item, index) => { + + let prod: RowsEntity = item; + if (typeof item === 'string') { + prod = { + description: item as string, + } as RowsEntity; + } + + 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, + } as RowsEntity; + }), + }], + }, + }; + if (menu.mensagem) { + msgInteractive.body = { + text: menu.mensagem as string, + }; + } + if (menu.rodape) { + msgInteractive.footer = { + text: menu.rodape as string, + }; + } + + let content: MessageObjectRequest = { + recipient_type: 'individual', + messaging_product: 'whatsapp', + to: waid, + type: 'interactive', + interactive: msgInteractive, + }; + + return defaultFetch(auth, content); }