-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathAutomateLayer.ts
127 lines (113 loc) · 3.48 KB
/
AutomateLayer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { ListenerLayer } from './listener.layer';
import { Mutex } from 'async-mutex';
import { Browser, Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
export class AutomateLayer 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();
}
private async selectChatViaTyping(chatId: string): Promise<boolean> {
let 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(chatId)) {
return false;
}
try {
let search_element = await this.page.waitForXPath(xpath, {
timeout: 1000
});
// @ts-ignore
await search_element.click();
let phone_number = chatId.replace('@c.us', '');
await this.page.keyboard.type(' ' + phone_number + '\n', { delay: 200 });
await this.page.waitForTimeout(3000);
return true;
} catch (error) {
return false;
}
}
private async typeMultiLine(content: string): Promise<void> {
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');
}
}
/**
* Sends a photo or video to given chat, by injecting keystrokes
* @param to chat id: [email protected]
* @param fileName full path to media file
* @param caption media caption
*/
public async sendPhotoVideoViaTyping(
to: string,
fileName: string,
caption: string = ''
): Promise<boolean> {
let release = await this.typingMutex.acquire();
let success = await this.selectChatViaTyping(to);
if (!success) {
release();
return false;
}
let plus_button_xpath =
'//*[@id="main"]/footer/div[1]/div/span[2]/div/div[1]/div[2]/div/div/div/span';
try {
let plus_button = await this.page.waitForXPath(plus_button_xpath);
// @ts-ignore
await plus_button.click();
} catch (error) {
release();
return false;
}
for (let i = 0; i < 5; i++) {
await this.page.keyboard.press('ArrowUp');
await this.page.waitForTimeout(500);
}
const [fileChooser] = await Promise.all([
this.page.waitForFileChooser(),
this.page.evaluate(() => {
// @ts-ignore
document.activeElement.click();
})
]);
await fileChooser.accept([fileName]);
await this.page.waitForTimeout(1000);
await this.typeMultiLine(caption);
await this.page.keyboard.press('Enter');
release();
return true;
}
/**
* Sends a text message to given chat, by injecting keystrokes
* @param to chat id: [email protected]
* @param content text message
*/
public async sendTextViaTyping(to: string, content: string) {
let release = await this.typingMutex.acquire();
let success = await this.selectChatViaTyping(to);
if (success) {
release();
return false;
}
await this.typeMultiLine(content);
await this.page.keyboard.press('Enter');
release();
return true;
}
}