Whatsapp-web.js: getContactById

Created on 4 Jun 2020  路  10Comments  路  Source: pedroslopez/whatsapp-web.js

Hallo , i want to getcontact but use id custom ,

my code in example js
`

const contact = msg.getContactById();

`

i put my code in message.js

>     getContactById(Nomor) {
>         return this.client.getContactById(Nomor);
>     }

Most helpful comment

On the other hand, since there is already a battery event... maybe one could implement a function that returns the current battery state. This would probably be callable under client.info

All 10 comments

and how to get info for batery and send to group ?

You can get battery info with this code from example.js:

client.on('change_battery', (batteryInfo) => {
  // Battery percentage for attached device has changed
  const { battery, plugged } = batteryInfo;
  console.log(`Battery: ${battery}% - Charging? ${plugged}`);
});

so just msg.reply(batteryInfo);

And I dont really get what you mean by getContactById, can you elaborate?

You can get battery info with this code from example.js:

client.on('change_battery', (batteryInfo) => {
  // Battery percentage for attached device has changed
  const { battery, plugged } = batteryInfo;
  console.log(`Battery: ${battery}% - Charging? ${plugged}`);
});

so just msg.reply(batteryInfo);

And I dont really get what you mean by getContactById, can you elaborate?

(node:12176) UnhandledPromiseRejectionWarning: ReferenceError: batteryInfo is not defined
i have this error ,
and for getcontactid, I agree to get info about the ID that I have specified

post your entire code so we can help you. an error message is not enough

client.on('message', async msg => {
console.log('MESSAGE RECEIVED', msg);
if (msg.body == "cek")
{
msg.reply(batteryInfo);
}
});

client.on('change_battery', (batteryInfo) => {
// Battery percentage for attached device has changed
const { battery, plugged } = batteryInfo;
console.log(Battery: ${battery}% - Charging? ${plugged});
});

You can't call batteryInfo because it is scopped inside client.on('change_battery').

Try like this:

let globalBatteryInfo;

client.on('change_battery', (batteryInfo) => {
   const { battery, plugged } = batteryInfo;
   globalBatteryInfo = batteryInfo;
}

client.on('message', async msg => {
  console.log('message received', msg);
  msg.reply(globalBatteryInfo);
};


keep in mind that change_battery must be called first, otherwise globalBatteryInfo will return as undefined.

a second option is

client.on('change_battery', 'message', async (batteryInfo, msg ) => {
   const { battery, plugged } = batteryInfo;
   console.log(batteryInfo);
   console.log('message received', msg);
}

not tested so let me know if it works.

On the other hand, since there is already a battery event... maybe one could implement a function that returns the current battery state. This would probably be callable under client.info

A direct way of getting battery status like @Aliyss has been implemented in 753b6772aba194faa4315771f60a73fd54b57b1d

Closing this as it seems like the original issue has been resolved.

client.on('change_battery', 'message', async (batteryInfo, msg ) => {
   const { battery, plugged } = batteryInfo;
   console.log(batteryInfo);
   console.log('message received', msg);
}

use second option i have problem
events.js:109
throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
^

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received type string ('message')

@mastenwap That second option given isn't a valid use case.

If you want to listen to battery changes, you should use the change_battery event. If you want to directly get the current battery status, you should use client.info.getBatteryStatus(). https://pedroslopez.me/whatsapp-web.js/ClientInfo.html#getBatteryStatus

Was this page helpful?
0 / 5 - 0 ratings

Related issues

riagie picture riagie  路  6Comments

parthibd picture parthibd  路  4Comments

gabriel-tandil picture gabriel-tandil  路  5Comments

waltergammarota picture waltergammarota  路  8Comments

centralderastreamentogps picture centralderastreamentogps  路  7Comments