mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-18 19:49:16 +00:00
Poll implementation
This commit is contained in:
@@ -6,6 +6,7 @@ const Location = require('./Location');
|
||||
const Order = require('./Order');
|
||||
const Payment = require('./Payment');
|
||||
const { MessageTypes } = require('../util/Constants');
|
||||
const PollVote = require('./PollVote');
|
||||
|
||||
/**
|
||||
* Represents a Message on WhatsApp
|
||||
@@ -240,6 +241,19 @@ class Message extends Base {
|
||||
this.selectedRowId = data.listResponse.singleSelectReply.selectedRowId;
|
||||
}
|
||||
|
||||
if (this.type == MessageTypes.POLL_CREATION) {
|
||||
|
||||
/** Selectable poll options */
|
||||
this.pollOptions = data.pollOptions.map(option => {
|
||||
return option.name;
|
||||
});
|
||||
|
||||
/** Current poll votes, refresh with Message.refreshPollVotes() */
|
||||
this.pollVotes = data.pollVotes.map((pollVote) => {
|
||||
return new PollVote(this.client, pollVote);
|
||||
});
|
||||
}
|
||||
|
||||
return super._patch(data);
|
||||
}
|
||||
|
||||
@@ -527,6 +541,34 @@ class Message extends Base {
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the current poll votes
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async refreshPollVotes() {
|
||||
if (this.type != MessageTypes.POLL_CREATION) throw 'Invalid usage! Can only be used with a pollCreation message';
|
||||
const pollVotes = await this.client.evaluate((parentMsgId) => {
|
||||
return Store.PollVote.getForParent(parentMsgId).getModelsArray().map(a => a.serialize())
|
||||
}, this.id);
|
||||
this.pollVotes = pollVotes.map((pollVote) => {
|
||||
return new PollVote(this.client, pollVote);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vote to the poll.
|
||||
* @param {Array<string>} selectedOptions Array of options selected.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async vote(selectedOptions) {
|
||||
if (this.type != MessageTypes.POLL_CREATION) throw 'Invalid usage! Can only be used with a pollCreation message';
|
||||
|
||||
return this.client.evaluate((creationMsgId, selectedOptions) => {
|
||||
window.WWebJS.votePoll(creationMsgId, selectedOptions);
|
||||
}, this.id, selectedOptions);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Message;
|
||||
|
||||
Reference in New Issue
Block a user