From f23ab825655b620e0b977468f5719b7bf5126ee3 Mon Sep 17 00:00:00 2001 From: alechkos <93551621+alechkos@users.noreply.github.com> Date: Sat, 1 Apr 2023 22:30:12 +0300 Subject: [PATCH] feat: added contact_changed event (#2027) --- example.js | 49 +++++++++++++++++++++++++++++++++++++------ index.d.ts | 13 ++++++++++++ src/Client.js | 31 +++++++++++++++++++++++++++ src/util/Constants.js | 1 + 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/example.js b/example.js index 7187b6f..f00b130 100644 --- a/example.js +++ b/example.js @@ -1,4 +1,4 @@ -const { Client, Location, List, Buttons, LocalAuth} = require('./index'); +const { Client, Location, List, Buttons, LocalAuth } = require('./index'); const client = new Client({ authStrategy: new LocalAuth(), @@ -189,11 +189,11 @@ client.on('message', async msg => { client.interface.openChatWindowAt(quotedMsg.id._serialized); } } else if (msg.body === '!buttons') { - let button = new Buttons('Button body',[{body:'bt1'},{body:'bt2'},{body:'bt3'}],'title','footer'); + let button = new Buttons('Button body', [{ body: 'bt1' }, { body: 'bt2' }, { body: 'bt3' }], 'title', 'footer'); client.sendMessage(msg.from, button); } else if (msg.body === '!list') { - let sections = [{title:'sectionTitle',rows:[{title:'ListItem1', description: 'desc'},{title:'ListItem2'}]}]; - let list = new List('List body','btnText',sections,'Title','footer'); + let sections = [{ title: 'sectionTitle', rows: [{ title: 'ListItem1', description: 'desc' }, { title: 'ListItem2' }] }]; + let list = new List('List body', 'btnText', sections, 'Title', 'footer'); client.sendMessage(msg.from, list); } else if (msg.body === '!reaction') { msg.react('👍'); @@ -231,7 +231,7 @@ client.on('message_ack', (msg, ack) => { ACK_PLAYED: 4 */ - if(ack == 3) { + if (ack == 3) { // The message was read } }); @@ -254,7 +254,7 @@ client.on('group_update', (notification) => { }); client.on('change_state', state => { - console.log('CHANGE STATE', state ); + console.log('CHANGE STATE', state); }); // Change to false if you don't want to reject incoming calls @@ -270,6 +270,43 @@ client.on('disconnected', (reason) => { console.log('Client was logged out', reason); }); +client.on('contact_changed', async (message, oldId, newId, isContact) => { + /** The time the event occurred. */ + const eventTime = (new Date(message.timestamp * 1000)).toLocaleString(); + + console.log( + `The contact ${oldId.slice(0, -5)}` + + `${!isContact ? ' that participates in group ' + + `${(await client.getChatById(message.to ?? message.from)).name} ` : ' '}` + + `changed their phone number\nat ${eventTime}.\n` + + `Their new phone number is ${newId.slice(0, -5)}.\n`); + + /** + * Information about the {@name message}: + * + * 1. If a notification was emitted due to a group participant changing their phone number: + * {@name message.author} is a participant's id before the change. + * {@name message.recipients[0]} is a participant's id after the change (a new one). + * + * 1.1 If the contact who changed their number WAS in the current user's contact list at the time of the change: + * {@name message.to} is a group chat id the event was emitted in. + * {@name message.from} is a current user's id that got an notification message in the group. + * Also the {@name message.fromMe} is TRUE. + * + * 1.2 Otherwise: + * {@name message.from} is a group chat id the event was emitted in. + * {@name message.to} is @type {undefined}. + * Also {@name message.fromMe} is FALSE. + * + * 2. If a notification was emitted due to a contact changing their phone number: + * {@name message.templateParams} is an array of two user's ids: + * the old (before the change) and a new one, stored in alphabetical order. + * {@name message.from} is a current user's id that has a chat with a user, + * whos phone number was changed. + * {@name message.to} is a user's id (after the change), the current user has a chat with. + */ +}); + client.on('group_admin_changed', (notification) => { if (notification.type === 'promote') { /** diff --git a/index.d.ts b/index.d.ts index 98e3a19..23ba35c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -210,6 +210,18 @@ declare namespace WAWebJS { notification: GroupNotification ) => void): this + /** Emitted when a contact or a group participant changed their phone number. */ + on(event: 'contact_changed', listener: ( + /** Message with more information about the event. */ + message: Message, + /** Old user's id. */ + oldId : String, + /** New user's id. */ + newId : String, + /** Indicates if a contact or a group participant changed their phone number. */ + isContact : Boolean + ) => void): this + /** Emitted when media has been uploaded for a message sent by the client */ on(event: 'media_uploaded', listener: ( /** The message with media that was uploaded */ @@ -532,6 +544,7 @@ declare namespace WAWebJS { MESSAGE_REVOKED_ME = 'message_revoke_me', MESSAGE_ACK = 'message_ack', MEDIA_UPLOADED = 'media_uploaded', + CONTACT_CHANGED = 'contact_changed', GROUP_JOIN = 'group_join', GROUP_LEAVE = 'group_leave', GROUP_ADMIN_CHANGED = 'group_admin_changed', diff --git a/src/Client.js b/src/Client.js index 5ed0114..d0c949c 100644 --- a/src/Client.js +++ b/src/Client.js @@ -45,6 +45,7 @@ const NoAuth = require('./authStrategies/NoAuth'); * @fires Client#group_update * @fires Client#disconnected * @fires Client#change_state + * @fires Client#contact_changed * @fires Client#group_admin_changed */ class Client extends EventEmitter { @@ -384,6 +385,36 @@ class Client extends EventEmitter { last_message = msg; } + /** + * The event notification that is received when one of + * the group participants changes thier phone number. + */ + const isParticipant = msg.type === 'gp2' && msg.subtype === 'modify'; + + /** + * The event notification that is received when one of + * the contacts changes thier phone number. + */ + const isContact = msg.type === 'notification_template' && msg.subtype === 'change_number'; + + if (isParticipant || isContact) { + /** {@link GroupNotification} object does not provide enough information about this event, so a {@link Message} object is used. */ + const message = new Message(this, msg); + + const newId = isParticipant ? msg.recipients[0] : msg.to; + const oldId = isParticipant ? msg.author : msg.templateParams.find(id => id !== newId); + + /** + * Emitted when a contact or a group participant changes their phone number. + * @event Client#contact_changed + * @param {Message} message Message with more information about the event. + * @param {String} oldId The user's id (an old one) who changed their phone number + * and who triggered the notification. + * @param {String} newId The user's new id after the change. + * @param {Boolean} isContact Indicates if a contact or a group participant changed their phone number. + */ + this.emit(Events.CONTACT_CHANGED, message, oldId, newId, isContact); + } }); await page.exposeFunction('onRemoveMessageEvent', (msg) => { diff --git a/src/util/Constants.js b/src/util/Constants.js index 2d77de0..97512c1 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -46,6 +46,7 @@ exports.Events = { UNREAD_COUNT: 'unread_count', MESSAGE_REACTION: 'message_reaction', MEDIA_UPLOADED: 'media_uploaded', + CONTACT_CHANGED: 'contact_changed', GROUP_JOIN: 'group_join', GROUP_LEAVE: 'group_leave', GROUP_ADMIN_CHANGED: 'group_admin_changed',