This commit is contained in:
Pedro Lopez
2020-02-22 17:28:15 -04:00
5 changed files with 86 additions and 52 deletions

View File

@@ -86,9 +86,9 @@ class Client extends EventEmitter {
// Wait for QR Code
const QR_CANVAS_SELECTOR = 'canvas';
await page.waitForSelector(QR_CANVAS_SELECTOR);
const qrImgData = await page.$eval(QR_CANVAS_SELECTOR, canvas => [].slice.call(canvas.getContext('2d').getImageData(0,0,264,264).data));
const qrImgData = await page.$eval(QR_CANVAS_SELECTOR, canvas => [].slice.call(canvas.getContext('2d').getImageData(0, 0, 264, 264).data));
const qr = jsQR(qrImgData, 264, 264).data;
/**
* Emitted when the QR code is received
* @event Client#qr
@@ -162,7 +162,7 @@ class Client extends EventEmitter {
if (msg.type === 'revoked') {
const message = new Message(this, msg);
let revoked_msg;
if(last_message && msg.id.id === last_message.id.id) {
if (last_message && msg.id.id === last_message.id.id) {
revoked_msg = new Message(this, last_message);
}
@@ -175,11 +175,11 @@ class Client extends EventEmitter {
*/
this.emit(Events.MESSAGE_REVOKED_EVERYONE, message, revoked_msg);
}
});
await page.exposeFunction('onChangeMessageEvent', (msg) => {
if (msg.type !== 'revoked') {
last_message = msg;
}
@@ -254,32 +254,49 @@ class Client extends EventEmitter {
* @param {object} options
* @returns {Promise<Message>} Message that was just sent
*/
async sendMessage(chatId, content, options={}) {
async sendMessage(chatId, content, options = {}) {
let internalOptions = {
caption: options.caption,
quotedMessageId: options.quotedMessageId,
mentionedJidList: Array.isArray(options.mentions) ? options.mentions.map(contact => contact.id._serialized) : []
mentionedJidList: Array.isArray(options.mentions) ? options.mentions.map(contact => contact.id._serialized) : []
};
if(content instanceof MessageMedia) {
if (content instanceof MessageMedia) {
internalOptions.attachment = content;
content = '';
} else if(options.media instanceof MessageMedia) {
} else if (options.media instanceof MessageMedia) {
internalOptions.attachment = options.media;
internalOptions.caption = content;
} else if(content instanceof Location) {
} else if (content instanceof Location) {
internalOptions.location = content;
content = '';
}
const newMessage = await this.pupPage.evaluate(async (chatId, message, options) => {
const msg = await window.WWebJS.sendMessage(window.Store.Chat.get(chatId), message, options);
let chat = window.Store.Chat.get(chatId);
let msg;
if (!chat) { // The chat is not available in the previously chatted list
let newChatId = await window.WWebJS.getNumberId(chatId);
if (newChatId) {
//get the topmost chat object and assign the new chatId to it .
//This is just a workaround.May cause problem if there are no chats at all. Need to dig in and emulate how whatsapp web does
let chat = window.Store.Chat.models[0];
let originalChatObjId = chat.id;
chat.id = newChatId;
msg = await window.WWebJS.sendMessage(chat, message, options);
chat.id = originalChatObjId; //replace the chat with its original id
}
}
else
msg = await window.WWebJS.sendMessage(chat, message, options);
return msg.serialize();
}, chatId, content, internalOptions);
return new Message(this, newMessage);
}
/**
* Get all current chat instances
* @returns {Promise<Array<Chat>>}

View File

@@ -20,12 +20,20 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.MediaUpload = window.mR.findModule('uploadMedia')[0];
window.Store.Cmd = window.mR.findModule('Cmd')[0].default;
window.Store.MediaTypes = window.mR.findModule('msgToMediaType')[0];
window.Store.UserConstructor = window.mR.findModule((module) => (module.default && module.default.prototype && module.default.prototype.isServer && module.default.prototype.isUser) ? module.default : null)[0].default;
};
exports.LoadUtils = () => {
window.WWebJS = {};
window.WWebJS.getNumberId = async (id) => {
window.WWebJS.sendMessage = async (chat, content, options={}) => {
let result = await window.Store.Wap.queryExist(id);
if (result.jid === undefined)
throw 'The number provided is not a registered whatsapp user';
return result.jid;
};
window.WWebJS.sendMessage = async (chat, content, options = {}) => {
let attOptions = {};
if (options.attachment) {
attOptions = await window.WWebJS.processMediaData(options.attachment);
@@ -35,27 +43,27 @@ exports.LoadUtils = () => {
let quotedMsgOptions = {};
if (options.quotedMessageId) {
let quotedMessage = window.Store.Msg.get(options.quotedMessageId);
if(quotedMessage.canReply()) {
if (quotedMessage.canReply()) {
quotedMsgOptions = quotedMessage.msgContextInfo(chat);
}
delete options.quotedMessageId;
}
if (options.mentionedJidList) {
options.mentionedJidList = options.mentionedJidList.map(cId=> window.Store.Contact.get(cId).id);
options.mentionedJidList = options.mentionedJidList.map(cId => window.Store.Contact.get(cId).id);
}
let locationOptions = {};
if (options.location) {
locationOptions = {
type: 'location',
loc: options.location.description,
lat: options.location.latitude,
loc: options.location.description,
lat: options.location.latitude,
lng: options.location.longitude
};
delete options.location;
}
const newMsgId = new window.Store.MsgKey({
from: window.Store.Conn.me,
to: chat.id,
@@ -95,9 +103,9 @@ exports.LoadUtils = () => {
isGif: mediaData.isGif
});
if(!(mediaData.mediaBlob instanceof window.Store.OpaqueData.default)) {
if (!(mediaData.mediaBlob instanceof window.Store.OpaqueData.default)) {
mediaData.mediaBlob = await window.Store.OpaqueData.default.createFromData(mediaData.mediaBlob, mediaData.mediaBlob.type);
}
}
mediaData.renderableUrl = mediaData.mediaBlob.url();
mediaObject.consolidate(mediaData.toJSON());
@@ -162,7 +170,7 @@ exports.LoadUtils = () => {
return res;
};
window.WWebJS.getContact = contactId => {
const contact = window.Store.Contact.get(contactId);
return window.WWebJS.getContactModel(contact);
@@ -173,16 +181,16 @@ exports.LoadUtils = () => {
return contacts.map(contact => window.WWebJS.getContactModel(contact));
};
window.WWebJS.mediaInfoToFile = ({data, mimetype, filename}) => {
window.WWebJS.mediaInfoToFile = ({ data, mimetype, filename }) => {
const binaryData = atob(data);
const buffer = new ArrayBuffer(binaryData.length);
const view = new Uint8Array(buffer);
for(let i=0; i < binaryData.length; i++) {
for (let i = 0; i < binaryData.length; i++) {
view[i] = binaryData.charCodeAt(i);
}
const blob = new Blob([buffer], {type: mimetype});
const blob = new Blob([buffer], { type: mimetype });
return new File([blob], filename, {
type: mimetype,
lastModified: Date.now()
@@ -190,7 +198,7 @@ exports.LoadUtils = () => {
};
window.WWebJS.downloadBuffer = (url) => {
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
@@ -217,13 +225,13 @@ exports.LoadUtils = () => {
window.WWebJS.readBlobAsync = (blob) => {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsDataURL(blob);
});
};