feat: Event messages (#3345)
* feat: add createCallLink function * feat: create Event class * feat: add functionality for sending Events * feat: add editEvent function * refactor: send event location prop as wa does * refactor: combine modules * feat: add sendResponseToEvent function * fix: createEventCallLink parameter * docs: add documentation * fix: eslint * refactor: change timestamp to Date for the user's convenience * refactor: rename 'Event' to 'ScheduledEvent' for more clarity * style: fix typo in inner function * refactor: remove unnecessary inner function * refactor: rename 'sendResponseToEvent' to 'sendResponseToScheduledEvent' for more clarity * fix: update the module name
This commit is contained in:
parent
ab321f3d7e
commit
f9db14d185
1 changed files with 261 additions and 4 deletions
71
src/structures/ScheduledEvent.js
Normal file
71
src/structures/ScheduledEvent.js
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* ScheduledEvent send options
|
||||
* @typedef {Object} ScheduledEventSendOptions
|
||||
* @property {?string} description The scheduled event description
|
||||
* @property {?Date} endTime The end time of the event
|
||||
* @property {?string} location The location of the event
|
||||
* @property {?string} callType The type of a WhatsApp call link to generate, valid values are: `video` | `voice`
|
||||
* @property {boolean} [isEventCanceled = false] Indicates if a scheduled event should be sent as an already canceled
|
||||
* @property {?Array<number>} messageSecret The custom message secret, can be used as an event ID. NOTE: it has to be a unique vector with a length of 32
|
||||
*/
|
||||
|
||||
/** Represents a ScheduledEvent on WhatsApp */
|
||||
class ScheduledEvent {
|
||||
/**
|
||||
* @param {string} name
|
||||
* @param {Date} startTime
|
||||
* @param {ScheduledEventSendOptions} options
|
||||
*/
|
||||
constructor(name, startTime, options = {}) {
|
||||
/**
|
||||
* The name of the event
|
||||
* @type {string}
|
||||
*/
|
||||
this.name = this._validateInputs('name', name).trim();
|
||||
|
||||
/**
|
||||
* The start time of the event
|
||||
* @type {number}
|
||||
*/
|
||||
this.startTimeTs = Math.floor(startTime.getTime() / 1000);
|
||||
|
||||
/**
|
||||
* The send options for the event
|
||||
* @type {Object}
|
||||
*/
|
||||
this.eventSendOptions = {
|
||||
description: options.description?.trim(),
|
||||
endTimeTs: options.endTime ? Math.floor(options.endTime.getTime() / 1000) : null,
|
||||
location: options.location?.trim(),
|
||||
callType: this._validateInputs('callType', options.callType),
|
||||
isEventCanceled: options.isEventCanceled ?? false,
|
||||
messageSecret: options.messageSecret
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Inner function to validate input values
|
||||
* @param {string} propName The property name to validate the value of
|
||||
* @param {string | number} propValue The property value to validate
|
||||
* @returns {string | number} The property value if a validation succeeded
|
||||
*/
|
||||
_validateInputs(propName, propValue) {
|
||||
if (propName === 'name' && !propValue) {
|
||||
throw new class CreateScheduledEventError extends Error {
|
||||
constructor(m) { super(m); }
|
||||
}(`Empty '${propName}' parameter value is provided.`);
|
||||
}
|
||||
|
||||
if (propName === 'callType' && propValue && !['video', 'voice'].includes(propValue)) {
|
||||
throw new class CreateScheduledEventError extends Error {
|
||||
constructor(m) { super(m); }
|
||||
}(`Invalid '${propName}' parameter value is provided. Valid values are: 'voice' | 'video'.`);
|
||||
}
|
||||
|
||||
return propValue;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ScheduledEvent;
|
||||
Loading…
Reference in a new issue