Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vote_update event #2596

Merged
merged 48 commits into from
Apr 30, 2024
Merged

feat: vote_update event #2596

merged 48 commits into from
Apr 30, 2024

Conversation

alechkos
Copy link
Collaborator

@alechkos alechkos commented Oct 23, 2023

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)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix/feature that would cause existing functionality to change)

Checklist

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

@alechkos alechkos changed the title feat: poll vote event feat: poll_update event Oct 25, 2023
@alechkos alechkos marked this pull request as ready for review October 25, 2023 02:47
This was referenced Nov 11, 2023
@alechkos alechkos changed the title feat: poll_update event feat: vote_update event Nov 14, 2023
Repository owner deleted a comment from abimtekno Apr 4, 2024
@Eraxorice Eraxorice mentioned this pull request Apr 4, 2024
7 tasks
@alechkos alechkos marked this pull request as draft April 16, 2024 02:52
auto-merge was automatically disabled April 16, 2024 02:52

Pull request was converted to draft

Repository owner locked and limited conversation to collaborators Apr 16, 2024
Repository owner unlocked this conversation Apr 19, 2024
@alechkos alechkos marked this pull request as ready for review April 22, 2024 05:10
@seowzhenjun0126
Copy link

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

@alechkos
Copy link
Collaborator Author

@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

@seowzhenjun0126
Copy link

Got it. Thanks for your help!

@felipeaffonsobsi
Copy link

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

@alechkos
Copy link
Collaborator Author

alechkos commented Apr 24, 2024

@felipeaffonsobsi

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

#2596 (comment)

@felipeaffonsobsi
Copy link

@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.

@alechkos alechkos requested review from PurpShell and shirser121 and removed request for aliyss and shirser121 April 29, 2024 10:13
@alechkos alechkos enabled auto-merge (squash) April 29, 2024 10:20
@alechkos alechkos merged commit 01714fe into pedroslopez:main Apr 30, 2024
1 check passed
ninajika pushed a commit to KDevRev/whatsapp-web.js that referenced this pull request May 2, 2024
* Merge branch 'add-polls' into polls-ext

* added listener to poll votes

* added isUnvote property

* added option for custom message secret

* usage example updated

* added logic for catching removed votes

* usage example updated

* fixed logic for poll vote events

* temp usage example

* No more `isCurrentState`

* messageSecret converted to array

Co-authored-by: tuyuribr <[email protected]>

* docs updated

* we already have messageSecret in vote.parentMessage

* PollVote.selectedOptions changed to be similar to pollOptions

* docs fixed

* window.injectToFunction clarifications

* minor clarifications

* fix: typo

* style: fix broken link in readme file

---------

Co-authored-by: tuyuribr <[email protected]>
seowzhenjun0126 added a commit to seowzhenjun0126/whatsapp-web.js that referenced this pull request May 6, 2024
ninajika added a commit to KDevRev/whatsapp-web.js that referenced this pull request May 7, 2024
@crocodile2024
Copy link

crocodile2024 commented Jun 9, 2024

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: "false_groupId@[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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Receive poll message voting How to trigger polls ??
8 participants