summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/index.ts66
-rw-r--r--src/instagram_task.ts16
-rw-r--r--src/slack_reply_task.ts0
-rw-r--r--src/tasks.ts16
4 files changed, 68 insertions, 30 deletions
diff --git a/src/index.ts b/src/index.ts
index f184d13..a485a95 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,15 +1,59 @@
import { addTask, getTask } from "./tasks";
import { runDownloadInstagramTask } from "./instagram_task";
import { runWithRetries, wait } from "./utils";
+import { AppOptions, App as SlackApp } from "@slack/bolt";
+import fs from "fs";
+import dotenv from "dotenv";
+dotenv.config();
-// TESTING DATA
-addTask({
- type: "download_instagram",
- url: "https://www.instagram.com/reel/C33u39RBF5p/?igsh=MXZuZ2drN2wya2JmZg==",
- replyId: "123",
+const slackAppConfig: AppOptions = {
+ token: process.env.SLACK_BOT_TOKEN,
+ signingSecret: process.env.SLACK_SIGNING_SECRET,
+ socketMode: true,
+ appToken: process.env.SLACK_APP_TOKEN,
+ clientId: process.env.SLACK_CLIENT_ID,
+ clientSecret: process.env.SLACK_CLIENT_SECRET,
+};
+
+const app = new SlackApp(slackAppConfig);
+
+app.command("/veeti", async ({ command, ack, respond, client }) => {
+ await ack();
+ console.log("Received command: /veeti", command.text);
+ const url = command.text.trim();
+
+ if (!url.includes("instagram.com")) {
+ respond("Only instagram.com URLs are supported");
+ return;
+ }
+
+ const respondWithFile = async (message: string, filePath: string) => {
+ try {
+ console.log("Uploading file", filePath);
+ const fileContent = fs.readFileSync(filePath);
+ const extension = filePath.split(".").pop();
+ const fileName = `veeti.${extension}`;
+ console.log("Sending message to channel", command.channel_id);
+ await client.filesUploadV2({
+ channel_id: command.channel_id,
+ initial_comment: message,
+ file: fileContent,
+ filename: fileName,
+ });
+ console.log("Success");
+ } catch (e) {
+ console.error("Failed to upload file", e);
+ }
+ };
+
+ addTask({
+ type: "download_instagram",
+ url,
+ respondWithFile,
+ });
});
-(async () => {
+const taskLoop = async () => {
const MAX_RETRIES = 5;
// Main loop
while (true) {
@@ -19,8 +63,6 @@ addTask({
await runWithRetries(async () => {
if (task.type === "download_instagram") {
await runDownloadInstagramTask(task);
- } else if (task.type === "slack_reply") {
- console.log("TODO slack reply task");
} else {
console.log("Unknown task type, skipping", task);
}
@@ -32,4 +74,10 @@ addTask({
await wait(1000);
}
}
-})();
+};
+
+// run Puppeteer tasks in background
+void taskLoop();
+
+// start listening for Slack events
+app.start();
diff --git a/src/instagram_task.ts b/src/instagram_task.ts
index 6ab9838..7ca0c77 100644
--- a/src/instagram_task.ts
+++ b/src/instagram_task.ts
@@ -1,8 +1,13 @@
-import puppeteer, { Browser, Page, TimeoutError } from "puppeteer";
-import { InstagramTask, addTask } from "./tasks";
import fs from "fs";
+import puppeteer, { TimeoutError } from "puppeteer";
import { wait } from "./utils";
+export interface InstagramTask {
+ type: "download_instagram";
+ url: string;
+ respondWithFile: (message: string, filePath: string) => Promise<void>;
+}
+
export const runDownloadInstagramTask = async (task: InstagramTask) => {
const browser = await puppeteer.launch({
headless: false,
@@ -117,12 +122,7 @@ export const runDownloadInstagramTask = async (task: InstagramTask) => {
const downloadedFile = downloadedFiles[0];
- addTask({
- type: "slack_reply",
- replyId: task.replyId,
- message: "Here's the video you requested",
- filePath: `./downloads/${downloadedFile}`,
- });
+ await task.respondWithFile("🗿🗿🗿", `./downloads/${downloadedFile}`);
} finally {
await page.close();
await browser.close();
diff --git a/src/slack_reply_task.ts b/src/slack_reply_task.ts
deleted file mode 100644
index e69de29..0000000
--- a/src/slack_reply_task.ts
+++ /dev/null
diff --git a/src/tasks.ts b/src/tasks.ts
index 388dde8..18b6fe8 100644
--- a/src/tasks.ts
+++ b/src/tasks.ts
@@ -1,17 +1,7 @@
-export interface InstagramTask {
- type: "download_instagram";
- url: string;
- replyId: string;
-}
+import { InstagramTask } from "./instagram_task";
-export interface SlackReplyTask {
- type: "slack_reply";
- replyId: string;
- message: string;
- filePath: string;
-}
-
-export type Task = InstagramTask | SlackReplyTask;
+// union of possible task types
+export type Task = InstagramTask;
const taskQueue: Task[] = [];