mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-18 03:29:14 +00:00
[+] Expose information about the logged in client (#19)
This commit is contained in:
@@ -81,6 +81,15 @@ client.on('message', async msg => {
|
|||||||
} else if(msg.body == '!chats') {
|
} else if(msg.body == '!chats') {
|
||||||
const chats = await client.getChats();
|
const chats = await client.getChats();
|
||||||
client.sendMessage(msg.from, `The bot has ${chats.length} chats open.`);
|
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) {
|
} else if(msg.body == '!mediainfo' && msg.hasMedia) {
|
||||||
const attachmentData = await msg.downloadMedia();
|
const attachmentData = await msg.downloadMedia();
|
||||||
msg.reply(`
|
msg.reply(`
|
||||||
|
|||||||
3
index.js
3
index.js
@@ -9,5 +9,6 @@ module.exports = {
|
|||||||
Chat: require('./src/structures/Chat'),
|
Chat: require('./src/structures/Chat'),
|
||||||
PrivateChat: require('./src/structures/PrivateChat'),
|
PrivateChat: require('./src/structures/PrivateChat'),
|
||||||
GroupChat: require('./src/structures/GroupChat'),
|
GroupChat: require('./src/structures/GroupChat'),
|
||||||
Message: require('./src/structures/Message')
|
Message: require('./src/structures/Message'),
|
||||||
|
ClientInfo: require('./src/structures/ClientInfo')
|
||||||
};
|
};
|
||||||
@@ -9,6 +9,7 @@ const Util = require('./util/Util');
|
|||||||
const { WhatsWebURL, UserAgent, DefaultOptions, Events, WAState } = require('./util/Constants');
|
const { WhatsWebURL, UserAgent, DefaultOptions, Events, WAState } = require('./util/Constants');
|
||||||
const { ExposeStore, LoadUtils } = require('./util/Injected');
|
const { ExposeStore, LoadUtils } = require('./util/Injected');
|
||||||
const ChatFactory = require('./factories/ChatFactory');
|
const ChatFactory = require('./factories/ChatFactory');
|
||||||
|
const ClientInfo = require('./structures/ClientInfo');
|
||||||
const Message = require('./structures/Message');
|
const Message = require('./structures/Message');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -98,6 +99,11 @@ class Client extends EventEmitter {
|
|||||||
//Load util functions (serializers, helper functions)
|
//Load util functions (serializers, helper functions)
|
||||||
await page.evaluate(LoadUtils);
|
await page.evaluate(LoadUtils);
|
||||||
|
|
||||||
|
// Expose client info
|
||||||
|
this.info = new ClientInfo(this, await page.evaluate(() => {
|
||||||
|
return Store.Conn.serialize();
|
||||||
|
}));
|
||||||
|
|
||||||
// Register events
|
// Register events
|
||||||
await page.exposeFunction('onAddMessageEvent', msg => {
|
await page.exposeFunction('onAddMessageEvent', msg => {
|
||||||
if (!msg.isNewMsg) return;
|
if (!msg.isNewMsg) return;
|
||||||
@@ -115,12 +121,12 @@ class Client extends EventEmitter {
|
|||||||
this.emit(Events.DISCONNECTED);
|
this.emit(Events.DISCONNECTED);
|
||||||
this.destroy();
|
this.destroy();
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
Store.Msg.on('add', onAddMessageEvent);
|
Store.Msg.on('add', onAddMessageEvent);
|
||||||
Store.AppState.on('change:state', onAppStateChangedEvent);
|
Store.AppState.on('change:state', onAppStateChangedEvent);
|
||||||
}).catch(err => console.log(err.message));
|
});
|
||||||
|
|
||||||
this.pupBrowser = browser;
|
this.pupBrowser = browser;
|
||||||
this.pupPage = page;
|
this.pupPage = page;
|
||||||
|
|||||||
27
src/structures/ClientInfo.js
Normal file
27
src/structures/ClientInfo.js
Normal 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;
|
||||||
Reference in New Issue
Block a user