forked from pedroslopez/whatsapp-web.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: listen to group events (pedroslopez#90)
This allows you to listen for user joins or leaves, description updates, subject updates, picture updates, and group settings updates. Co-authored-by: Pedro S. Lopez <pedroslopez@me.com>
- Loading branch information
1 parent
588dc93
commit 48b9ae1
Showing
4 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict'; | ||
|
||
const Base = require('./Base'); | ||
|
||
/** | ||
* Represents a GroupNotification on WhatsApp | ||
* @extends {Base} | ||
*/ | ||
class GroupNotification extends Base { | ||
constructor(client, data) { | ||
super(client); | ||
|
||
if(data) this._patch(data); | ||
} | ||
|
||
_patch(data) { | ||
/** | ||
* ID that represents the groupNotification | ||
* @type {object} | ||
*/ | ||
this.id = data.id; | ||
|
||
/** | ||
* Extra content | ||
* @type {string} | ||
*/ | ||
this.body = data.body || ''; | ||
|
||
/** | ||
* GroupNotification type | ||
* @type {GroupNotificationTypes} | ||
*/ | ||
this.type = data.subtype; | ||
|
||
/** | ||
* Unix timestamp for when the groupNotification was created | ||
* @type {number} | ||
*/ | ||
this.timestamp = data.t; | ||
|
||
/** | ||
* ID for the Chat that this groupNotification was sent for. | ||
* | ||
* @type {string} | ||
*/ | ||
this.chatId = typeof (data.to) === 'object' ? data.to._serialized : data.to; | ||
|
||
/** | ||
* ContactId for the user that produced the GroupNotification. | ||
* @type {string} | ||
*/ | ||
this.author = typeof (data.author) === 'object' ? data.author._serialized : data.author; | ||
|
||
/** | ||
* Contact IDs for the users that were affected by this GroupNotification. | ||
* @type {Array<string>} | ||
*/ | ||
this.recipientIds = []; | ||
|
||
if (data.recipients) { | ||
this.recipientIds = data.recipients; | ||
} | ||
|
||
return super._patch(data); | ||
} | ||
|
||
/** | ||
* Returns the Chat this groupNotification was sent in | ||
* @returns {Promise<Chat>} | ||
*/ | ||
getChat() { | ||
return this.client.getChatById(this.chatId); | ||
} | ||
|
||
/** | ||
* Returns the Contact this GroupNotification was produced by | ||
* @returns {Promise<Contact>} | ||
*/ | ||
getContact() { | ||
return this.client.getContactById(this.author); | ||
} | ||
|
||
/** | ||
* Returns the Contacts affected by this GroupNotification. | ||
* @returns {Promise<Array<Contact>>} | ||
*/ | ||
async getRecipients() { | ||
return await Promise.all(this.recipientIds.map(async m => await this.client.getContactById(m))); | ||
} | ||
|
||
/** | ||
* Sends a message to the same chat this GroupNotification was produced in. | ||
* | ||
* @param {string|MessageMedia|Location} content | ||
* @param {object} options | ||
* @returns {Promise<Message>} | ||
*/ | ||
async reply(content, options={}) { | ||
return this.client.sendMessage(this.chatId, content, options); | ||
} | ||
|
||
} | ||
|
||
module.exports = GroupNotification; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters