Skip to content

Commit

Permalink
fix: delay for every iteration; send link instead of json
Browse files Browse the repository at this point in the history
  • Loading branch information
Myphz committed Jan 2, 2024
1 parent 6aed3b8 commit 6a5793d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ export const PEXELS_API_KEY =
process.env.PEXELS_API_KEY ?? process.env.PEXELS_API_KEY_DEBUG ?? throwError("PEXELS_API_KEY not set!");
// Maximum number of photos per message
export const TELEGRAM_MAX_PHOTOS_PER_MESSAGE = 10;
// Maximum number of photos per second, must be a multiple of the above
export const TELEGRAM_MAX_PHOTOS_PER_SECOND = 30;
2 changes: 1 addition & 1 deletion src/routes/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FilesAPI.get("/file", async (req: Request, res: Response) => {
return res.status(404).send();
}

res.json(file);
res.send(file);
});

FilesAPI.get("/download", async (req: Request, res: Response) => {
Expand Down
12 changes: 3 additions & 9 deletions src/telegram/telegram.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import TelegramBot, { InputMediaPhoto } from "node-telegram-bot-api";
import {
CHAT_ID,
TELEGRAM_MAX_PHOTOS_PER_MESSAGE,
TELEGRAM_MAX_PHOTOS_PER_SECOND,
TRIPS_BOT_API_TOKEN,
} from "../constants";
import { CHAT_ID, TELEGRAM_MAX_PHOTOS_PER_MESSAGE, TRIPS_BOT_API_TOKEN } from "../constants";
import { throwError } from "../utils/throw";
import { splitArrayIntoChunks } from "../utils/array";

Expand Down Expand Up @@ -39,9 +34,8 @@ export async function uploadFiles(files: Express.Multer.File[]) {
const uploadedChunk = await uploadFileGroup(fileChunks[i], mediaChunks[i]);
// Merge results
ret = { ...ret, ...uploadedChunk };
// Wait 1s after uploading TELEGRAM_MAX_PHOTOS_PER_SECOND photos
if (i && ((i + 1) * TELEGRAM_MAX_PHOTOS_PER_MESSAGE) % TELEGRAM_MAX_PHOTOS_PER_SECOND === 0)
await new Promise((res) => setTimeout(res, 1000));
// Wait 1s after every chunk
await new Promise((res) => setTimeout(res, 1000));
}

console.log("Files uploaded successfully");
Expand Down

0 comments on commit 6a5793d

Please sign in to comment.