Skip to content

Commit

Permalink
feat: Added client.editMessage function (#1927)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLourenc0 committed Nov 15, 2023
1 parent cdfcbee commit 394ef17
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/api/layers/controls.layer.ts
Expand Up @@ -15,10 +15,13 @@
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
*/

import { EditMessageOptions } from '@wppconnect/wa-js/dist/chat/functions/editMessage';
import { MsgKey } from '@wppconnect/wa-js/dist/whatsapp';
import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { evaluateAndReturn } from '../helpers';
import { UILayer } from './ui.layer';
import { Message } from '../model';

export class ControlsLayer extends UILayer {
constructor(public page: Page, session?: string, options?: CreateConfig) {
Expand Down Expand Up @@ -171,6 +174,44 @@ export class ControlsLayer extends UILayer {
return true;
}

/**
* Edits message of given message id
* @category Chat
* @param msgId The specific message id of the message to be edited
* @param newText New content of specified message
* @param options Common message options
*
* @example
* ```javascript
* // Simple message
* client.editMessage('true_<number>@c.us_messageId', 'new Text For Simple Message');
* ```
*/
public async editMessage(
msgId: string | MsgKey,
newText: string,
options: EditMessageOptions = {}
) {
const editResult = await evaluateAndReturn(
this.page,
({ msgId, newText, options }) =>
WPP.chat.editMessage(msgId, newText, options),
{ msgId, newText, options }
);

const result = (await evaluateAndReturn(
this.page,
async ({ messageId }) => {
return JSON.parse(JSON.stringify(await WAPI.getMessageById(messageId)));
},
{ messageId: editResult.id }
)) as Message;

if (result.body !== newText) throw editResult;

return result;
}

/**
* Stars message of given message id
* @category Chat
Expand Down

0 comments on commit 394ef17

Please sign in to comment.