Skip to content

Commit

Permalink
feat: Send Status
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Feb 9, 2021
1 parent 72e24d3 commit 66af91d
Show file tree
Hide file tree
Showing 11 changed files with 360 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/api/layers/controls.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,8 @@ export class ControlsLayer extends UILayer {
{ chatId, option }
);
}

public async isInsideChat() {
return this.page.evaluate(() => WAPI.isInsideChat());
}
}
17 changes: 17 additions & 0 deletions src/api/layers/profile.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ export class ProfileLayer extends HostLayer {
});
}

/**
* Send status text
* @param text The text for the status
*/
public async sendStatusText(text: string) {
return new Promise(async (resolve, reject) => {
const result = await this.page.evaluate((text) => {
return WAPI.sendStatusText(text);
}, text);
if (result['erro'] == true) {
reject(result);
} else {
resolve(result);
}
});
}

/**
* Change the theme
* @param string types "dark" or "light"
Expand Down
119 changes: 113 additions & 6 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/

import { type } from 'os';
import * as path from 'path';
import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
Expand Down Expand Up @@ -152,6 +154,103 @@ export class SenderLayer extends ListenerLayer {
});
}

/**
* Sends video status
* @param filePath File path or http link
* @param caption Text video
*/
public async sendStatusVideo(
filePath: string,
caption?: string
): Promise<SendFileResult> {
return new Promise(async (resolve, reject) => {
let base64 = await downloadFileToBase64(filePath, ['video/mp4']);

let to = undefined,
filename = undefined,
type = 'sendStatusVideo',
status = true;

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

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

if (!filename) {
filename = path.basename(filePath);
}

let mimeType = base64MimeType(base64);

if (!mimeType) {
const obj = {
erro: true,
to: to,
text: 'Invalid base64!',
};
return reject(obj);
}

if (!mimeType.includes('video')) {
const obj = {
erro: true,
to: to,
text: 'Not an video, allowed formats mp4',
};
return reject(obj);
}

filename = filenameFromMimeType(filename, mimeType);

const result = await this.page.evaluate(
({ to, base64, filename, caption, type, status }) => {
return WAPI.sendFile(base64, to, filename, caption, type, status);
},
{ to, base64, filename, caption, type, status }
);
if (result['erro'] == true) {
reject(result);
} else {
resolve(result);
}
});
}

/**
* Sends image status
* @param filePath File path or http link
* @param text
*/
public async sendStatusImg(
filePath: string,
text?: string
): Promise<SendFileResult> {
return new Promise(async (resolve, reject) => {
return await this.sendImage(
null,
filePath,
null,
text,
'sendImgStatus',
true
)
.then((r) => {
resolve(r);
})
.catch((r) => {
reject(r);
});
});
}

/**
* Sends image message
* @param to Chat id
Expand All @@ -163,7 +262,9 @@ export class SenderLayer extends ListenerLayer {
to: string,
filePath: string,
filename?: string,
caption?: string
caption?: string,
type?: string,
status?: boolean
): Promise<SendFileResult> {
return new Promise(async (resolve, reject) => {
let base64 = await downloadFileToBase64(filePath, [
Expand Down Expand Up @@ -191,7 +292,11 @@ export class SenderLayer extends ListenerLayer {
filename = path.basename(filePath);
}

this.sendImageFromBase64(to, base64, filename, caption)
if (!status) {
type = 'sendImage';
}

this.sendImageFromBase64(to, base64, filename, caption, type, status)
.then(resolve)
.catch(reject);
});
Expand All @@ -208,7 +313,9 @@ export class SenderLayer extends ListenerLayer {
to: string,
base64: string,
filename: string,
caption?: string
caption?: string,
type?: string,
status?: boolean
): Promise<SendFileResult> {
return new Promise(async (resolve, reject) => {
let mimeType = base64MimeType(base64);
Expand All @@ -234,10 +341,10 @@ export class SenderLayer extends ListenerLayer {
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, type, status }) => {
return WAPI.sendImage(base64, to, filename, caption, type, status);
},
{ to, base64, filename, caption }
{ to, base64, filename, caption, type, status }
);
if (result['erro'] == true) {
reject(result);
Expand Down
78 changes: 78 additions & 0 deletions src/lib/wapi/functions/await-inside-chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mMMMMMMMMMNNNmmNNNMMNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mmNMMNMMMMNNNNNmmmddhdddNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mddNMMNy:/odNmmddmmNNmdhhddmNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
NmmdNMNd:--+dNmmddhhddmmhsyhhmdmmNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
NmNmdNmy:.-oyNmmmhmdhho+sososyhhhddNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM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MNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/

export async function isInsideChat() {
let err;
do {
try {
await new Promise((r) => setTimeout(r, 2000));
if (
(document.getElementsByClassName('app')[0] &&
document.getElementsByClassName('app')[0].attributes &&
!!document.getElementsByClassName('app')[0].attributes.tabindex) ||
(document.getElementsByClassName('two')[0] &&
document.getElementsByClassName('two')[0].attributes &&
!!document.getElementsByClassName('two')[0].attributes.tabindex)
) {
return true;
} else {
throw 1;
err = false;
}
} catch (e) {
err = true;
}
} while (err);
}
2 changes: 2 additions & 0 deletions src/lib/wapi/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,5 @@ export { getSessionTokenBrowser } from './get-session-token';
export { sendMute } from './send-mute';
export { getListMute, interfaceMute } from './get-list-mute';
export { downloadMedia } from './download-media';
export { sendStatusText } from './send-status-text';
export { isInsideChat } from './await-inside-chat';
6 changes: 6 additions & 0 deletions src/lib/wapi/functions/process-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ export async function processFiles(chat, blobs) {
if (!Array.isArray(blobs)) {
blobs = [blobs];
}

const mediaCollection = new Store.MediaCollection({
chatParticipantCount: chat.getParticipantCount(),
});

const mediaCollection = new Store.MediaCollection(chat);

await mediaCollection.processFiles(
Debug.VERSION === '0.4.613'
? blobs
Expand All @@ -71,5 +74,8 @@ export async function processFiles(chat, blobs) {
chat,
1
);

await mediaCollection.processFiles(blobs, chat, 1);

return mediaCollection;
}
42 changes: 33 additions & 9 deletions src/lib/wapi/functions/send-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { processFiles } from './process-files';
import { base64ToFile } from '../helper';

export async function sendFile(imgBase64, chatid, filename, caption, type) {
export async function sendFile(
imgBase64,
chatid,
filename,
caption,
type,
status
) {
type = type ? type : 'sendFile';

if (
Expand All @@ -69,14 +74,33 @@ export async function sendFile(imgBase64, chatid, filename, caption, type) {
if (mime && mime.length) {
mime = mime[1];
}
var chat = await WAPI.sendExist(chatid);

let SendLocal = atob('c3RhdHVzQGJyb2FkY2FzdA=='),
mediaBlob = WAPI.base64ToFile(imgBase64, filename),
chat = {};
if (status != true) {
chat = await WAPI.sendExist(chatid);
}
if (!chat.erro) {
var mediaBlob = base64ToFile(imgBase64, filename),
mediaCollection = await processFiles(chat, mediaBlob),
media = mediaCollection.models[0];
var result = (await media.sendToChat(chat, { caption: caption })) || '';
var m = { type: type, filename: filename, text: caption, mimeType: mime },
let result = false,
m = { type: type, filename: filename, text: caption, mimeType: mime },
To = null;

if (status === true) {
const idUser = new Store.WidFactory.createWid(SendLocal);
await Store.Chat.find(idUser).then(async (chat) => {
await WAPI.processFiles(chat, mediaBlob).then(
async (mediaCollection) => {
var media = mediaCollection.models[0];
result = (await media.sendToChat(chat, { caption: caption })) || '';
}
);
});
} else {
let mediaCollection = await WAPI.processFiles(chat, mediaBlob),
media = mediaCollection.models[0];
To = await WAPI.getchatId(chat.id);
}
if (result === 'success' || result === 'OK') {
var obj = WAPI.scope(To, false, result, null);
Object.assign(obj, m);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/wapi/functions/send-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ import { sendFile } from './send-file';
* @param {string} caption
* @param {Function} done Optional callback
*/
export function sendImage(imgBase64, chatid, filename, caption) {
return sendFile(imgBase64, chatid, filename, caption, 'sendImage');
export function sendImage(imgBase64, chatid, filename, caption, stype, status) {
return sendFile(imgBase64, chatid, filename, caption, stype, status);
}
Loading

0 comments on commit 66af91d

Please sign in to comment.