Closed emdadgar2 closed 7 months ago
Hi @edgardmessias , I took a look at how I could implement this function in the library, but I encountered a problem.
Since the client has not been started yet, it is not possible to call its functions to generate the login number. The only alternative I thought of would be to pass it as a parameter in the session create, but this would require the user to enter the phone number in the create as well. Do you have any other alternative implementation suggestions?
that would be great to create the session with an optional number parameter, to cases where its needed to start a session from the mobile phone where whatsapp is installed
Hi @edgardmessias , I took a look at how I could implement this function in the library, but I encountered a problem.
Since the client has not been started yet, it is not possible to call its functions to generate the login number. The only alternative I thought of would be to pass it as a parameter in the session create, but this would require the user to enter the phone number in the create as well. Do you have any other alternative implementation suggestions?
Phone number can be set in "wppconnect.create" call as a parameter
I've tested locally using wa-js, and I'm pretty sure it's doable. It even worked once while debugging, although I cannot replicate it for some reason. It goes something like this:
if (this.options.phoneNumber !== null) {
this.logger.info(
'Logging in with Phone Number. Please Wait..................'
);
const code = await this.page.evaluate(() => {
return WPP.conn.genLinkDeviceCodeForPhoneNumber('<phone number>');
});
this.logger.info(`Code generated: ${code}`);
}
I'm guessing it should be somewhere inside initializer.ts or host.layer.ts (I tried both), but I cannot get it to work. This feature would be awesome to have !
I've tested locally using wa-js, and I'm pretty sure it's doable. It even worked once while debugging, although I cannot replicate it for some reason. It goes something like this:
if (this.options.phoneNumber !== null) { this.logger.info( 'Logging in with Phone Number. Please Wait..................' ); const code = await this.page.evaluate(() => { return WPP.conn.genLinkDeviceCodeForPhoneNumber('<phone number>'); }); this.logger.info(`Code generated: ${code}`); }
I'm guessing it should be somewhere inside initializer.ts or host.layer.ts (I tried both), but I cannot get it to work. This feature would be awesome to have !
Hello, I have already created the code, however, it is being tested locally. I will implement it soon, the code is very close to what you did, however, I have to make adjustments to prevent injection errors.
Please, test this PR https://github.com/wppconnect-team/wppconnect/pull/2097
Will do, also it so happens that I worked on it as well, and here is what I ended up with
under host.layer.ts in HostLayer I declared a couple of variables:
protected phoneLinkCode = null;
protected didGetCode = false;
protected sendPhoneLinkInterval?: NodeJS.Timer = null;
Inside the start() function:
public async start() {
if (this.isStarted) {
return;
}
this.isStarted = true;
await initWhatsapp(
this.page,
null,
false,
this.options.whatsappVersion,
this.log.bind(this)
);
await this.page.exposeFunction('checkQrCode', () => this.checkQrCode());
await this.page.exposeFunction('checkInChat', () => this.checkInChat());
this.checkStartInterval = setInterval(() => this.checkStart(), 5000);
// phoneNumber is defined, generate the code for login
if (this.options.phoneNumber !== null) {
this.sendPhoneLinkInterval = setInterval(async () => {
await this.sendPhoneLink();
// clear the interval
// No need to start a new interval process, phoneLinkCode should be valid for 5 minutes
if (this.phoneLinkCode !== null) {
clearInterval(this.sendPhoneLinkInterval as NodeJS.Timeout);
}
}, 5000);
}
this.page.on('close', () => {
clearInterval(this.checkStartInterval as NodeJS.Timeout);
});
}
defined a sendPhoneLink function:
protected async sendPhoneLink() {
try {
const connected = await isAuthenticated(this.page);
if (connected) {
this.phoneLinkCode = null;
return;
}
// Only use genLinkDeviceCodeForPhoneNumber once, otherwise it spams the user
if (this.phoneLinkCode === null) {
try {
this.phoneLinkCode = await this.page.evaluate((phoneNumber) => {
return WPP.conn.genLinkDeviceCodeForPhoneNumber(phoneNumber);
}, this.options.phoneNumber);
if (this.phoneLinkCode) {
this.logger.info(`Code is: ${this.phoneLinkCode}`);
}
} catch (error) {
this.logger.info('Generating Code...');
}
}
} catch (error) {
this.logger.info('Generating Code...');
}
}
Now we just need to add the config in create-config.ts
/**
* Login using phone number
* @default null
*/
phoneNumber?: string | null;
I hope this helps, I've tested this and it seems to work quite fine...
awesome, waiting for this in @wppconnect-server
Same, this would be a great addition in @wppconnect-server
awesome, waiting for this in @wppconnect-server
Next release of @wppconnect-server
Thanks for your support
Hi
Is there any plan to use "Link with Phone Number" instead of QRCode?