mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-17 19:26:20 +00:00
feat: added contact_changed event (#2027)
This commit is contained in:
49
example.js
49
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') {
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user