From ba8edc8d40992f4a28a5fc6e2dba4b2f210cf033 Mon Sep 17 00:00:00 2001 From: "Pedro S. Lopez" Date: Sat, 1 Feb 2020 21:16:03 -0400 Subject: [PATCH] [+] Expose information about the logged in client (#19) --- example.js | 9 +++++++++ index.js | 3 ++- src/Client.js | 10 ++++++++-- src/structures/ClientInfo.js | 27 +++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/structures/ClientInfo.js diff --git a/example.js b/example.js index 6e46f26..be7c1a2 100644 --- a/example.js +++ b/example.js @@ -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(` diff --git a/index.js b/index.js index aa31ec8..8ce52f8 100644 --- a/index.js +++ b/index.js @@ -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') }; \ No newline at end of file diff --git a/src/Client.js b/src/Client.js index 1788b58..118cd4f 100644 --- a/src/Client.js +++ b/src/Client.js @@ -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; diff --git a/src/structures/ClientInfo.js b/src/structures/ClientInfo.js new file mode 100644 index 0000000..dfe6432 --- /dev/null +++ b/src/structures/ClientInfo.js @@ -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; \ No newline at end of file