From 61c0a6be56e861936342c71793c2469acf72067c Mon Sep 17 00:00:00 2001 From: Alon Schwartzblat <63599777+Schwartzblat@users.noreply.github.com> Date: Tue, 21 Jun 2022 07:46:07 +0300 Subject: [PATCH] Reaction feature added. (#1400) * Fix get order. * Fix types. * Add set picture for profile and for groups. * Fix bug. * Fix * Fix types * Fix eslint * Add send reaction feature. * Add send reaction feature. * Add set picture for profile and for groups. * Add send reaction feature. * Add send reaction feature. * Add send reaction feature. * Add send reaction feature. * Add send reaction feature. * Add send reaction feature. * Update src/structures/Reaction.js Co-authored-by: Noah van der Aa * Bug fixes. * Bug fixes. * Bug fixes. * Fix * Fix * Fix example * Fix conflict * Fix conflict * Fix conflict * Fix conflict * Fix conflict * Fix conflict * move implementation to message model Co-authored-by: Noah van der Aa Co-authored-by: Pedro Lopez --- example.js | 4 +++- index.d.ts | 14 ++++++++------ src/Client.js | 6 +++--- src/structures/GroupChat.js | 2 +- src/structures/Message.js | 12 ++++++++++++ src/structures/index.js | 2 +- src/util/Injected.js | 2 +- src/util/Util.js | 1 - tests/client.js | 3 ++- 9 files changed, 31 insertions(+), 15 deletions(-) diff --git a/example.js b/example.js index 021efb3..3c84bab 100644 --- a/example.js +++ b/example.js @@ -1,4 +1,4 @@ -const { Client, Location, List, Buttons, LocalAuth } = require('./index'); +const { Client, Location, List, Buttons, LocalAuth} = require('./index'); const client = new Client({ authStrategy: new LocalAuth(), @@ -191,6 +191,8 @@ client.on('message', async msg => { let sections = [{title:'sectionTitle',rows:[{title:'ListItem1', description: 'desc'},{title:'ListItem2'}]}]; let list = new List('List body','btnText',sections,'Title','footer'); client.sendMessage(msg.from, list); + } else if (msg.body === '!reaction') { + msg.react('👍'); } }); diff --git a/index.d.ts b/index.d.ts index 12f4848..a242bf4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -114,7 +114,7 @@ declare namespace WAWebJS { /** Send a message to a specific chatId */ sendMessage(chatId: string, content: MessageContent, options?: MessageSendOptions): Promise - + /** Searches for messages */ searchMessages(query: string, options?: { chatId?: string, page?: number, limit?: number }): Promise @@ -141,7 +141,7 @@ declare namespace WAWebJS { * @param displayName New display name */ setDisplayName(displayName: string): Promise - + /** Changes and returns the archive state of the Chat */ unarchiveChat(chatId: string): Promise @@ -687,7 +687,7 @@ declare namespace WAWebJS { acceptGroupV4Invite: () => Promise<{status: number}>, /** Deletes the message from the chat */ delete: (everyone?: boolean) => Promise, - /** Downloads and returns the attatched message media */ + /** Downloads and returns the attached message media */ downloadMedia: () => Promise, /** Returns the Chat this message was sent in */ getChat: () => Promise, @@ -703,6 +703,8 @@ declare namespace WAWebJS { * If not, it will send the message in the same Chat as the original message was sent. */ reply: (content: MessageContent, chatId?: string, options?: MessageSendOptions) => Promise, + /** React to this message with an emoji*/ + react: (reaction: string) => Promise, /** * Forwards this message to another chat */ @@ -711,7 +713,7 @@ declare namespace WAWebJS { star: () => Promise, /** Unstar this message */ unstar: () => Promise, - /** Get information about message delivery statuso */ + /** Get information about message delivery status */ getInfo: () => Promise, /** * Gets the order associated with a given message @@ -816,7 +818,7 @@ declare namespace WAWebJS { static fromUrl: (url: string, options?: MediaFromURLOptions) => Promise } - export type MessageContent = string | MessageMedia | Location | Contact | Contact[] | List | Buttons + export type MessageContent = string | MessageMedia | Location | Contact | Contact[] | List | Buttons /** * Represents a Contact on WhatsApp @@ -1287,7 +1289,7 @@ declare namespace WAWebJS { constructor(body: string, buttonText: string, sections: Array, title?: string | null, footer?: string | null) } - /** Message type buttons */ + /** Message type Buttons */ export class Buttons { body: string | MessageMedia buttons: Array<{ buttonId: string; buttonText: {displayText: string}; type: number }> diff --git a/src/Client.js b/src/Client.js index 74499da..843a9fe 100644 --- a/src/Client.js +++ b/src/Client.js @@ -10,7 +10,7 @@ const { WhatsWebURL, DefaultOptions, Events, WAState } = require('./util/Constan const { ExposeStore, LoadUtils } = require('./util/Injected'); const ChatFactory = require('./factories/ChatFactory'); const ContactFactory = require('./factories/ContactFactory'); -const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification, Label, Call, Buttons, List } = require('./structures'); +const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification, Label, Call, Buttons, List} = require('./structures'); const LegacySessionAuth = require('./authStrategies/LegacySessionAuth'); const NoAuth = require('./authStrategies/NoAuth'); @@ -585,7 +585,7 @@ class Client extends EventEmitter { internalOptions.list = content; content = ''; } - + if (internalOptions.sendMediaAsSticker && internalOptions.attachment) { internalOptions.attachment = await Util.formatToWebpSticker( internalOptions.attachment, { @@ -749,7 +749,7 @@ class Client extends EventEmitter { return couldSet; } - + /** * Gets the current connection state for the client * @returns {WAState} diff --git a/src/structures/GroupChat.js b/src/structures/GroupChat.js index 5031d29..c11f5a9 100644 --- a/src/structures/GroupChat.js +++ b/src/structures/GroupChat.js @@ -147,7 +147,7 @@ class GroupChat extends Chat { this.groupMetadata.desc = description; return true; } - + /** * Updates the group settings to only allow admins to send messages. * @param {boolean} [adminsOnly=true] Enable or disable this option diff --git a/src/structures/Message.js b/src/structures/Message.js index 4a0c83c..94893cc 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -335,6 +335,18 @@ class Message extends Base { return this.client.sendMessage(chatId, content, options); } + /** + * React to this message with an emoji + * @param {string} reaction - Emoji to react with. Send an empty string to remove the reaction. + * @return {Promise} + */ + async react(reaction){ + await this.client.pupPage.evaluate(async (messageId, reaction) => { + const msg = await window.Store.Msg.get(messageId); + await window.Store.sendReactionToMsg(msg, reaction); + }, this.id._serialized, reaction); + } + /** * Accept Group V4 Invite * @returns {Promise} diff --git a/src/structures/index.js b/src/structures/index.js index 60a198c..fc5b212 100644 --- a/src/structures/index.js +++ b/src/structures/index.js @@ -17,5 +17,5 @@ module.exports = { Call: require('./Call'), Buttons: require('./Buttons'), List: require('./List'), - Payment: require('./Payment') + Payment: require('./Payment'), }; diff --git a/src/util/Injected.js b/src/util/Injected.js index 19ffe68..7eea752 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -49,6 +49,7 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.findCommonGroups = window.mR.findModule('findCommonGroups')[0].findCommonGroups; window.Store.StatusUtils = window.mR.findModule('setMyStatus')[0]; window.Store.ConversationMsgs = window.mR.findModule('loadEarlierMsgs')[0]; + window.Store.sendReactionToMsg = window.mR.findModule('sendReactionToMsg')[0].sendReactionToMsg; window.Store.StickerTools = { ...window.mR.findModule('toWebpSticker')[0], ...window.mR.findModule('addWebpMetadata')[0] @@ -99,7 +100,6 @@ exports.LoadUtils = () => { delete options.attachment; delete options.sendMediaAsSticker; } - let quotedMsgOptions = {}; if (options.quotedMessageId) { let quotedMessage = window.Store.Msg.get(options.quotedMessageId); diff --git a/src/util/Util.js b/src/util/Util.js index a3a6bb9..39a8a01 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -6,7 +6,6 @@ const { tmpdir } = require('os'); const ffmpeg = require('fluent-ffmpeg'); const webp = require('node-webpmux'); const fs = require('fs').promises; - const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k); /** diff --git a/tests/client.js b/tests/client.js index 7642d2f..2335905 100644 --- a/tests/client.js +++ b/tests/client.js @@ -332,6 +332,7 @@ describe('Client', function() { 'QueryOrder', 'QueryProduct', 'PresenceUtils', + 'ProfilePic', 'QueryExist', 'QueryProduct', 'QueryOrder', @@ -347,7 +348,7 @@ describe('Client', function() { 'Wap', 'WidFactory', 'findCommonGroups', - 'ProfilePic', + 'sendReactionToMsg', ]; const loadedModules = await client.pupPage.evaluate((expectedModules) => {