From f44dc083bdf6cdc11f45430ed4e0463b98841a01 Mon Sep 17 00:00:00 2001 From: jonalan Date: Wed, 17 Mar 2021 10:32:24 -0300 Subject: [PATCH] feat: check values sendImageFromBase64 - ts --- src/api/helpers/layers-interface.ts | 2 +- src/api/layers/sender.layer.ts | 135 ++++++++++++++++++---------- 2 files changed, 87 insertions(+), 50 deletions(-) diff --git a/src/api/helpers/layers-interface.ts b/src/api/helpers/layers-interface.ts index 5801daa13..bf48aa72e 100644 --- a/src/api/helpers/layers-interface.ts +++ b/src/api/helpers/layers-interface.ts @@ -13,7 +13,7 @@ export function checkValuesSender(data: any) { let result = data[i].type && data[i].value && infoType(data[i].value, data[i].type) ? true - : false; + : !data[i].value && data[i].isUser === false ? true : false; if (!result) { return { erro: true, diff --git a/src/api/layers/sender.layer.ts b/src/api/layers/sender.layer.ts index 87f1d46b8..64b6706f5 100644 --- a/src/api/layers/sender.layer.ts +++ b/src/api/layers/sender.layer.ts @@ -95,12 +95,14 @@ export class SenderLayer extends ListenerLayer { type: type, value: to, function: typeFunction, + isUser: true, }, { param: 'content', type: type, value: content, function: typeFunction, + isUser: true, }, ]; const validating = checkValuesSender(check); @@ -141,18 +143,21 @@ export class SenderLayer extends ListenerLayer { type: type, value: chatId, function: typeFunction, + isUser: true, }, { param: 'url', type: type, value: url, function: typeFunction, + isUser: true, }, { param: 'title', type: type, value: title, function: typeFunction, + isUser: false, }, ]; const validating = checkValuesSender(check); @@ -173,6 +178,87 @@ export class SenderLayer extends ListenerLayer { }); } + /** + * Sends image message + * @param to Chat id + * @param base64 File path, http link or base64Encoded + * @param filename + * @param caption + */ + public async sendImageFromBase64( + to: string, + base64: string, + filename?: string, + caption?: string + ): Promise { + return new Promise(async (resolve, reject) => { + const typeFunction = 'sendImageFromBase64'; + const type = 'string'; + const check = [ + { + param: 'to', + type: type, + value: to, + function: typeFunction, + isUser: true, + }, + { + param: 'base64', + type: type, + value: base64, + function: typeFunction, + isUser: true, + }, + { + param: 'filename', + type: type, + value: filename, + function: typeFunction, + isUser: false, + }, + ]; + const validating = checkValuesSender(check); + if (typeof validating === 'object') { + return reject(validating); + } + + let mimeType = base64MimeType(base64); + + if (!mimeType) { + const obj = { + erro: true, + to: to, + text: 'Invalid base64!', + }; + return reject(obj); + } + + if (!mimeType.includes('image')) { + const obj = { + erro: true, + to: to, + text: 'Not an image, allowed formats png, jpeg and webp', + }; + return reject(obj); + } + + filename = filenameFromMimeType(filename, mimeType); + + const result = await this.page.evaluate( + ({ to, base64, filename, caption }) => { + return WAPI.sendImage(base64, to, filename, caption); + }, + { to, base64, filename, caption } + ); + if (result['erro'] == true) { + reject(result); + } else { + resolve(result); + } + }); + } + + public async sendMessageOptions( chat: any, content: any, @@ -242,55 +328,6 @@ export class SenderLayer extends ListenerLayer { }); } - /** - * Sends image message - * @param to Chat id - * @param base64 File path, http link or base64Encoded - * @param filename - * @param caption - */ - public async sendImageFromBase64( - to: string, - base64: string, - filename: string, - caption?: string - ): Promise { - return new Promise(async (resolve, reject) => { - let mimeType = base64MimeType(base64); - - if (!mimeType) { - const obj = { - erro: true, - to: to, - text: 'Invalid base64!', - }; - return reject(obj); - } - - if (!mimeType.includes('image')) { - const obj = { - erro: true, - to: to, - text: 'Not an image, allowed formats png, jpeg and webp', - }; - return reject(obj); - } - - filename = filenameFromMimeType(filename, mimeType); - - const result = await this.page.evaluate( - ({ to, base64, filename, caption }) => { - return WAPI.sendImage(base64, to, filename, caption); - }, - { to, base64, filename, caption } - ); - if (result['erro'] == true) { - reject(result); - } else { - resolve(result); - } - }); - } /** * Sends message with thumbnail