Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Mar 9, 2024
2 parents ca22e22 + 379c4ba commit fdf8a25
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v4.0.2
with:
node-version: 16.x
node-version: 18.x

- name: Get npm cache directory
id: npm-cache
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v4.0.2
with:
node-version: 16.x
node-version: 18.x

- name: Get npm cache directory
id: npm-cache
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v4.0.2
with:
node-version: 16.x
node-version: 18.x

- name: Get npm cache directory
id: npm-cache
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v4.0.2
with:
node-version: 16.x
node-version: 18.x
registry-url: 'https://registry.npmjs.org'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v4.0.2
with:
node-version: 16.x
node-version: 18.x

- name: Get npm cache directory
id: npm-cache
Expand Down
25 changes: 13 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@types/atob": "^2.1.4",
"@types/mime-types": "^2.1.4",
"@types/mocha": "^10.0.6",
"@types/node": "^18.19.21",
"@types/node": "^18.19.22",
"@types/rimraf": "^3.0.2",
"@types/sharp": "^0.31.1",
"@types/shelljs": "^0.8.15",
Expand All @@ -89,13 +89,13 @@
"shx": "^0.3.4",
"ts-node": "^10.9.2",
"typedoc": "^0.25.10",
"typescript": "^5.3.3",
"typescript": "^5.4.2",
"webpack": "^5.90.3",
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@wppconnect/wa-js": "^2.28.1",
"@wppconnect/wa-version": "^1.2.263",
"@wppconnect/wa-version": "^1.2.267",
"atob": "^2.1.2",
"axios": "^1.6.7",
"boxen": "^5.1.2",
Expand Down Expand Up @@ -125,6 +125,7 @@
"ws": "^8.16.0"
},
"optionalDependencies": {
"@img/sharp-win32-x64": "^0.33.2",
"fsevents": "^2.3.3"
},
"directories": {
Expand Down
4 changes: 1 addition & 3 deletions src/api/layers/group.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,12 @@ export class GroupLayer extends RetrieverLayer {
groupId: string,
participantId: string | string[]
) {
await evaluateAndReturn(
return await evaluateAndReturn(
this.page,
({ groupId, participantId }) =>
WPP.group.removeParticipants(groupId, participantId),
{ groupId, participantId }
);

return true;
}

/**
Expand Down
47 changes: 42 additions & 5 deletions src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { evaluateAndReturn, scrapeImg } from '../helpers';
import {
CatchQRCallback,
HostDevice,
LinkByCodeCallback,
LoadingScreenCallback,
StatusFindCallback,
} from '../model';
Expand All @@ -57,6 +58,7 @@ export class HostLayer {
public catchQR?: CatchQRCallback = null;
public statusFind?: StatusFindCallback = null;
public onLoadingScreen?: LoadingScreenCallback = null;
public catchLinkCode?: LinkByCodeCallback = null;

constructor(public page: Page, session?: string, options?: CreateConfig) {
this.session = session;
Expand Down Expand Up @@ -143,8 +145,8 @@ export class HostLayer {
evaluateAndReturn(this.page, () => {
WPP.on('conn.main_ready', (window as any).checkInChat);
}).catch(() => null);
this.checkQrCode();
this.checkInChat();
this.checkQrCode();
}

public async start() {
Expand All @@ -163,6 +165,9 @@ export class HostLayer {
);

await this.page.exposeFunction('checkQrCode', () => this.checkQrCode());
await this.page.exposeFunction('loginByCode', (phone: string) =>
this.loginByCode(phone)
);
await this.page.exposeFunction('checkInChat', () => this.checkInChat());

this.checkStartInterval = setInterval(() => this.checkStart(), 5000);
Expand All @@ -188,9 +193,13 @@ export class HostLayer {
}

const result = await this.getQrCode();

if (!result?.urlCode || this.urlCode === result.urlCode) {
return;
}
if (typeof this.options.phoneNumber !== undefined) {
return this.loginByCode(this.options.phoneNumber);
}
this.urlCode = result.urlCode;
this.attempt++;

Expand All @@ -213,6 +222,24 @@ export class HostLayer {
this.catchQR?.(result.base64Image, qr, this.attempt, result.urlCode);
}

protected async loginByCode(phone: string) {
const code = await evaluateAndReturn(
this.page,
async ({ phone }) => {
return JSON.parse(
JSON.stringify(await WPP.conn.genLinkDeviceCodeForPhoneNumber(phone))
);
},
{ phone }
);
if (this.options.logQR) {
this.log('info', `Waiting for Login By Code (Code: ${code})\n`);
} else {
this.log('verbose', `Waiting for Login By Code`);
}
this.catchLinkCode?.(code);
}

protected async checkInChat() {
const inChat = await isInsideChat(this.page).catch(() => null);

Expand Down Expand Up @@ -339,11 +366,21 @@ export class HostLayer {
this.startAutoClose();

if (authenticated === false) {
this.log('http', 'Waiting for QRCode Scan...');
this.log(
'http',
typeof this.options.phoneNumber !== undefined
? 'Waiting for Login by Code...'
: 'Waiting for QRCode Scan...'
);
this.statusFind?.('notLogged', this.session);
await this.waitForQrCodeScan();

this.log('http', 'Checking QRCode status...');
this.log(
'http',
typeof this.options.phoneNumber !== undefined
? 'Checking Login by Code status...'
: 'Checking QRCode status...'
);
// Wait for interface update
await sleep(200);
authenticated = await isAuthenticated(this.page).catch(() => null);
Expand All @@ -352,10 +389,10 @@ export class HostLayer {
this.log('warn', 'Failed to authenticate');
this.statusFind?.('qrReadError', this.session);
} else if (authenticated) {
this.log('http', 'QRCode Success');
this.log('http', 'Login with success');
this.statusFind?.('qrReadSuccess', this.session);
} else {
this.log('warn', 'QRCode Fail');
this.log('warn', 'Login Fail');
this.statusFind?.('qrReadFail', this.session);
this.tryAutoClose();
throw 'Failed to read the QRCode';
Expand Down
26 changes: 22 additions & 4 deletions src/api/layers/status.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
fileToBase64,
downloadFileToBase64,
} from '../helpers';
import { SendStatusOptions } from '@wppconnect/wa-js/dist/status';

export class StatusLayer extends LabelsLayer {
constructor(public page: Page, session?: string, options?: CreateConfig) {
Expand All @@ -37,9 +38,18 @@ export class StatusLayer extends LabelsLayer {
* ```javascript
* client.sendImageStatus('data:image/jpeg;base64,<a long base64 file...>');
* ```
*
* @example
* ```javascript
* // Send with caption
* client.sendImageStatus('data:image/jpeg;base64,<a long base64 file...>', { caption: 'example test' } );
* ```
* @param pathOrBase64 Path or base 64 image
*/
public async sendImageStatus(pathOrBase64: string) {
public async sendImageStatus(
pathOrBase64: string,
options?: SendStatusOptions & { caption?: string }
) {
let base64: string = '';
if (pathOrBase64.startsWith('data:')) {
base64 = pathOrBase64;
Expand Down Expand Up @@ -81,7 +91,7 @@ export class StatusLayer extends LabelsLayer {
return await evaluateAndReturn(
this.page,
({ base64 }) => {
WPP.status.sendImageStatus(base64);
WPP.status.sendImageStatus(base64, options);
},
{ base64 }
);
Expand All @@ -94,9 +104,17 @@ export class StatusLayer extends LabelsLayer {
* ```javascript
* client.sendVideoStatus('data:video/mp4;base64,<a long base64 file...>');
* ```
* @example
* ```javascript
* // Send with caption
* client.sendVideoStatus('data:video/mp4;base64,<a long base64 file...>', { caption: 'example test' } );
* ```
* @param pathOrBase64 Path or base 64 image
*/
public async sendVideoStatus(pathOrBase64: string) {
public async sendVideoStatus(
pathOrBase64: string,
options?: SendStatusOptions & { caption?: string }
) {
let base64: string = '';
if (pathOrBase64.startsWith('data:')) {
base64 = pathOrBase64;
Expand All @@ -121,7 +139,7 @@ export class StatusLayer extends LabelsLayer {
return await evaluateAndReturn(
this.page,
({ base64 }) => {
WPP.status.sendVideoStatus(base64);
WPP.status.sendVideoStatus(base64, options);
},
{ base64 }
);
Expand Down
11 changes: 11 additions & 0 deletions src/api/model/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export type StatusFindCallback = (
*/
export type LoadingScreenCallback = (percent: number, message: string) => void;

/**
* A callback will be received, informing a code to you connect
*/
export type LinkByCodeCallback = (code: string) => void;

export interface CreateOptions extends CreateConfig {
/**
* You must pass a string type parameter, this parameter will be the name of the client's session. If the parameter is not passed, the section name will be "session".
Expand All @@ -50,6 +55,12 @@ export interface CreateOptions extends CreateConfig {
* A callback will be received, informing the status of the qrcode
*/
catchQR?: CatchQRCallback;

/**
* A callback will be received, informing a code to you connect
*/
catchLinkCode?: LinkByCodeCallback;

/**
* A callback will be received, informing the customer's status
*/
Expand Down
7 changes: 6 additions & 1 deletion src/config/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface CreateConfig {
* Headless chrome
* @default true
*/
headless?: boolean;
headless?: boolean | 'shell';
/**
* Open devtools by default
* @default false
Expand Down Expand Up @@ -168,6 +168,11 @@ export interface CreateConfig {
* @default 'WPPConnect'
*/
poweredBy?: string;

/**
* Insert the phone number for connect by link phone, qr code not wil generate
*/
phoneNumber?: string;
}

export const defaultOptions: CreateConfig = {
Expand Down

0 comments on commit fdf8a25

Please sign in to comment.