Skip to content

Commit

Permalink
feat: Added delay option for text messages (#1354)
Browse files Browse the repository at this point in the history
Co-authored-by: joselourenco <joselourenco@flipfloplab.com.br>
  • Loading branch information
JoseLourenc0 and JoseLourenc0 committed Sep 16, 2023
1 parent 55587c6 commit d0dc4a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/chat/defaultSendMessageOptions.ts
Expand Up @@ -32,4 +32,5 @@ export const defaultSendMessageOptions: AllMessageOptions = {
linkPreview: true,
markIsRead: true,
waitForAck: true,
delay: 0,
};
15 changes: 14 additions & 1 deletion src/chat/functions/prepareRawMessage.ts
Expand Up @@ -25,7 +25,12 @@ import {
unixTime,
} from '../../whatsapp/functions';
import { defaultSendMessageOptions, RawMessage, SendMessageOptions } from '..';
import { generateMessageID, getMessageById } from '.';
import {
generateMessageID,
getMessageById,
markIsComposing,
markIsPaused,
} from '.';

/**
* Prepare a raw message
Expand Down Expand Up @@ -53,6 +58,14 @@ export async function prepareRawMessage<T extends RawMessage>(
...message,
};

if (options.delay && message.type === 'chat') {
await markIsComposing(chat.id);
await new Promise((resolve) =>
setTimeout(() => resolve(true), options.delay)
);
await markIsPaused(chat.id);
}

if (message.type !== 'protocol') {
const ephemeral = getEphemeralFields(chat);

Expand Down
12 changes: 12 additions & 0 deletions src/chat/types.ts
Expand Up @@ -103,6 +103,18 @@ export interface SendMessageOptions {
* ```
*/
waitForAck?: boolean;
/**
* Delay some time (in ms) before sending message
* While delaying, Typing Status is used to look like a human interaction
* @default 0
* @example
* ```javascript
* WPP.chat.sendTextMessage('[number]@c.us', 'Delay with typing', {
* delay: 4000
* })
* ```
*/
delay?: number;
}

export interface SendMessageReturn {
Expand Down

0 comments on commit d0dc4a0

Please sign in to comment.