mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-18 03:29:14 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9043e61c40 | ||
|
|
bea1ebf480 | ||
|
|
021a213de3 | ||
|
|
d0bef55b8a | ||
|
|
64912dccfd |
@@ -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}
|
||||
`);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
7
package-lock.json
generated
7
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "whatsapp-web.js",
|
||||
"version": "0.2.0",
|
||||
"version": "0.3.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@@ -168,6 +168,11 @@
|
||||
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
|
||||
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
|
||||
},
|
||||
"jsqr": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/jsqr/-/jsqr-1.2.0.tgz",
|
||||
"integrity": "sha512-wKcQS9QC2VHGk7aphWCp1RrFyC0CM6fMgC5prZZ2KV/Lk6OKNoCod9IR6bao+yx3KPY0gZFC5dc+h+KFzCI0Wg=="
|
||||
},
|
||||
"mime": {
|
||||
"version": "2.4.4",
|
||||
"resolved": "https://registry.npmjs.org/mime/-/mime-2.4.4.tgz",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "whatsapp-web.js",
|
||||
"version": "0.2.0",
|
||||
"version": "0.3.1",
|
||||
"description": "Library for interacting with the WhatsApp Web API ",
|
||||
"main": "./index.js",
|
||||
"scripts": {
|
||||
@@ -25,6 +25,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/pedroslopez/whatsapp-web.js#readme",
|
||||
"dependencies": {
|
||||
"jsqr": "^1.2.0",
|
||||
"moduleraid": "git+https://github.com/pixeldesu/moduleRaid.git",
|
||||
"puppeteer": "^1.20.0"
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
const EventEmitter = require('events');
|
||||
const puppeteer = require('puppeteer');
|
||||
const moduleRaid = require('moduleraid/moduleraid');
|
||||
const jsQR = require('jsqr');
|
||||
|
||||
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');
|
||||
|
||||
/**
|
||||
@@ -65,12 +65,11 @@ class Client extends EventEmitter {
|
||||
|
||||
} else {
|
||||
// Wait for QR Code
|
||||
const QR_CONTAINER_SELECTOR = '._2d3Jz';
|
||||
const QR_VALUE_SELECTOR = '._1pw2F';
|
||||
|
||||
await page.waitForSelector(QR_CONTAINER_SELECTOR);
|
||||
|
||||
const qr = await page.$eval(QR_VALUE_SELECTOR, node => node.getAttribute('data-ref'));
|
||||
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 qr = jsQR(qrImgData, 264, 264).data;
|
||||
|
||||
this.emit(Events.QR_RECEIVED, qr);
|
||||
|
||||
// Wait for code scan
|
||||
@@ -96,8 +95,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 => {
|
||||
|
||||
@@ -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;
|
||||
@@ -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 = () => {
|
||||
|
||||
Reference in New Issue
Block a user