Skip to content

Commit

Permalink
fix: return parameters sendVoice
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Mar 9, 2021
1 parent 34688e1 commit a7b6813
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 69 deletions.
5 changes: 5 additions & 0 deletions src/api/layers/layers-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Scope {
erro: boolean;
to: string;
text: string;
}
95 changes: 46 additions & 49 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ import {
} from '../model';
import { ChatState } from '../model/enum';
import { ListenerLayer } from './listener.layer';
import { Scope } from './layers-interface';

let obj: Scope;

export class SenderLayer extends ListenerLayer {
constructor(public page: Page, session?: string, options?: CreateConfig) {
Expand Down Expand Up @@ -303,61 +306,52 @@ export class SenderLayer extends ListenerLayer {
}

/**
* Sends file
* base64 parameter should have mime type already defined
* @param to Chat id
* @param base64 base64 data
* @param filename
* @param caption
*/
public async sendPttFromBase64(
to: string,
base64: string,
filename: string,
caption?: string
) {
return this.page.evaluate(
({ to, base64, filename, caption }) => {
WAPI.sendPtt(base64, to, filename, caption);
},
{ to, base64, filename, caption }
);
}

/**
* Sends file
* base64 parameter should have mime type already defined
* Send audio file
* @param to Chat id
* @param base64 base64 data
* @param filePath Path file
*/
public async sendVoice(to: string, filePath: string) {
let base64 = await downloadFileToBase64(filePath, ['audio/mpeg']);

if (!base64) {
base64 = await fileToBase64(filePath);
}

if (!base64) {
const obj = {
erro: true,
to: to,
text: 'No such file or directory, open "' + filePath + '"',
};
return obj;
}
return new Promise(async (resolve, reject) => {
let base64: string | false = await downloadFileToBase64(filePath, [
'audio/mpeg',
]);

path.basename(filePath);
if (!base64) {
base64 = await fileToBase64(filePath);
}

let filename = '';
if (!base64) {
obj = {
erro: true,
to: to,
text: 'No such file or directory, open "' + filePath + '"',
};
return reject(obj);
}

let caption = '';
const mimeInfo: any = base64MimeType(base64);

return this.page.evaluate(
({ to, base64, filename, caption }) => {
WAPI.sendPtt(base64, to, filename, caption);
},
{ to, base64, filename, caption }
);
if (!mimeInfo || mimeInfo.includes('audio/mpeg')) {
const result: any = await this.page.evaluate(
({ to, base64 }) => {
return WAPI.sendPtt(base64, to);
},
{ to, base64 }
);
if (result['erro'] == true) {
reject(result);
} else {
resolve(result);
}
} else {
obj = {
erro: true,
to: to,
text: 'Use the MP3 format to be able to send an audio!',
};
return reject(obj);
}
});
}

/**
Expand All @@ -378,7 +372,7 @@ export class SenderLayer extends ListenerLayer {
let mimeType = base64MimeType(base64);

if (!mimeType) {
const obj = {
obj = {
erro: true,
to: to,
text: 'Invalid base64!',
Expand Down Expand Up @@ -624,14 +618,17 @@ export class SenderLayer extends ListenerLayer {
'image/jpeg',
'image/webp',
]);

if (!b64) {
b64 = await fileToBase64(path);
}

if (b64) {
const buff = Buffer.from(
b64.replace(/^data:image\/(png|jpe?g|webp|gif);base64,/, ''),
'base64'
);

const mimeInfo = base64MimeType(b64);

if (!mimeInfo || mimeInfo.includes('image')) {
Expand Down
8 changes: 7 additions & 1 deletion src/lib/wapi/functions/get-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ export function getChat(id) {
return false;
}
id = typeof id == 'string' ? id : id._serialized;
const found = Store.Chat.get(id);
let found = Store.Chat.get(id);
if (!found) {
const ConstructChat = new window.Store.UserConstructor(id, {
intentionallyUsePrivateConstructor: !0,
});
found = Store.Chat.find(ConstructChat) || false;
}
if (found) {
found.sendMessage = found.sendMessage
? found.sendMessage
Expand Down
37 changes: 24 additions & 13 deletions src/lib/wapi/functions/send-ptt.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,30 @@ import { base64ToFile } from '../helper';
* Sends image to given chat if
* @param {string} imgBase64 base64 encoded file
* @param {string} chatid Chat id
* @param {string} filename
* @param {string} caption
* @param {Function} done Optional callback
*/
export function sendPtt(imgBase64, chatid, filename, caption, done) {
const idUser = new Store.WidFactory.createWid(chatid);
return Store.Chat.find(idUser).then((chat) => {
var mediaBlob = base64ToFile(imgBase64, filename);
processFiles(chat, mediaBlob).then((mediaCollection) => {
var media = mediaCollection.models[0];
media.mediaPrep._mediaData.type = 'ptt';
media.mediaPrep.sendToChat(chat, { caption: caption });
if (done !== undefined) done(true);
export async function sendPtt(imgBase64, chatid) {
const chat = await WAPI.sendExist(chatid);
if (chat && chat.status != 404) {
let result = await Store.Chat.find(chatid).then(async (chat) => {
var mediaBlob = base64ToFile(imgBase64);
return await processFiles(chat, mediaBlob).then(async (mc) => {
var media = mc.models[0];
media.mediaPrep._mediaData.type = 'ptt';
return await media.mediaPrep.sendToChat(chat, {});
});
});
});
const m = { type: 'sendPtt' };
let To = await WAPI.getchatId(chat.id);
if (result === 'success' || result === 'OK') {
let obj = WAPI.scope(To, false, result, null);
Object.assign(obj, m);
return obj;
} else {
let obj = WAPI.scope(To, true, result, null);
Object.assign(obj, m);
return obj;
}
} else {
return chat;
}
}
7 changes: 1 addition & 6 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,7 @@ interface WAPI {
chatId: string
) => void;
sendMute: (id: string, time: number, type: string) => Promise<object>;
sendPtt: (
base64: string,
to: string,
filename: string,
caption: string
) => any;
sendPtt: (base64: string, to: string) => any;
sendVideoAsGif: (
base64: string,
to: string,
Expand Down

0 comments on commit a7b6813

Please sign in to comment.