tangled
alpha
login
or
join now
dunkirk.sh
/
irc-slack-bridge
1
fork
atom
this repo has no description
1
fork
atom
overview
issues
pulls
pipelines
feat: type cdn and extract to lib
dunkirk.sh
3 months ago
3ef74b0c
0a212597
verified
This commit was signed with the committer's
known signature
.
dunkirk.sh
SSH Key Fingerprint:
SHA256:DqcG0RXYExE26KiWo3VxJnsxswN1QNfTBvB+bdSpk80=
+37
-26
3 changed files
expand all
collapse all
unified
split
src
index.ts
lib
cdn.ts
types.ts
+9
-26
src/index.ts
···
3
3
import { version } from "../package.json";
4
4
import { registerCommands } from "./commands";
5
5
import { channelMappings, userMappings } from "./db";
6
6
+
import { uploadToCDN } from "./lib/cdn";
6
7
import { parseIRCFormatting, parseSlackMarkdown } from "./parser";
7
8
import type { CachetUser } from "./types";
8
9
···
251
252
unfurl_media: true,
252
253
});
253
254
}
254
254
-
console.log(`IRC → Slack: <${nick}> ${text}`);
255
255
+
console.log(`IRC (${to}) → Slack: <${nick}> ${text}`);
255
256
} catch (error) {
256
257
console.error("Error posting to Slack:", error);
257
258
}
···
342
343
],
343
344
});
344
345
345
345
-
console.log(`IRC → Slack (action): ${actionText}`);
346
346
+
console.log(`IRC (${to}) → Slack (action): ${actionText}`);
346
347
},
347
348
);
348
349
349
350
// Slack event handlers
350
350
-
slackApp.event("message", async ({ payload, context }) => {
351
351
+
slackApp.event("message", async ({ payload }) => {
351
352
// Ignore bot messages and threaded messages
352
353
if (payload.subtype && payload.subtype !== "file_share") return;
353
354
if (payload.bot_id) return;
···
431
432
// Handle file uploads
432
433
if (payload.files && payload.files.length > 0) {
433
434
try {
434
434
-
// Extract private file URLs
435
435
const fileUrls = payload.files.map((file) => file.url_private);
436
436
-
437
437
-
// Upload to Hack Club CDN
438
438
-
const response = await fetch("https://cdn.hackclub.com/api/v3/new", {
439
439
-
method: "POST",
440
440
-
headers: {
441
441
-
Authorization: `Bearer ${process.env.CDN_TOKEN}`,
442
442
-
"X-Download-Authorization": `Bearer ${process.env.SLACK_BOT_TOKEN}`,
443
443
-
"Content-Type": "application/json",
444
444
-
},
445
445
-
body: JSON.stringify(fileUrls),
446
446
-
});
436
436
+
const data = await uploadToCDN(fileUrls);
447
437
448
448
-
if (response.ok) {
449
449
-
const data = await response.json();
450
450
-
451
451
-
// Send each uploaded file URL to IRC
452
452
-
for (const file of data.files) {
453
453
-
const fileMessage = `<${username}> ${file.deployedUrl}`;
454
454
-
ircClient.say(mapping.irc_channel, fileMessage);
455
455
-
console.log(`Slack → IRC (file): ${fileMessage}`);
456
456
-
}
457
457
-
} else {
458
458
-
console.error("Failed to upload files to CDN:", response.statusText);
438
438
+
for (const file of data.files) {
439
439
+
const fileMessage = `<${username}> ${file.deployedUrl}`;
440
440
+
ircClient.say(mapping.irc_channel, fileMessage);
441
441
+
console.log(`Slack → IRC (file): ${fileMessage}`);
459
442
}
460
443
} catch (error) {
461
444
console.error("Error uploading files to CDN:", error);
+21
src/lib/cdn.ts
···
1
1
+
import type { CDNUploadResponse } from "../types";
2
2
+
3
3
+
export async function uploadToCDN(
4
4
+
fileUrls: string[],
5
5
+
): Promise<CDNUploadResponse> {
6
6
+
const response = await fetch("https://cdn.hackclub.com/api/v3/new", {
7
7
+
method: "POST",
8
8
+
headers: {
9
9
+
Authorization: `Bearer ${process.env.CDN_TOKEN}`,
10
10
+
"X-Download-Authorization": `Bearer ${process.env.SLACK_BOT_TOKEN}`,
11
11
+
"Content-Type": "application/json",
12
12
+
},
13
13
+
body: JSON.stringify(fileUrls),
14
14
+
});
15
15
+
16
16
+
if (!response.ok) {
17
17
+
throw new Error(`CDN upload failed: ${response.statusText}`);
18
18
+
}
19
19
+
20
20
+
return (await response.json()) as CDNUploadResponse;
21
21
+
}
+7
src/types.ts
···
7
7
imageUrl: string;
8
8
expiration: string;
9
9
}
10
10
+
11
11
+
export interface CDNUploadResponse {
12
12
+
files: Array<{
13
13
+
deployedUrl: string;
14
14
+
originalUrl: string;
15
15
+
}>;
16
16
+
}