Compare commits

...

3 Commits

Author SHA1 Message Date
Pedro Lopez
021a213de3 0.3.0 2019-11-24 04:25:35 -04:00
Pedro S. Lopez
d0bef55b8a Merge pull request #24 from pedroslopez/development
Download message attachments (images, documents, etc)
2019-11-24 04:25:14 -04:00
Pedro Lopez
64912dccfd Download message attachments (images, documents, etc) 2019-11-24 04:23:08 -04:00
6 changed files with 76 additions and 8 deletions

View File

@@ -81,6 +81,14 @@ client.on('message', async msg => {
} else if(msg.body == '!chats') {
const chats = await client.getChats();
client.sendMessage(msg.from, `The bot has ${chats.length} chats open.`);
} else if(msg.body == '!mediainfo' && msg.hasMedia) {
const attachmentData = await msg.downloadMedia();
msg.reply(`
*Media info*
MimeType: ${attachmentData.mimetype}
Filename: ${attachmentData.filename}
Data (length): ${attachmentData.data.length}
`);
}
});

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{
"name": "whatsapp-web.js",
"version": "0.2.0",
"version": "0.3.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@@ -1,6 +1,6 @@
{
"name": "whatsapp-web.js",
"version": "0.2.0",
"version": "0.3.0",
"description": "Library for interacting with the WhatsApp Web API ",
"main": "./index.js",
"scripts": {

View File

@@ -6,7 +6,7 @@ const moduleRaid = require('moduleraid/moduleraid');
const Util = require('./util/Util');
const { WhatsWebURL, UserAgent, DefaultOptions, Events, WAState } = require('./util/Constants');
const { ExposeStore, LoadCustomSerializers } = require('./util/Injected');
const { ExposeStore, LoadUtils } = require('./util/Injected');
const ChatFactory = require('./factories/ChatFactory');
const Chat = require('./structures/Chat');
const Message = require('./structures/Message');
@@ -96,8 +96,8 @@ class Client extends EventEmitter {
// Check Store Injection
await page.waitForFunction('window.Store != undefined');
//Load custom serializers
await page.evaluate(LoadCustomSerializers);
//Load util functions (serializers, helper functions)
await page.evaluate(LoadUtils);
// Register events
await page.exposeFunction('onAddMessageEvent', msg => {

View File

@@ -15,7 +15,8 @@ class Message extends Base {
_patch(data) {
this.id = data.id;
this.body = data.body;
this.hasMedia = data.clientUrl ? true : false;
this.body = this.hasMedia ? data.caption || '' : data.body || '';
this.type = data.type;
this.timestamp = data.t;
this.from = data.from;
@@ -60,6 +61,26 @@ class Message extends Base {
}, chatId, this.id._serialized, message);
}
async downloadMedia() {
if (!this.hasMedia) {
return undefined;
}
return await this.client.pupPage.evaluate(async (msgId) => {
const msg = Store.Msg.get(msgId);
const buffer = await WWebJS.downloadBuffer(msg.clientUrl);
const decrypted = await Store.CryptoLib.decryptE2EMedia(msg.type, buffer, msg.mediaKey, msg.mimetype);
const data = await WWebJS.readBlobAsync(decrypted._blob);
return {
data,
mimetype: msg.mimetype,
filename: msg.filename
}
}, this.id._serialized);
}
}
module.exports = Message;

View File

@@ -8,12 +8,12 @@ exports.ExposeStore = (moduleRaidStr) => {
window.mR = moduleRaid();
window.Store = window.mR.findModule("Chat")[1].default;
window.Store.AppState = window.mR.findModule("STREAM")[0].default;
window.Store.CryptoLib = window.mR.findModule("decryptE2EMedia")[0];
window.Store.genId = window.mR.findModule((module) => module.default && typeof module.default === 'function' && module.default.toString().match(/crypto/))[0].default;
window.Store.SendMessage = window.mR.findModule("sendTextMsgToChat")[0].sendTextMsgToChat;
}
exports.LoadCustomSerializers = () => {
exports.LoadUtils = () => {
window.WWebJS = {};
window.WWebJS.getChatModel = chat => {
@@ -37,6 +37,45 @@ exports.LoadCustomSerializers = () => {
const chats = Store.Chat.models;
return chats.map(chat => WWebJS.getChatModel(chat));
}
window.WWebJS.downloadBuffer = (url) => {
return new Promise(function(resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
if (xhr.status == 200) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send(null);
});
}
window.WWebJS.readBlobAsync = (blob) => {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsDataURL(blob);
})
}
}
exports.MarkAllRead = () => {