marcosvrs / WTF

WTF - Chrome Extension that allows you to send messages in bulk via Whatsapp™ Web
https://chrome.google.com/webstore/detail/kcdlihaidnmkenhlnofkjfoachidbnif
MIT License
65 stars 19 forks source link

Problems sending the message #26

Closed luizsergiorf closed 12 months ago

luizsergiorf commented 1 year ago

Hello, congratulations on the project, very useful and well implemented. However, I have the following problem.

Here in Brazil, we changed the inclusion of an extra digit on phones, so many people's WhatsApp has changed, however in this tool, when I send it with an extra digit, it is not directed to the correct WhatsApp and does not reach the user, for example, +551199889988, it was a WhatsApp before being increased by the ninth, with the implementation the number became +5511999889988. And when the tool sends it to 5511999889988, it doesn't reach the user, only if it is sent by the old one, 551199889988. What could it be?

Strangely, if you use wa.me/+5511999889988, it works and directs you to 551199889988.

luizsergiorf commented 1 year ago

I got a response from WPPConnect/WA-JS. They said that you need to use the WPP.contact.queryExists function before sending message to get the real number.

@marcosvrs Could you implement this functionality, please?

luizsergiorf commented 1 year ago

For anyone looking for this solution, here's what I did:

async function sendMessage({ contact, hash }: { contact: string, hash: number }) {
    if (!WPP.conn.isAuthenticated()) {
        const errorMsg = 'Conecte-se primeiro!';
        alert(errorMsg);
        throw new Error(errorMsg);
    }
    const { message } = await storageManager.retrieveMessage(hash);

    const resultAux = await WPP.contact.queryExists(contact);
    if (resultAux && resultAux.wid) {
        contact = resultAux.wid.user;
    } else {
        WebpageMessageManager.sendMessage(ChromeMessageTypes.ADD_LOG, { level: 1, message: "Contato não encontrado", attachment: message.attachment != null, contact: contact });
        return;
    }

    const result = await sendWPPMessage({ contact, ...message });
    return result.sendMsgResult.then(value => {
        const result = (value as any).messageSendResult ?? value;
        if (result !== WPP.whatsapp.enums.SendMsgResult.OK) {
            throw new Error('Falha ao enviar a mensagem: ' + value);
        } else {
            WebpageMessageManager.sendMessage(ChromeMessageTypes.ADD_LOG, { level: 3, message: 'Mensagem enviada com sucesso!', attachment: message.attachment != null, contact: contact });
        }
    });
}

Perhaps in the future it would be better to check this before the object add in queue. I also made a correction to improve the delay, where a random number of the chosen value is generated and the range can be up to twice it, thus improving the risk of avoiding bans in mass uploads.

async function addToQueue(message: Message) {
    try {
        const messageHash = AsyncStorageManager.calculateMessageHash(message);
        await storageManager.storeMessage(message, messageHash);

        let min = message.delay as number;
        let max = min * 2;
        let aux = Math.floor(Math.random() * (max - min)) + min;
        message.delay = aux;

        await asyncQueue.add({ eventHandler: sendMessage, detail: { contact: message.contact, hash: messageHash, delay: message.delay } });
        return true;
    } catch (error) {
        if (error instanceof Error) {
            WebpageMessageManager.sendMessage(ChromeMessageTypes.ADD_LOG, { level: 1, message: error.message, attachment: message.attachment != null, contact: message.contact });
        }
        throw error;
    }
}

All files I edit wa-js.ts.

PragneshAgola commented 12 months ago

@marcosvrs & @luizsergiorf, there is a problem, WhatsApp web portal doesn't open when extension is enabled. And this issue I face from today. Have any idea why we facing this issue?