Skip to content

Commit

Permalink
fix: Fixed Puppeteer error and undefined sendList (#1453)
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Dec 11, 2022
1 parent 6c07878 commit d0891fe
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -110,7 +110,7 @@
"logform": "^2.4.2",
"lookpath": "^1.2.2",
"mime-types": "^2.1.35",
"puppeteer": "*",
"puppeteer": "^19.4.0",
"puppeteer-extra": "^3.3.4",
"puppeteer-extra-plugin-stealth": "^2.11.1",
"puppeteer-extra-plugin-user-data-dir": "^2.4.0",
Expand Down
7 changes: 5 additions & 2 deletions src/api/helpers/evaluate-and-return.ts
Expand Up @@ -15,12 +15,15 @@
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
*/

import { Page } from 'puppeteer';

import {
EvaluateFn,
EvaluateFnReturnType,
Page,
SerializableOrJSHandle,
} from 'puppeteer';
} from '../../types/Evaluate';

//EvaluateFn, EvaluateFnReturnType, SerializableOrJSHandle //

export async function evaluateAndReturn<T extends EvaluateFn>(
page: Page,
Expand Down
33 changes: 24 additions & 9 deletions src/api/layers/sender.layer.ts
Expand Up @@ -24,7 +24,7 @@ import type {
PoolMessageOptions,
} from '@wppconnect/wa-js/dist/chat';
import * as path from 'path';
import { JSONObject, Page } from 'puppeteer';
import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { convertToMP4GIF } from '../../utils/ffmpeg';
import { sleep } from '../../utils/sleep';
Expand Down Expand Up @@ -1200,20 +1200,35 @@ export class SenderLayer extends ListenerLayer {
* @category Chat
*/
public async sendListMessage(to: string, options: ListMessageOptions) {
return await evaluateAndReturn(
const sendResult = await evaluateAndReturn(
this.page,
({ to, options }) => {
WPP.chat.sendListMessage(to, options);
},
{ to, options: options as unknown as JSONObject }
({ to, options }) => WPP.chat.sendListMessage(to, options),
{
to,
options: options,
}
);

// I don't know why the evaluate is returning undefined for direct call
// To solve that, I added `JSON.parse(JSON.stringify(<message>))` to solve that
const result = (await evaluateAndReturn(
this.page,
async ({ messageId }) => {
return JSON.parse(JSON.stringify(await WAPI.getMessageById(messageId)));
},
{ messageId: sendResult.id }
)) as Message;

if (result['erro'] == true) {
throw result;
}

return result;
}

/**
* Send a create poll message
*
* Note: This only works for groups
*
* @example
* ```javascript
* // Single pool
Expand Down Expand Up @@ -1252,7 +1267,7 @@ export class SenderLayer extends ListenerLayer {
chatId,
name,
choices,
options: options as unknown as JSONObject,
options: options,
}
);

Expand Down
10 changes: 6 additions & 4 deletions src/controllers/auth.ts
Expand Up @@ -25,7 +25,7 @@ import { sleep } from '../utils/sleep';

export const getInterfaceStatus = async (
waPage: puppeteer.Page
): Promise<string | null> => {
): Promise<puppeteer.HandleFor<Awaited<ReturnType<any>>>> => {
return await waPage
.waitForFunction(
() => {
Expand All @@ -52,8 +52,10 @@ export const getInterfaceStatus = async (
polling: 100,
}
)
.then(async (element) => {
return (await element.evaluate((a) => a)) as string;
.then(async (element: puppeteer.HandleFor<Awaited<ReturnType<any>>>) => {
return (await element.evaluate((a: any) => a)) as puppeteer.HandleFor<
ReturnType<any>
>;
})
.catch(() => null);
};
Expand Down Expand Up @@ -160,7 +162,7 @@ export async function injectSessionToken(
await page.goto(puppeteerConfig.whatsappUrl + '?_=' + Date.now());

if (clear) {
await page.evaluate((session) => {
await page.evaluate(() => {
if (document.title !== 'Initializing WhatsApp') {
return;
}
Expand Down
28 changes: 28 additions & 0 deletions src/types/Evaluate.d.ts
@@ -0,0 +1,28 @@
/*
* This file is part of WPPConnect.
*
* WPPConnect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WPPConnect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
*/

export declare type EvaluateFn<T = any, U = any, V = any> =
| string
| ((arg1: T, ...args: U[]) => V);

export declare type EvaluateFnReturnType<T extends EvaluateFn> = T extends (
...args: any[]
) => infer R
? R
: any;

export declare type SerializableOrJSHandle = Serializable | JSHandle;

0 comments on commit d0891fe

Please sign in to comment.