-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (132 loc) · 4.16 KB
/
index.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require("dotenv").config();
const { App } = require("@slack/bolt");
const puppeteer = require("puppeteer");
const app = new App({
signingSecret: process.env.SIGNING_SECRET,
token: process.env.SLACK_TOKEN,
socketMode: true,
appToken: process.env.SLACK_APP_TOKEN,
});
async function login(page) {
const domainInput = await page.waitForSelector(
'input[data-qa="signin_domain_input"]'
);
// Cookie popup
const cookiePopup = await page.$("#onetrust-accept-btn-handler");
if (cookiePopup) {
await cookiePopup.click();
await new Promise((resolve) => setTimeout(resolve, 1000));
}
await domainInput.type("rareagency");
await new Promise((resolve) => setTimeout(resolve, 1000));
await page.click('button[data-qa="submit_team_domain_button"]');
const email = await page.waitForSelector('input[data-qa="login_email"]');
await email.type(process.env.SLACK_USERNAME);
await page.type(
'input[data-qa="login_password"]',
process.env.SLACK_PASSWORD
);
await new Promise((resolve) => setTimeout(resolve, 1000));
await page.click('button[data-qa="signin_button"]');
}
let currentBrowser;
async function open(url) {
console.log("📞 Joining call", url);
if (currentBrowser) {
currentBrowser.close();
}
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
executablePath: process.env.CHROMIUM_EXECUTABLE,
userDataDir: "./data",
args: [
"--start-fullscreen",
"--kiosk",
"--disable-infobars",
"--disable-session-crashed-bubble",
"--noerrdialogs",
],
ignoreDefaultArgs: ["--enable-automation"],
});
currentBrowser = browser;
const context = browser.defaultBrowserContext();
context.overridePermissions(url, ["camera", "microphone"]);
const page = await browser.newPage();
await page.goto(url, {
timeout: 0
});
if (page.url().includes("workspace-signin")) {
await login();
} else {
console.log('Already logged in');
}
// Turn camera on
const videoButton = await page.waitForSelector(
'button[data-qa="video-button"]',
{
timeout: null,
}
);
const [isCameraOn] = await page.$$eval("button[data-qa='video-button']", el => el.map(x => x.getAttribute("aria-checked")));
if (isCameraOn !== "true") {
console.log("Enabled camera");
await videoButton.click();
}
// Turn mic on
const micButton = await page.waitForSelector('button[data-qa="mic-button"]', {
timeout: null,
});
const [isMicOn] = await page.$$eval("button[data-qa='mic-button']", el => el.map(x => x.getAttribute("aria-checked")));
if (isMicOn !== "true") {
console.log("Unmuted microphone");
await micButton.click();
}
/*
* Auto leave
*/
const roomEmptyText = () => page.$(".p-active_call__empty_message_heading");
const startedWaiting = Date.now();
const fiveMinutes = 1000 * 60 * 5;
let roomEmpty = await roomEmptyText();
if (roomEmpty) {
console.log("Room is empty, waiting for others");
while (roomEmpty) {
const waitedOver5Minutes = Date.now() - startedWaiting < fiveMinutes;
if (waitedOver5Minutes) {
console.log("Waited for 5 minutes in an empty room, leaving");
await currentBrowser.close();
currentBrowser = null;
return;
}
roomEmpty = await roomEmptyText();
await new Promise((resolve) => setTimeout(resolve, 10000));
}
} else {
console.log("Someone is in the room already");
}
// Leave the room once everyone else is gone
while (!roomEmpty) {
roomEmpty = await roomEmptyText();
console.log("Room currently empty", Boolean(roomEmpty));
await new Promise((resolve) => setTimeout(resolve, 10000));
}
console.log("Everyone left, bye");
await currentBrowser.close();
currentBrowser = null;
}
// Listens to incoming messages that contain "hello"
app.message(async ({ message }) => {
if (message.subtype === "sh_room_created") {
try {
await open(`https://app.slack.com/free-willy/TTHGF5X5Y/${message.room.id}`);
} catch (error) {
console.error(error);
}
}
});
(async () => {
// Start your app
await app.start(process.env.PORT || 3000);
console.log("⚡️ Bolt app is running!");
})();