whatsapp-web.js icon indicating copy to clipboard operation
whatsapp-web.js copied to clipboard

feat: `vote_update` event

Open alechkos opened this issue 2 years ago • 9 comments

Table of Contents

- Description

- Related Issues

- Usage Example

- I Want to Test this PR

- I Got an Error While Testing This PR ❌

- How Has the PR Been Tested (latest test on 22.04.2024)

- Types of Changes


Special thanks to @tuyuribr for his help


Description

The PR adds an event that will be triggered when users vote or unvote in a poll. Each time the event is fired, it shows a user's current selected option(s) on the poll.


Related Issues

The PR closes #2494, closes #2626


Usage Example

client.on('vote_update', (vote) => {
    /**
     * The {@link vote} that was affected:
     * 
     * {
     *   voter: '[email protected]',
     *   selectedOptions: [ { name: 'B', localId: 1 } ],
     *   interractedAtTs: 1698195555555,
     *   parentMessage: {
     *     ...,
     *     pollName: 'PollName',
     *     pollOptions: [
     *       { name: 'A', localId: 0 },
     *       { name: 'B', localId: 1 }
     *     ],
     *     allowMultipleAnswers: true,
     *     messageSecret: [
     *        1, 2, 3, 0, 0, 0, 0, 0,
     *        0, 0, 0, 0, 0, 0, 0, 0,
     *        0, 0, 0, 0, 0, 0, 0, 0,
     *        0, 0, 0, 0, 0, 0, 0, 0
     *     ]
     *   }
     * }
     */
    console.log(vote);
});

To test this PR by yourself you should do two steps:

1. Install the PR by running one of the following commands:

  • NPM
npm install github:alechkos/whatsapp-web.js#polls-ext
  • YARN
yarn add github:alechkos/whatsapp-web.js#polls-ext

2. Lock your WWeb version on 2.2412.54:

const wwebVersion = '2.2412.54';

const client = new Client({
    authStrategy: new LocalAuth(), // your authstrategy here
    puppeteer: {
        // puppeteer args here
    },
    // locking the wweb version
    webVersionCache: {
        type: 'remote',
        remotePath: `https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/${wwebVersion}.html`,
    },
});

If you encounter any errors while testing this PR, please provide in a comment:

  1. The code you've used without any sensitive information (use syntax highlighting for more readability)
  2. The error you got
  3. The library version
  4. The WWeb version: console.log(await client.getWWebVersion());
  5. The browser (Chrome/Chromium)

[!IMPORTANT] You have to reapply the PR each time it is changed (new commits were pushed since your last application)


How Has The PR Been Tested (latest test on 22.04.2024)

Tested by selecting and deselecting poll options in both single-option and multiple-option polls.

Tested On:

Types of accounts:

  • Personal
  • Buisness

Environment:

  • Android 10:
    • WhatsApp: latest
    • WA Business: latest
  • Windows 10:
    • WWebJS: v1.23.1-alpha.5
    • WWeb: v2.2412.54
    • Puppeteer: v18.2.1
    • Node: v18.17.1
    • Chrome: latest

Types of Changes

  • [ ] Dependency change
  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [X] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix/feature that would cause existing functionality to change)

Checklist

  • [X] My code follows the code style of this project
  • [X] I have updated the usage example accordingly (example.js)
  • [X] I have updated the documentation accordingly (index.d.ts)

alechkos avatar Oct 23 '23 19:10 alechkos

The feature works like a charm for me. Is there any estimate on when the PR will be merged?

Dezzley avatar Nov 28 '23 16:11 Dezzley

Is it feasible to add an object containing the votes of all participants inside parentMessage? Something like:

participantsSelectedOptions: {
      '[email protected]': [{ name: 'B'; localId: 1 }];
      '[email protected]': [{ name: 'A'; localId: 0 }, { name: 'B'; localId: 1 }];
}

Dezzley avatar Nov 29 '23 10:11 Dezzley

@Dezzley

Is it feasible to add an object containing the votes of all participants inside parentMessage?

Check #1887

alechkos avatar Nov 29 '23 10:11 alechkos

Will this work for WhatsApp web 2.3x for now / future? Thanks!

seowzhenjun0126 avatar Apr 23 '24 00:04 seowzhenjun0126

@seowzhenjun0126

Will this work for WhatsApp web 2.3x for now / future? Thanks!

As soon as the patch is a part of main, I will add required changes to support wweb 2.3xxx

alechkos avatar Apr 23 '24 03:04 alechkos

Got it. Thanks for your help!

seowzhenjun0126 avatar Apr 23 '24 03:04 seowzhenjun0126

Não consigo utilizar e ler o voto do usuário. Alguém pode me ajudar?

felipeaffonsobsi avatar Apr 24 '24 22:04 felipeaffonsobsi

@felipeaffonsobsi

Não consigo utilizar e ler o voto do usuário. Alguém pode me ajudar?

https://github.com/pedroslopez/whatsapp-web.js/pull/2596#pr-issues

alechkos avatar Apr 24 '24 22:04 alechkos

@felipeaffonsobsi

Não consigo utilizar e ler o voto do usuário. Alguém pode me ajudar?

#2596 (comentário)

você reenviou essa página, desculpa mas isso não me ajudou.

felipeaffonsobsi avatar Apr 25 '24 00:04 felipeaffonsobsi

Hello, my bot do not trigger the vote_update Event.

This is my code: `const { Client, LocalAuth } = require("whatsapp-web.js"); const qrcode = require("qrcode-terminal"); const axios = require("axios"); const webhookURL = "https://example.com/webhooks/logger.php";

const client = new Client({ puppeteer: { headless: true, args: ["--no-sandbox"], }, authStrategy: new LocalAuth(), webVersionCache: { type: "remote", remotePath: "https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2411.2.html", }, authTimeoutMs: 300000, // Optional: timeout for authentication in milliseconds qrTimeout: 30000, // Optional: timeout for QR code generation });

client.on("qr", (qr) => { qrcode.generate(qr, { small: true }); const data = { event: "qr", msg: "", qr: qr }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); });

client.on("ready", () => { console.log("Client is ready!"); const data = { event: "ready", msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); });

client.on("authenticated", () => { console.log("Client is authenticated!"); const data = { event: "authenticated", msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); });

client.on("auth_failure", (msg) => { console.error("Authentication failure", msg); const data = { event: "auth_failure", msg: msg }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); });

client.on("message", async (msg) => { console.log("MESSAGE RECEIVED", msg); const data = { event: "message", msg: msg }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); if (msg.body === "!ping") { msg.reply("pong"); }

if (msg.body === "!smart_1") {
    const chat = await msg.getChat();
    // simulates typing in the chat
    await chat.sendStateTyping();
    axios.get("http://192.168.178.46/cm?cmnd=status%208").then(response => {
        if (response.status === 200) {
            try {
                console.log(response);
                //const d = JSON.parse(response.data);
                const d = response.data;
                m = "Aktuelle Werte:\n   Energie:\n     Heute: " + d.StatusSNS.ENERGY.Today + " kWh\n     Gestern: " + d.StatusSNS.ENERGY.Yesterday + " kWh\n     Gesamt: " + d.StatusSNS.ENERGY.Total + " kWh\n   Strom: " + d.StatusSNS.ENERGY.Current + " A\n   Spannung: " + d.StatusSNS.ENERGY.Voltage + " V";
                //chat.clearState();
                //msg.reply(m);
            } catch (e) {
                console.error(e)
            }
        }
    }).catch(error => {
        console.error("Error: " + error);
        msg.reply("Error: " + error);
    });

    axios.get("http://192.168.178.46/cm?cmnd=status%2011").then(response => {
        if (response.status === 200) {
            try {
                console.log(response);
                //const d = JSON.parse(response.data);
                const d = response.data;
                m = m + "\n   Schaltzustand: " + d.StatusSTS.POWER;
                chat.clearState();
                msg.reply(m);
            } catch (e) {
                console.error(e)
            }
        }
    }).catch(error => {
        console.error("Error: " + error);
        msg.reply("Error: " + error);
    });
}

if (msg.body === "!smart_2") {
    const chat = await msg.getChat();
    // simulates typing in the chat
    await chat.sendStateTyping();
    axios.get("http://192.168.178.51/cm?cmnd=status%208").then(response => {
        if (response.status === 200) {
            try {
                console.log(response);
                //const d = JSON.parse(response.data);
                const d = response.data;
                m = "Aktuelle Werte:\n   Energie:\n     Heute: " + d.StatusSNS.ENERGY.Today + " kWh\n     Gestern: " + d.StatusSNS.ENERGY.Yesterday + " kWh\n     Gesamt: " + d.StatusSNS.ENERGY.Total + " kWh\n   Strom: " + d.StatusSNS.ENERGY.Current + " A\n   Spannung: " + d.StatusSNS.ENERGY.Voltage + " V";
                //chat.clearState();
                //msg.reply(m);
            } catch (e) {
                console.error(e)
            }
        }
    }).catch(error => {
        console.error("Error: " + error);
        msg.reply("Error: " + error);
    });

    axios.get("http://192.168.178.51/cm?cmnd=status%2011").then(response => {
        if (response.status === 200) {
            try {
                console.log(response);
                //const d = JSON.parse(response.data);
                const d = response.data;
                m = m + "\n   Schaltzustand: " + d.StatusSTS.POWER;
                chat.clearState();
                msg.reply(m);
            } catch (e) {
                console.error(e)
            }
        }
    }).catch(error => {
        console.error("Error: " + error);
        msg.reply("Error: " + error);
    });
}

if (msg.body === "!smart_1_toggle") {
    const chat = await msg.getChat();
    // simulates typing in the chat
    await chat.sendStateTyping();
    axios.get("http://192.168.178.46/cm?cmnd=Power%20TOGGLE").then(response => {
        if (response.status === 200) {
            try {
                console.log(response);
                //const d = JSON.parse(response.data);
                const d = response.data;
                m = "Neuer Schaltzustand: " + d.POWER;
                chat.clearState();
                msg.reply(m);
            } catch (e) {
                console.error(e)
            }
        }
    }).catch(error => {
        console.error("Error: " + error);
        msg.reply("Error: " + error);
    });
}

if (msg.body === "!smart_2_toggle") {
    const chat = await msg.getChat();
    // simulates typing in the chat
    await chat.sendStateTyping();
    axios.get("http://192.168.178.51/cm?cmnd=Power%20TOGGLE").then(response => {
        if (response.status === 200) {
            try {
                console.log(response);
                //const d = JSON.parse(response.data);
                const d = response.data;
                m = "Neuer Schaltzustand: " + d.POWER;
                chat.clearState();
                msg.reply(m);
            } catch (e) {
                console.error(e)
            }
        }
    }).catch(error => {
        console.error("Error: " + error);
        msg.reply("Error: " + error);
    });
}

});

client.on("group_join", (notification) => { const data = { event: "group_join", msg: "", notification: notification }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); // User has joined or been added to the group. console.log("join", notification); notification.reply("User joined."); });

client.on("group_leave", (notification) => { const data = { event: "group_leave", notification: notification, msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); // User has left or been kicked from the group. console.log("leave", notification); notification.reply("User left."); });

client.on("group_update", (notification) => { const data = { event: "group_update", notification: notification, msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); // Group picture, subject or description has been updated. console.log("update", notification); });

client.on("change_state", state => { const data = { event: "change_state", state: state, msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); console.log("CHANGE STATE", state); });

// Change to false if you don"t want to reject incoming calls let rejectCalls = true;

client.on("call", async (call) => { const data = { event: "call", call: call, msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); console.log("Call received, rejecting. GOTO Line 261 to disable", call); if (rejectCalls) await call.reject(); await client.sendMessage(call.from, [${call.fromMe ? "Outgoing" : "Incoming"}] Phone call from ${call.from}, type ${call.isGroup ? "group" : ""} ${call.isVideo ? "video" : "audio"} call. ${rejectCalls ? "This call was automatically rejected by the script." : ""}); });

client.on("disconnected", (reason) => { const data = { event: "disconnected", reason: reason, msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); console.log("Client was logged out", reason); });

client.on("contact_changed", async (message, oldId, newId, isContact) => { const data = { event: "contact_changed", message: message, oldId: oldId, newId: newId, isContact: isContact }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); /** The time the event occurred. */ const eventTime = (new Date(message.timestamp * 1000)).toLocaleString();

console.log(
    `The contact ${oldId.slice(0, -5)}` +
    `${!isContact ? " that participates in group " +
        `${(await client.getChatById(message.to ?? message.from)).name} ` : " "}` +
    `changed their phone number\nat ${eventTime}.\n` +
    `Their new phone number is ${newId.slice(0, -5)}.\n`);

/**
 * Information about the @param {message}:
 * 
 * 1. If a notification was emitted due to a group participant changing their phone number:
 * @param {message.author} is a participant"s id before the change.
 * @param {message.recipients[0]} is a participant"s id after the change (a new one).
 * 
 * 1.1 If the contact who changed their number WAS in the current user"s contact list at the time of the change:
 * @param {message.to} is a group chat id the event was emitted in.
 * @param {message.from} is a current user"s id that got an notification message in the group.
 * Also the @param {message.fromMe} is TRUE.
 * 
 * 1.2 Otherwise:
 * @param {message.from} is a group chat id the event was emitted in.
 * @param {message.to} is @type {undefined}.
 * Also @param {message.fromMe} is FALSE.
 * 
 * 2. If a notification was emitted due to a contact changing their phone number:
 * @param {message.templateParams} is an array of two user"s ids:
 * the old (before the change) and a new one, stored in alphabetical order.
 * @param {message.from} is a current user"s id that has a chat with a user,
 * whos phone number was changed.
 * @param {message.to} is a user"s id (after the change), the current user has a chat with.
 */

});

client.on("group_admin_changed", (notification) => { const data = { event: "group_admin_changed", notification: notification, msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); if (notification.type === "promote") { /** * Emitted when a current user is promoted to an admin. * {@link notification.author} is a user who performs the action of promoting/demoting the current user. / console.log(You were promoted by ${notification.author}); } else if (notification.type === "demote") /* Emitted when a current user is demoted to a regular user. */ console.log(You were demoted by ${notification.author}); });

client.on("group_membership_request", async (notification) => { const data = { event: "group_membership_request", notification: notification, msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); /** * The example of the {@link notification} output: * { * id: { * fromMe: false, * remote: "[email protected]", * id: "123123123132132132", * participant: "[email protected]", * _serialized: "[email protected][email protected]" * }, * body: "", * type: "created_membership_requests", * timestamp: 1694456538, * chatId: "[email protected]", * author: "[email protected]", * recipientIds: [] * } * / console.log(notification); /* You can approve or reject the newly appeared membership request: */ await client.approveGroupMembershipRequestss(notification.chatId, notification.author); await client.rejectGroupMembershipRequests(notification.chatId, notification.author); });

client.on("vote_update", (vote) => { const data = { event: "vote_update", vote: vote, msg: "" }; axios.post(webhookURL, data) .then(response => { console.log(Status: ${response.status}); console.log("Body: ", response.data); }) .catch(error => { console.error(Error: ${error}); }); /** The vote that was affected: */ console.log(vote); });

client.on("message_ack", (msg, ack) => { /* == ACK VALUES == ACK_ERROR: -1 ACK_PENDING: 0 ACK_SERVER: 1 ACK_DEVICE: 2 ACK_READ: 3 ACK_PLAYED: 4 */

const data = {
    event: "message_ack",
    ack: ack,
    msg: msg
};
axios.post(webhookURL, data)
    .then(response => {
        console.log(`Status: ${response.status}`);
        console.log("Body: ", response.data);
    })
    .catch(error => {
        console.error(`Error: ${error}`);
    });

if (ack == 3) {
    // The message was read
console.log("Nachricht wurde gelesen:");
console.log(msg);
}

});

client .initialize() .then(() => { console.log("Client initialized successfully"); }) .catch((err) => { console.error("Error initializing client", err); });`

Sorry the code format do not work. Has an idea why the vote_update event is not triggered?

Crocodile202405

crocodile2024 avatar Jun 09 '24 00:06 crocodile2024

Why doesn't it work anymore apart from displaying the person's vote?

918483 avatar Jun 27 '24 22:06 918483

Hi,

my understanding is that WWeb: v2.2412.54 got deprecated today and since this feature depends on it we lost the functionality. So we need this to be updated to work with v2.3xx.

Eraxorice avatar Jun 28 '24 08:06 Eraxorice