Implemented changes mentioned in SimiPrambos PR#28 (#52)

* Implemented changes mentioned in SimiPrambos PR#28
To reflect new code structure
This commit is contained in:
sahalMoidu
2020-02-15 03:23:28 +05:30
committed by GitHub
parent 9891d85b35
commit 6ade08e814
3 changed files with 74 additions and 50 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,45 @@ 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
//todo : Check if the number is a whatsapp enabled. Whatsapp web sends query exists via ws.
chat = window.Store.Chat.models[0]; //get the topmost chat object and assign the new chatId to it
let originalChatObjId = chat.id;
chat.id = typeof originalChatObjId === 'string' ? chatId : new window.Store.UserConstructor(chatId, { intentionallyUsePrivateConstructor: true });
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>>}