Skip to content

Commit

Permalink
Merge pull request #2665 from 9cb14c1ec0/textviatyping
Browse files Browse the repository at this point in the history
Add sendTextViaTyping function
  • Loading branch information
orkestral committed Mar 29, 2024
2 parents a2ef04f + 8913c26 commit 936215b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ await client
console.error('Error when sending: ', erro); //return object error
});


// Send by injecting keystrokes into WhatsApp, thus maintaining the typing indicator
await client.sendTextViaTyping('[email protected]', '👋 Hello from venom!');

// Send location
await client
.sendLocation('[email protected]', '-13.6561589', '-69.7309264', 'Brasil')
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"webpack-cli": "^5.0.1"
},
"dependencies": {
"async-mutex": "^0.5.0",
"atob": "^2.1.2",
"axios": "^1.4.0",
"boxen": "^5.1.1",
Expand Down
46 changes: 46 additions & 0 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ import { Message, SendFileResult, SendStickerResult } from '../model';
import { ChatState } from '../model/enum';
import { ListenerLayer } from './listener.layer';
import { Scope, checkValuesSender } from '../helpers/layers-interface';
import { Mutex } from 'async-mutex';

let obj: Scope;

export class SenderLayer extends ListenerLayer {
private typingMutex: Mutex;
constructor(
public browser: Browser,
public page: Page,
session?: string,
options?: CreateConfig
) {
super(browser, page, session, options);
this.typingMutex = new Mutex();
}

public async createCommunity(name: string, description: string) {
Expand Down Expand Up @@ -362,6 +365,49 @@ export class SenderLayer extends ListenerLayer {
}
});
}

public async sendTextViaTyping(
to: string,
content: string
): Promise<boolean> {
const xpath = '//*[@id="side"]/div[1]/div/div[2]/div[2]/div/div[1]/p';
let ids = await this.page.evaluate(() => {
return WAPI.getAllChatIds();
});
if (!ids.includes(to)) {
return false;
}

let release = await this.typingMutex.acquire();
try {
let search_element = await this.page.waitForXPath(xpath);
// @ts-ignore
await search_element.click();
let phone_number = to.replace('@c.us', '');
await this.page.keyboard.type(' ' + phone_number + '\n', { delay: 200 });

content = content.replace('\r', '\n');
const lines = content.split(/\r?\n/);

for (let index = 0; index < lines.length; index++) {
let line = lines[index];
await this.page.keyboard.type(line, { delay: 200 });
await this.page.keyboard.down('Shift');
// Press the Enter key while the Shift key is down
await this.page.keyboard.press('Enter');
// Release the Shift key
await this.page.keyboard.up('Shift');
}

await this.page.keyboard.press('Enter');
release();
return true;
} catch (e) {
release();
return false;
}
}

/**
* Sends a text message to given chat
* @param to chat id: [email protected]
Expand Down
1 change: 1 addition & 0 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface WAPI {
skipMyMessages: boolean
) => Promise<object>;
getAllChats: () => Promise<Chat[] | object[]>;
getAllChatIds: () => Promise<string[]>;
getAllChatsWithMessages: (withNewMessageOnly?: boolean) => Chat[];
getAllChatsWithNewMsg: () => Chat[];
getAllContacts: () => Contact[];
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,13 @@ async-each@^1.0.1:
resolved "https://registry.npmjs.org/async-each/-/async-each-1.0.6.tgz"
integrity sha512-c646jH1avxr+aVpndVMeAfYw7wAa6idufrlN3LPA4PmKS0QEGp6PIC9nwz0WQkkvBGAMEki3pFdtxaF39J9vvg==

async-mutex@^0.5.0:
version "0.5.0"
resolved "https://registry.npmjs.org/async-mutex/-/async-mutex-0.5.0.tgz"
integrity sha512-1A94B18jkJ3DYq284ohPxoXbfTA5HsQ7/Mf4DEhcyLx3Bz27Rh59iScbB6EPiP+B+joue6YCxcMXSbFC1tZKwA==
dependencies:
tslib "^2.4.0"

[email protected]:
version "1.3.3"
resolved "https://registry.npmjs.org/async-retry/-/async-retry-1.3.3.tgz"
Expand Down Expand Up @@ -9166,7 +9173,7 @@ tslib@^1.8.1:
resolved "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.0.1, tslib@^2.1.0, tslib@^2.5.0:
tslib@^2.0.1, tslib@^2.1.0, tslib@^2.4.0, tslib@^2.5.0:
version "2.5.3"
resolved "https://registry.npmjs.org/tslib/-/tslib-2.5.3.tgz"
integrity sha512-mSxlJJwl3BMEQCUNnxXBU9jP4JBktcEGhURcPR6VQVlnP0FdDEsIaz0C35dXNGLyRfrATNofF0F5p2KPxQgB+w==
Expand Down

0 comments on commit 936215b

Please sign in to comment.