[+] Expose information about the logged in client (#19)

This commit is contained in:
Pedro S. Lopez
2020-02-01 21:16:03 -04:00
committed by GitHub
parent 88c56b1371
commit ba8edc8d40
4 changed files with 46 additions and 3 deletions

View File

@@ -81,6 +81,15 @@ 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 == '!info') {
let info = client.info;
client.sendMessage(msg.from, `
*Connection info*
User name: ${info.pushname}
My number: ${info.me.user}
Platform: ${info.platform}
WhatsApp version: ${info.phone.wa_version}
`);
} else if(msg.body == '!mediainfo' && msg.hasMedia) {
const attachmentData = await msg.downloadMedia();
msg.reply(`

View File

@@ -9,5 +9,6 @@ module.exports = {
Chat: require('./src/structures/Chat'),
PrivateChat: require('./src/structures/PrivateChat'),
GroupChat: require('./src/structures/GroupChat'),
Message: require('./src/structures/Message')
Message: require('./src/structures/Message'),
ClientInfo: require('./src/structures/ClientInfo')
};

View File

@@ -9,6 +9,7 @@ const Util = require('./util/Util');
const { WhatsWebURL, UserAgent, DefaultOptions, Events, WAState } = require('./util/Constants');
const { ExposeStore, LoadUtils } = require('./util/Injected');
const ChatFactory = require('./factories/ChatFactory');
const ClientInfo = require('./structures/ClientInfo');
const Message = require('./structures/Message');
/**
@@ -98,6 +99,11 @@ class Client extends EventEmitter {
//Load util functions (serializers, helper functions)
await page.evaluate(LoadUtils);
// Expose client info
this.info = new ClientInfo(this, await page.evaluate(() => {
return Store.Conn.serialize();
}));
// Register events
await page.exposeFunction('onAddMessageEvent', msg => {
if (!msg.isNewMsg) return;
@@ -115,12 +121,12 @@ class Client extends EventEmitter {
this.emit(Events.DISCONNECTED);
this.destroy();
}
})
});
await page.evaluate(() => {
Store.Msg.on('add', onAddMessageEvent);
Store.AppState.on('change:state', onAppStateChangedEvent);
}).catch(err => console.log(err.message));
});
this.pupBrowser = browser;
this.pupPage = page;

View File

@@ -0,0 +1,27 @@
'use strict';
const Base = require('./Base');
/**
* Current connection information
* @extends {Base}
*/
class ClientInfo extends Base {
constructor(client, data) {
super(client);
if(data) this._patch(data);
}
_patch(data) {
this.pushname = data.pushname;
this.me = data.me;
this.phone = data.phone;
this.platform = data.platform;
return super._patch(data);
}
}
module.exports = ClientInfo;