Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added EditMessage method #1927

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/api/layers/controls.layer.ts
Original file line number Diff line number Diff line change
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
Loading