-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2665 from 9cb14c1ec0/textviatyping
Add sendTextViaTyping function
- Loading branch information
Showing
6 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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== | ||
|