mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-18 11:39:14 +00:00
docs: add more documentation
This commit is contained in:
@@ -17,6 +17,15 @@ const MessageMedia = require('./structures/MessageMedia');
|
||||
/**
|
||||
* Starting point for interacting with the WhatsApp Web API
|
||||
* @extends {EventEmitter}
|
||||
* @fires Client#qr
|
||||
* @fires Client#authenticated
|
||||
* @fires Client#auth_failure
|
||||
* @fires Client#ready
|
||||
* @fires Client#message
|
||||
* @fires Client#message_create
|
||||
* @fires Client#message_revoke_me
|
||||
* @fires Client#message_revoke_everyone
|
||||
* @fires Client#disconnected
|
||||
*/
|
||||
class Client extends EventEmitter {
|
||||
constructor(options = {}) {
|
||||
@@ -57,6 +66,11 @@ class Client extends EventEmitter {
|
||||
await page.waitForSelector(KEEP_PHONE_CONNECTED_IMG_SELECTOR, { timeout: 15000 });
|
||||
} catch (err) {
|
||||
if (err.name === 'TimeoutError') {
|
||||
/**
|
||||
* Emitted when there has been an error while trying to restore an existing session
|
||||
* @event Client#auth_failure
|
||||
* @param {string} message
|
||||
*/
|
||||
this.emit(Events.AUTHENTICATION_FAILURE, 'Unable to log in. Are the session details valid?');
|
||||
browser.close();
|
||||
|
||||
@@ -73,6 +87,11 @@ class Client extends EventEmitter {
|
||||
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;
|
||||
|
||||
/**
|
||||
* Emitted when the QR code is received
|
||||
* @event Client#qr
|
||||
* @param {string} qr QR Code
|
||||
*/
|
||||
this.emit(Events.QR_RECEIVED, qr);
|
||||
|
||||
// Wait for code scan
|
||||
@@ -93,6 +112,11 @@ class Client extends EventEmitter {
|
||||
WAToken2: localStorage.WAToken2
|
||||
};
|
||||
|
||||
/**
|
||||
* Emitted when authentication is successful
|
||||
* @event Client#authenticated
|
||||
* @param {object} session Object containing session information. Can be used to restore the session.
|
||||
*/
|
||||
this.emit(Events.AUTHENTICATED, session);
|
||||
|
||||
// Check window.Store Injection
|
||||
@@ -111,9 +135,21 @@ class Client extends EventEmitter {
|
||||
if (!msg.isNewMsg) return;
|
||||
|
||||
const message = new Message(this, msg);
|
||||
|
||||
/**
|
||||
* Emitted when a new message is created, which may include the current user's own messages.
|
||||
* @event Client#message_create
|
||||
* @param {Message} message The message that was created
|
||||
*/
|
||||
this.emit(Events.MESSAGE_CREATE, message);
|
||||
|
||||
if (msg.id.fromMe) return;
|
||||
|
||||
/**
|
||||
* Emitted when a new message is received.
|
||||
* @event Client#message
|
||||
* @param {Message} message The message that was received
|
||||
*/
|
||||
this.emit(Events.MESSAGE_RECEIVED, message);
|
||||
});
|
||||
|
||||
@@ -127,6 +163,14 @@ class Client extends EventEmitter {
|
||||
if(last_message && msg.id.id === last_message.id.id) {
|
||||
revoked_msg = new Message(this, last_message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted when a message is deleted for everyone in the chat.
|
||||
* @event Client#message_revoke_everyone
|
||||
* @param {Message} message The message that was revoked, in its current state. It will not contain the original message's data.
|
||||
* @param {?Message} revoked_msg The message that was revoked, before it was revoked. It will contain the message's original data.
|
||||
* Note that due to the way this data is captured, it may be possible that this param will be undefined.
|
||||
*/
|
||||
this.emit(Events.MESSAGE_REVOKED_EVERYONE, message, revoked_msg);
|
||||
}
|
||||
|
||||
@@ -145,6 +189,12 @@ class Client extends EventEmitter {
|
||||
if (!msg.isNewMsg) return;
|
||||
|
||||
const message = new Message(this, msg);
|
||||
|
||||
/**
|
||||
* Emitted when a message is deleted by the current user.
|
||||
* @event Client#message_revoke_me
|
||||
* @param {Message} message The message that was revoked
|
||||
*/
|
||||
this.emit(Events.MESSAGE_REVOKED_ME, message);
|
||||
|
||||
});
|
||||
@@ -152,6 +202,10 @@ class Client extends EventEmitter {
|
||||
await page.exposeFunction('onAppStateChangedEvent', (AppState, state) => {
|
||||
const ACCEPTED_STATES = [WAState.CONNECTED, WAState.OPENING, WAState.PAIRING];
|
||||
if (!ACCEPTED_STATES.includes(state)) {
|
||||
/**
|
||||
* Emitted when the client has been disconnected
|
||||
* @event Client#disconnected
|
||||
*/
|
||||
this.emit(Events.DISCONNECTED);
|
||||
this.destroy();
|
||||
}
|
||||
@@ -168,9 +222,16 @@ class Client extends EventEmitter {
|
||||
this.pupBrowser = browser;
|
||||
this.pupPage = page;
|
||||
|
||||
/**
|
||||
* Emitted when the client has initialized and is ready to receive messages.
|
||||
* @event Client#ready
|
||||
*/
|
||||
this.emit(Events.READY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the client
|
||||
*/
|
||||
async destroy() {
|
||||
await this.pupBrowser.close();
|
||||
}
|
||||
@@ -180,6 +241,7 @@ class Client extends EventEmitter {
|
||||
* @param {string} chatId
|
||||
* @param {string|MessageMedia} content
|
||||
* @param {object} options
|
||||
* @returns {Promise<Message>} Message that was just sent
|
||||
*/
|
||||
async sendMessage(chatId, content, options={}) {
|
||||
let internalOptions = {
|
||||
@@ -205,6 +267,7 @@ class Client extends EventEmitter {
|
||||
|
||||
/**
|
||||
* Get all current chat instances
|
||||
* @returns {Promise<Array<Chat>>}
|
||||
*/
|
||||
async getChats() {
|
||||
let chats = await this.pupPage.evaluate(() => {
|
||||
@@ -217,6 +280,7 @@ class Client extends EventEmitter {
|
||||
/**
|
||||
* Get chat instance by ID
|
||||
* @param {string} chatId
|
||||
* @returns {Promise<Chat>}
|
||||
*/
|
||||
async getChatById(chatId) {
|
||||
let chat = await this.pupPage.evaluate(chatId => {
|
||||
@@ -228,6 +292,7 @@ class Client extends EventEmitter {
|
||||
|
||||
/**
|
||||
* Get all current contact instances
|
||||
* @returns {Promise<Array<Contact>>}
|
||||
*/
|
||||
async getContacts() {
|
||||
let contacts = await this.pupPage.evaluate(() => {
|
||||
@@ -240,6 +305,7 @@ class Client extends EventEmitter {
|
||||
/**
|
||||
* Get contact instance by ID
|
||||
* @param {string} contactId
|
||||
* @returns {Promise<Contact>}
|
||||
*/
|
||||
async getContactById(contactId) {
|
||||
let contact = await this.pupPage.evaluate(contactId => {
|
||||
@@ -250,8 +316,8 @@ class Client extends EventEmitter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts an invite by code
|
||||
* @param {string} inviteCode
|
||||
* Accepts an invitation to join a group
|
||||
* @param {string} inviteCode Invitation code
|
||||
*/
|
||||
async acceptInvite(inviteCode) {
|
||||
const chatId = await this.pupPage.evaluate(async inviteCode => {
|
||||
|
||||
Reference in New Issue
Block a user