feat: send polls (#2377)

* feat: added Poll class

* feat: logic for sending polls added

* updated poll properties on returned message instance

* docs updated

* usage example updated

* module fixed

* 'pollSendOptions' changed to 'options'

* window.compareWwebVersions fixed

* small update to improve the performance of findImpl fix

* added option for custom message secret

* usage example updated
This commit is contained in:
alechkos 2023-10-19 19:37:30 +03:00 • committed by GitHub
commit 67d305be78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 131 additions and 6 deletions

View file

@ -51,7 +51,7 @@ class Message extends Base {
* Message content
* @type {string}
*/
this.body = this.hasMedia ? data.caption || '' : data.body || '';
this.body = this.hasMedia ? data.caption || '' : data.body || data.pollName || '';
/**
* Message type
@ -271,6 +271,20 @@ class Message extends Base {
this.selectedRowId = data.listResponse.singleSelectReply.selectedRowId;
}
if (this.type === MessageTypes.POLL_CREATION) {
this.pollName = data.pollName;
this.pollOptions = data.pollOptions;
this.allowMultipleAnswers = Boolean(!data.pollSelectableOptionsCount);
this.pollInvalidated = data.pollInvalidated;
this.isSentCagPollCreation = data.isSentCagPollCreation;
delete this._data.pollName;
delete this._data.pollOptions;
delete this._data.pollSelectableOptionsCount;
delete this._data.pollInvalidated;
delete this._data.isSentCagPollCreation;
}
return super._patch(data);
}

44
src/structures/Poll.js Normal file
View file

@ -0,0 +1,44 @@
'use strict';
/**
* Poll send options
* @typedef {Object} PollSendOptions
* @property {boolean} [allowMultipleAnswers=false] If false it is a single choice poll, otherwise it is a multiple choice poll (false by default)
* @property {?Array<number>} messageSecret The custom message secret, can be used as a poll ID. NOTE: it has to be a unique vector with a length of 32
*/
/** Represents a Poll on WhatsApp */
class Poll {
/**
* @param {string} pollName
* @param {Array<string>} pollOptions
* @param {PollSendOptions} options
*/
constructor(pollName, pollOptions, options = {}) {
/**
* The name of the poll
* @type {string}
*/
this.pollName = pollName.trim();
/**
* The array of poll options
* @type {Array.<{name: string, localId: number}>}
*/
this.pollOptions = pollOptions.map((option, index) => ({
name: option.trim(),
localId: index
}));
/**
* The send options for the poll
* @type {PollSendOptions}
*/
this.options = {
allowMultipleAnswers: options.allowMultipleAnswers === true,
messageSecret: options.messageSecret
};
}
}
module.exports = Poll;

View file

@ -19,4 +19,5 @@ module.exports = {
List: require('./List'),
Payment: require('./Payment'),
Reaction: require('./Reaction'),
Poll: require('./Poll'),
};