-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.js
More file actions
556 lines (498 loc) · 18.1 KB
/
Copy pathBot.js
File metadata and controls
556 lines (498 loc) · 18.1 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import WebSocket from "ws";
import dotenv from "dotenv";
import http from "http";
import { exec } from "child_process";
import fs from "fs/promises";
import path from "path";
dotenv.config();
let BOT_USER_ID = process.env.BOT_USER_ID; // This is the User ID of the chat bot
const CLIENT_ID = process.env.CLIENT_ID_OF_APP;
const Redirect_URI = process.env.REDIRECT_URI_OF_APP;
let CHAT_CHANNEL_USER_ID = process.env.STREAMER_USER_ID; // This is the User ID of the channel that the bot will join and listen to chat messages of
const EVENTSUB_WEBSOCKET_URL = process.env.EVENTSUB_WEBSOCKET_URL;
let OAUTH_TOKEN = process.env.BOT_OAUTH_TOKEN;
let STREAMER_OAUTH_TOKEN = process.env.STREAMER_OAUTH_TOKEN;
const BOT_USERNAME = process.env.BOT_USERNAME;
const STREAMER_USERNAME = process.env.STREAMER_USERNAME;
const GITHUB_URL = process.env.GITHUB_URL;
let botWebsocketSessionID;
let streamerWebsocketSessionID;
// Start executing the bot from here
(async () => {
if (!OAUTH_TOKEN) {
OAUTH_TOKEN = await getOAuthToken();
}
if (!STREAMER_OAUTH_TOKEN) {
STREAMER_OAUTH_TOKEN = await getStreamerOAuthToken();
}
if (!BOT_USER_ID) {
BOT_USER_ID = await getUserIdFromApi(
OAUTH_TOKEN,
BOT_USERNAME,
"BOT_USER_ID",
);
}
if (!CHAT_CHANNEL_USER_ID) {
CHAT_CHANNEL_USER_ID = await getUserIdFromApi(
STREAMER_OAUTH_TOKEN,
STREAMER_USERNAME,
"STREAMER_USER_ID",
);
}
// Verify that the authentication is valid
const botAuth = await getAuth(OAUTH_TOKEN, "BOT_OAUTH_TOKEN");
const streamerAuth = await getAuth(
STREAMER_OAUTH_TOKEN,
"STREAMER_OAUTH_TOKEN",
);
if (BOT_USERNAME && STREAMER_USERNAME) {
const botName = BOT_USERNAME.trim().toLowerCase();
const streamerName = STREAMER_USERNAME.trim().toLowerCase();
if (botName && streamerName && botName === streamerName) {
console.warn(
"Warning: BOT_USERNAME and STREAMER_USERNAME are the same. This is unusual unless you intentionally use one account for both.",
);
}
}
warnIfUsernameMismatch(botAuth, BOT_USERNAME, "BOT_OAUTH_TOKEN");
warnIfUsernameMismatch(
streamerAuth,
STREAMER_USERNAME,
"STREAMER_OAUTH_TOKEN",
);
// Start WebSocket client and register handlers
const botWebsocketClient = startWebSocketClient(handleBotWebSocketMessage);
const streamerWebsocketClient = startWebSocketClient(
handleStreamerWebSocketMessage,
);
})();
// WebSocket will persist the application loop until you exit the program forcefully
async function getAuth(token, label) {
// Validate OAuth token
// https://dev.twitch.tv/docs/authentication/validate-tokens/#how-to-validate-a-token
let response = await fetch("https://id.twitch.tv/oauth2/validate", {
method: "GET",
headers: {
Authorization: "OAuth " + token,
},
});
if (response.status != 200) {
let data = await response.json();
console.error(
`${label} is not valid. /oauth2/validate returned status code ${response.status}`,
);
console.error(data);
process.exit(1);
}
const data = await response.json();
console.log(`Validated ${label}.`);
return data;
}
async function getOAuthToken() {
// Implicit grant flow - Get OAuth token
// https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#implicit-grant-flow
let Scopes = [
"user:bot",
"user:read:chat",
"user:write:chat",
];
return await authorizeWithLocalCallback("bot", Scopes);
}
async function getStreamerOAuthToken() {
let Scopes = ["channel:read:subscriptions", "moderator:read:followers"];
return await authorizeWithLocalCallback("streamer", Scopes);
}
async function authorizeWithLocalCallback(label, scopes) {
const redirectUrl = new URL(Redirect_URI);
if (redirectUrl.hostname !== "localhost") {
console.error(
`REDIRECT_URI_OF_APP must be localhost for local callback auth. Current: ${Redirect_URI}`,
);
process.exit(1);
}
console.log(
`Authorize the ${label} account in the browser window that opens next.`,
);
const authUrl = `https://id.twitch.tv/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=${Redirect_URI}&response_type=token&force_verify=true&scope=${scopes.join("%20")}`;
const token = await startLocalAuthServer(redirectUrl, label, authUrl);
const envKey = `${label.toUpperCase()}_OAUTH_TOKEN`;
await saveEnvValue(envKey, token);
console.log(
`Captured ${label} token. Saved to .env as ${envKey}.`,
);
return token;
}
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function warnIfUsernameMismatch(authData, expectedUsername, label) {
if (!authData || !expectedUsername) {
return;
}
const expected = expectedUsername.trim().toLowerCase();
const actual = String(authData.login || "").trim().toLowerCase();
if (expected && actual && expected !== actual) {
console.warn(
`Warning: ${label} belongs to "${authData.login}", but the configured username is "${expectedUsername}".`,
);
}
}
function upsertEnvValue(content, key, value) {
const line = `${key}=${value}`;
const keyPattern = new RegExp(`^${escapeRegExp(key)}=.*$`, "m");
if (keyPattern.test(content)) {
return content.replace(keyPattern, line);
}
const trimmed = content.replace(/\s*$/, "");
const separator = trimmed.length ? "\n" : "";
return `${trimmed}${separator}${line}\n`;
}
async function saveEnvValue(key, value) {
const envPath = path.join(process.cwd(), ".env");
let content = "";
try {
content = await fs.readFile(envPath, "utf8");
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
}
const updated = upsertEnvValue(content, key, value);
if (updated !== content) {
await fs.writeFile(envPath, updated, "utf8");
}
}
async function getUserIdFromApi(token, username, envKey) {
if (!username) {
console.error(
`${envKey} not set and no username provided. Set ${envKey} or ${
envKey === "BOT_USER_ID" ? "BOT_USERNAME" : "STREAMER_USERNAME"
} in .env.`,
);
process.exit(1);
}
const response = await fetch(
`https://api.twitch.tv/helix/users?login=${encodeURIComponent(
username,
)}`,
{
method: "GET",
headers: {
Authorization: "Bearer " + token,
"Client-Id": CLIENT_ID,
},
},
);
if (!response.ok) {
const data = await response.json().catch(() => ({}));
console.error(`Failed to fetch ${envKey} for ${username}`);
console.error(data);
process.exit(1);
}
const data = await response.json();
const userId = data?.data?.[0]?.id;
if (!userId) {
console.error(`No user found for ${username}`);
process.exit(1);
}
await saveEnvValue(envKey, userId);
console.log(`Saved ${envKey} to .env for ${username}.`);
return userId;
}
function openBrowser(url) {
const quoted = `"${url}"`;
const command =
process.platform === "darwin"
? `open ${quoted}`
: process.platform === "win32"
? `start ${quoted}`
: `xdg-open ${quoted}`;
exec(command, (error) => {
if (error) {
console.log("Open this URL in your browser to authorize:");
console.log(url);
}
});
}
function startLocalAuthServer(redirectUrl, label, authUrl) {
return new Promise((resolve, reject) => {
const port = redirectUrl.port || "3000";
const path = redirectUrl.pathname || "/";
const server = http.createServer((req, res) => {
if (req.method === "GET" && req.url?.startsWith(path)) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(`<!doctype html>
<html>
<head><meta charset="utf-8"><title>Twitch Auth</title></head>
<body>
<h1>Authorizing ${label} account...</h1>
<p>Please make sure you are logged in as the ${label} account.</p>
<p>You can close this tab after success.</p>
<script>
const hash = window.location.hash.slice(1);
const params = new URLSearchParams(hash);
const token = params.get("access_token");
if (token) {
fetch("/token", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token })
}).then(() => {
document.body.innerHTML = "<h1>Token received. You can close this tab.</h1>";
});
} else {
document.body.innerHTML = "<h1>No token found.</h1>";
}
</script>
</body>
</html>`);
return;
}
if (req.method === "POST" && req.url === "/token") {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
req.on("end", () => {
try {
const data = JSON.parse(body);
if (!data.token) {
throw new Error("Missing token");
}
res.writeHead(200);
res.end("OK");
server.close();
resolve(data.token);
} catch (err) {
res.writeHead(400);
res.end("Invalid token payload");
reject(err);
}
});
return;
}
res.writeHead(404);
res.end("Not found");
});
server.listen(Number(port), "localhost", () => {
console.log(
`Local auth server listening on http://localhost:${port}${path}`,
);
openBrowser(authUrl);
});
});
}
function startWebSocketClient(onMessage) {
let websocketClient = new WebSocket(EVENTSUB_WEBSOCKET_URL);
websocketClient.on("error", console.error);
websocketClient.on("open", () => {
console.log("WebSocket connection opened to " + EVENTSUB_WEBSOCKET_URL);
});
websocketClient.on("message", (data) => {
onMessage(JSON.parse(data.toString()));
});
return websocketClient;
}
function handleBotWebSocketMessage(data) {
switch (data.metadata.message_type) {
case "session_welcome": // First message you get from the WebSocket server when connecting
botWebsocketSessionID = data.payload.session.id; // Register the Session ID it gives us
// Listen to EventSub, which joins the chatroom from your bot's account
registerChatMessageListener();
break;
case "notification": // An EventSub notification has occurred, such as channel.chat.message
switch (data.metadata.subscription_type) {
case "channel.chat.message":
// First, print the message to the program's console.
console.log(
`MSG #${data.payload.event.broadcaster_user_login} <${data.payload.event.chatter_user_login}> ${data.payload.event.message.text}`,
);
// Then check to see if that message was "HeyGuys"
const messageText =
data.payload.event.message.text.trim();
const messageLower = messageText.toLowerCase();
if (messageText == "HeyGuys") {
// If so, send back "VoHiYo" to the chatroom
sendChatMessage("VoHiYo");
}
if (messageLower == "!lurk") {
sendChatMessage(
`Viel Spaß im Lurk, ${data.payload.event.chatter_user_name}!`,
);
}
if (messageLower == "!github") {
if (!GITHUB_URL) {
sendChatMessage(
"GitHub-Link fehlt. Bitte GITHUB_URL in der .env setzen.",
);
} else {
sendChatMessage(`GitHub: ${GITHUB_URL}`);
}
}
break;
}
break;
}
}
function handleStreamerWebSocketMessage(data) {
switch (data.metadata.message_type) {
case "session_welcome":
streamerWebsocketSessionID = data.payload.session.id;
registerSubscriptionListener();
registerFollowListener();
break;
case "notification":
switch (data.metadata.subscription_type) {
case "channel.subscribe":
console.log(
`SUB #${data.payload.event.broadcaster_user_login} <${data.payload.event.user_login}>`,
);
sendChatMessage(
`Danke für den Sub, ${data.payload.event.user_name}!`,
);
break;
case "channel.follow":
console.log(
`FOLLOW #${data.payload.event.broadcaster_user_login} <${data.payload.event.user_login}>`,
);
sendChatMessage(
`Danke fürs Folgen, ${data.payload.event.user_name}!`,
);
break;
}
break;
}
}
async function sendChatMessage(chatMessage) {
let response = await fetch("https://api.twitch.tv/helix/chat/messages", {
method: "POST",
headers: {
Authorization: "Bearer " + OAUTH_TOKEN,
"Client-Id": CLIENT_ID,
"Content-Type": "application/json",
},
body: JSON.stringify({
broadcaster_id: CHAT_CHANNEL_USER_ID,
sender_id: BOT_USER_ID,
message: chatMessage,
}),
});
if (response.status != 200) {
let data = await response.json();
console.error("Failed to send chat message");
console.error(data);
} else {
console.log("Sent chat message: " + chatMessage);
}
}
async function registerChatMessageListener() {
// Register channel.chat.message
let response_chat_message = await fetch(
"https://api.twitch.tv/helix/eventsub/subscriptions",
{
method: "POST",
headers: {
Authorization: "Bearer " + OAUTH_TOKEN,
"Client-Id": CLIENT_ID,
"Content-Type": "application/json",
},
body: JSON.stringify({
type: "channel.chat.message",
version: "1",
condition: {
broadcaster_user_id: CHAT_CHANNEL_USER_ID,
user_id: BOT_USER_ID,
},
transport: {
method: "websocket",
session_id: botWebsocketSessionID,
},
}),
},
);
if (response_chat_message.status != 202) {
let data = await response_chat_message.json();
console.error(
"Failed to subscribe to channel.chat.message. API call returned status code " +
response_chat_message.status,
);
console.error(data);
process.exit(1);
} else {
const data = await response_chat_message.json();
console.log(`Subscribed to channel.chat.message [${data.data[0].id}]`);
}
}
async function registerSubscriptionListener() {
let response_subscription = await fetch(
"https://api.twitch.tv/helix/eventsub/subscriptions",
{
method: "POST",
headers: {
Authorization: "Bearer " + STREAMER_OAUTH_TOKEN,
"Client-Id": CLIENT_ID,
"Content-Type": "application/json",
},
body: JSON.stringify({
type: "channel.subscribe",
version: "1",
condition: {
broadcaster_user_id: CHAT_CHANNEL_USER_ID,
},
transport: {
method: "websocket",
session_id: streamerWebsocketSessionID,
},
}),
},
);
if (response_subscription.status != 202) {
let data = await response_subscription.json();
console.error(
"Failed to subscribe to channel.subscribe. API call returned status code " +
response_subscription.status,
);
console.error(data);
process.exit(1);
} else {
const data2 = await response_subscription.json();
console.log(`Subscribed to channel.subscribe [${data2.data[0].id}]`);
}
}
async function registerFollowListener() {
let response_follow = await fetch(
"https://api.twitch.tv/helix/eventsub/subscriptions",
{
method: "POST",
headers: {
Authorization: "Bearer " + STREAMER_OAUTH_TOKEN,
"Client-Id": CLIENT_ID,
"Content-Type": "application/json",
},
body: JSON.stringify({
type: "channel.follow",
version: "2",
condition: {
broadcaster_user_id: CHAT_CHANNEL_USER_ID,
moderator_user_id: CHAT_CHANNEL_USER_ID,
},
transport: {
method: "websocket",
session_id: streamerWebsocketSessionID,
},
}),
},
);
if (response_follow.status != 202) {
let data = await response_follow.json();
console.error(
"Failed to subscribe to channel.follow. API call returned status code " +
response_follow.status,
);
console.error(data);
process.exit(1);
} else {
const data3 = await response_follow.json();
console.log(`Subscribed to channel.follow [${data3.data[0].id}]`);
}
}