Skip to content

Commit

Permalink
Improved: photos (and other file types) transferred as groups (albums…
Browse files Browse the repository at this point in the history
…) both directions

Fixed: lost caption if one file in album failed on transfer
  • Loading branch information
kutuk committed Aug 4, 2023
1 parent d460409 commit 0305b53
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 110 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tedicross",
"version": "0.12.0",
"version": "0.12.1",
"description": "Bridging Telegram and Discord",
"license": "MIT",
"repository": {
Expand Down
287 changes: 187 additions & 100 deletions src/discord2telegram/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,130 +230,217 @@ export function setup(
// For now: ignoring captions - always send as standalone message

// Check for attachments and pass them on
// TODO: limit = 10 files (if more - split)
const images = []; // size limit = 10 mb
const videos = []; // size limit = 20 mb
const audios = [];
const documents = [];
const images: InputMediaPhoto[] = [];
const videos: InputMediaVideo[] = [];
const audios: InputMediaAudio[] = [];
const documents: InputMediaDocument[] = [];

const handleMediaFile = (attachment: any, type: "video" | "photo" | "audio" | "document") => {
const maxFileSize = type === "video" ? 20000000 : 10000000;

if (attachment.size < maxFileSize) {
const mediaFile = { media: { url: attachment.url, filename: attachment.name }, type };
switch (type) {
case "video":
videos.push(mediaFile as InputMediaVideo);
break;
case "photo":
images.push(mediaFile as InputMediaPhoto);
break;
case "audio":
audios.push(mediaFile as InputMediaAudio);
break;
case "document":
documents.push(mediaFile as InputMediaDocument);
break;
}
} else {
logger.error(`[${bridge.name}] Too big attachment ${type} File: ${attachment.name}`);
}
};

for (const attachment of message.attachments.values()) {
const fileType: string = attachment.contentType || "";
if (fileType.indexOf("video") >= 0) {
if (attachment.size < 20000000) {
videos.push({
media: { url: attachment.url },
type: "video"
} as InputMediaVideo);
} else {
logger.error(`[${bridge.name}] Too big attachment Video File: ${attachment.name}`);
}
handleMediaFile(attachment, "video");
} else if (fileType.indexOf("image") >= 0) {
if (attachment.size < 10000000) {
images.push({ media: { url: attachment.url }, type: "photo" } as InputMediaPhoto);
} else {
logger.error(`[${bridge.name}] Too big attachment Image File: ${attachment.name}`);
}
handleMediaFile(attachment, "photo");
} else if (fileType.indexOf("audio") >= 0) {
if (attachment.size < 20000000) {
audios.push({ media: { url: attachment.url }, type: "audio" } as InputMediaAudio);
} else {
logger.error(`[${bridge.name}] Too big attachment Audio File: ${attachment.name}`);
}
handleMediaFile(attachment, "audio");
} else {
if (attachment.size < 20000000) {
documents.push({ media: { url: attachment.url }, type: "document" } as InputMediaDocument);
} else {
logger.error(`[${bridge.name}] Too big attachment Document File: ${attachment.name}`);
}
handleMediaFile(attachment, "document");
}
}

// TODO refactoring - combine in one cycle
// let tgMessage;
if (images.length) {
try {
if (images.length > 1) {
// tgMessage =
await tgBot.telegram.sendMediaGroup(bridge.telegram.chatId, images, {
reply_to_message_id: +replyId
});
} else {
// tgMessage =
await tgBot.telegram.sendPhoto(bridge.telegram.chatId, images[0].media, {
reply_to_message_id: +replyId
});
}
// NOTE: disabled message mapping for galleries
// messageMap.insert(
// MessageMap.DISCORD_TO_TELEGRAM,
// bridge,
// message.id,
// tgMessage.message_id.toString()
// );
} catch (err) {
logger.error(
`[${bridge.name}] Telegram did not accept an Image attachment:`,
(err as Error).toString()
);
}
}
const mediaArray = [];
if (videos.length) mediaArray.push([...videos]);
if (audios.length) mediaArray.push([...audios]);
if (images.length) mediaArray.push([...images]);
if (documents.length) mediaArray.push([...documents]);

if (videos.length) {
for (const oneArray of mediaArray) {
const type = oneArray[0].type;
try {
if (videos.length > 1) {
await tgBot.telegram.sendMediaGroup(bridge.telegram.chatId, videos, {
if (oneArray.length > 1) {
await tgBot.telegram.sendMediaGroup(bridge.telegram.chatId, oneArray, {
reply_to_message_id: +replyId
});
} else {
await tgBot.telegram.sendVideo(bridge.telegram.chatId, videos[0].media, {
reply_to_message_id: +replyId
});
switch (type) {
case "video":
await tgBot.telegram.sendVideo(bridge.telegram.chatId, oneArray[0].media, {
reply_to_message_id: +replyId
});
break;
case "audio":
await tgBot.telegram.sendAudio(bridge.telegram.chatId, oneArray[0].media, {
reply_to_message_id: +replyId
});
break;
case "photo":
await tgBot.telegram.sendPhoto(bridge.telegram.chatId, oneArray[0].media, {
reply_to_message_id: +replyId
});
break;
case "document":
await tgBot.telegram.sendDocument(bridge.telegram.chatId, oneArray[0].media, {
reply_to_message_id: +replyId
});
break;
}
}
} catch (err) {
logger.error(
`[${bridge.name}] Telegram did not accept Video attachment:`,
`[${bridge.name}] Telegram did not accept ${type} attachment:`,
(err as Error).toString()
);
}
}

if (audios.length) {
try {
if (videos.length > 1) {
await tgBot.telegram.sendMediaGroup(bridge.telegram.chatId, audios, {
reply_to_message_id: +replyId
});
} else {
await tgBot.telegram.sendAudio(bridge.telegram.chatId, audios[0].media, {
reply_to_message_id: +replyId
});
}
} catch (err) {
logger.error(
`[${bridge.name}] Telegram did not accept Audio attachment:`,
(err as Error).toString()
);
}
}
// const images = []; // size limit = 10 mb
// const videos = []; // size limit = 20 mb
// const audios = [];
// const documents = [];
// const mediaArrays = [];
//
// for (const attachment of message.attachments.values()) {
// const fileType: string = attachment.contentType || "";
// if (fileType.indexOf("video") >= 0) {
// if (attachment.size < 20000000) {
// videos.push({
// media: { url: attachment.url },
// type: "video"
// } as InputMediaVideo);
// } else {
// logger.error(`[${bridge.name}] Too big attachment Video File: ${attachment.name}`);
// }
// } else if (fileType.indexOf("image") >= 0) {
// if (attachment.size < 10000000) {
// images.push({ media: { url: attachment.url }, type: "photo" } as InputMediaPhoto);
// } else {
// logger.error(`[${bridge.name}] Too big attachment Image File: ${attachment.name}`);
// }
// } else if (fileType.indexOf("audio") >= 0) {
// if (attachment.size < 20000000) {
// audios.push({ media: { url: attachment.url }, type: "audio" } as InputMediaAudio);
// } else {
// logger.error(`[${bridge.name}] Too big attachment Audio File: ${attachment.name}`);
// }
// } else {
// if (attachment.size < 20000000) {
// documents.push({ media: { url: attachment.url }, type: "document" } as InputMediaDocument);
// } else {
// logger.error(`[${bridge.name}] Too big attachment Document File: ${attachment.name}`);
// }
// }
// }

if (documents.length) {
try {
if (videos.length > 1) {
await tgBot.telegram.sendMediaGroup(bridge.telegram.chatId, documents, {
reply_to_message_id: +replyId
});
} else {
await tgBot.telegram.sendDocument(bridge.telegram.chatId, documents[0].media, {
reply_to_message_id: +replyId
});
}
} catch (err) {
logger.error(
`[${bridge.name}] Telegram did not accept Document attachment:`,
(err as Error).toString()
);
}
}
// let tgMessage;

// if (images.length) {
// try {
// if (images.length > 1) {
// // tgMessage =
// await tgBot.telegram.sendMediaGroup(bridge.telegram.chatId, images, {
// reply_to_message_id: +replyId
// });
// } else {
// // tgMessage =
// await tgBot.telegram.sendPhoto(bridge.telegram.chatId, images[0].media, {
// reply_to_message_id: +replyId
// });
// }
// // NOTE: disabled message mapping for galleries
// // messageMap.insert(
// // MessageMap.DISCORD_TO_TELEGRAM,
// // bridge,
// // message.id,
// // tgMessage.message_id.toString()
// // );
// } catch (err) {
// logger.error(
// `[${bridge.name}] Telegram did not accept an Image attachment:`,
// (err as Error).toString()
// );
// }
// }
//
// if (videos.length) {
// try {
// if (videos.length > 1) {
// await tgBot.telegram.sendMediaGroup(bridge.telegram.chatId, videos, {
// reply_to_message_id: +replyId
// });
// } else {
// await tgBot.telegram.sendVideo(bridge.telegram.chatId, videos[0].media, {
// reply_to_message_id: +replyId
// });
// }
// } catch (err) {
// logger.error(
// `[${bridge.name}] Telegram did not accept Video attachment:`,
// (err as Error).toString()
// );
// }
// }
//
// if (audios.length) {
// try {
// if (videos.length > 1) {
// await tgBot.telegram.sendMediaGroup(bridge.telegram.chatId, audios, {
// reply_to_message_id: +replyId
// });
// } else {
// await tgBot.telegram.sendAudio(bridge.telegram.chatId, audios[0].media, {
// reply_to_message_id: +replyId
// });
// }
// } catch (err) {
// logger.error(
// `[${bridge.name}] Telegram did not accept Audio attachment:`,
// (err as Error).toString()
// );
// }
// }
//
// if (documents.length) {
// try {
// if (videos.length > 1) {
// await tgBot.telegram.sendMediaGroup(bridge.telegram.chatId, documents, {
// reply_to_message_id: +replyId
// });
// } else {
// await tgBot.telegram.sendDocument(bridge.telegram.chatId, documents[0].media, {
// reply_to_message_id: +replyId
// });
// }
// } catch (err) {
// logger.error(
// `[${bridge.name}] Telegram did not accept Document attachment:`,
// (err as Error).toString()
// );
// }
// }

// Check the message for embeds
message.embeds.forEach(embed => {
Expand Down
Loading

0 comments on commit 0305b53

Please sign in to comment.