From ae38c8e38c6c8b731b0ceda008c9224b497f42fd Mon Sep 17 00:00:00 2001 From: Edgard Date: Wed, 24 Feb 2021 14:35:03 -0300 Subject: [PATCH] feat: Added sendPtt from file --- src/api/layers/sender.layer.ts | 51 +++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/api/layers/sender.layer.ts b/src/api/layers/sender.layer.ts index 706128ed5..7da2aa07a 100644 --- a/src/api/layers/sender.layer.ts +++ b/src/api/layers/sender.layer.ts @@ -270,7 +270,7 @@ export class SenderLayer extends ListenerLayer { } /** - * Sends file + * Sends ptt audio * base64 parameter should have mime type already defined * @param to Chat id * @param base64 base64 data @@ -291,6 +291,55 @@ export class SenderLayer extends ListenerLayer { ); } + /** + * Sends ptt audio from path + * @param to Chat id + * @param filePath File path + * @param filename + * @param caption + */ + public async sendPtt( + to: string, + filePath: string, + filename?: string, + caption?: string + ) { + return new Promise(async (resolve, reject) => { + let base64 = await downloadFileToBase64(filePath, [ + 'audio/3gpp', + 'audio/3gpp2', + 'audio/aac', + 'audio/midi', + 'audio/mpeg', + 'audio/ogg', + 'audio/webm', + 'audio/x-wav', + ]), + obj: { erro: boolean; to: string; text: string }; + + if (!base64) { + base64 = await fileToBase64(filePath); + } + + if (!base64) { + obj = { + erro: true, + to: to, + text: 'No such file or directory, open "' + filePath + '"', + }; + return reject(obj); + } + + if (!filename) { + filename = path.basename(filePath); + } + + return this.sendPttFromBase64(to, base64, filename, caption) + .then(resolve) + .catch(reject); + }); + } + /** * Sends file * base64 parameter should have mime type already defined