Add support for attaching media with links

main
Astra 2024-09-10 17:59:21 +01:00
parent 3dd4b5f06b
commit d39f37faa0
1 changed files with 42 additions and 30 deletions

72
cli.ts
View File

@ -1,18 +1,11 @@
import { parseArgs } from "jsr:@std/cli/parse-args"; import { parseArgs } from "jsr:@std/cli/parse-args";
const botToken = Deno.env.get("BOT_TOKEN");
if (!botToken) {
console.error("BOT_TOKEN environment variable must be set.");
Deno.exit(1);
}
async function makeTelegramRequest( async function makeTelegramRequest(
command: string, command: string,
additionalParam: typeof args, additionalParam: typeof args,
) { ) {
const url = const url =
`https://api.telegram.org/bot${botToken}/${command}?chat_id=${additionalParam.chat_id}`; `https://api.telegram.org/bot${additionalParam.bot_token}/${command}?chat_id=${additionalParam.chat_id}`;
const formData = new FormData(); const formData = new FormData();
@ -25,38 +18,55 @@ async function makeTelegramRequest(
break; break;
} }
case "sendMediaGroup": { case "sendMediaGroup": {
const files = additionalParam.files.split(" ");
const jsonData = []; const jsonData = [];
for (let i = 0; i < additionalParam.files.length; i++) { for (let i = 0; i < files.length; i++) {
const file = await Deno.readFile(additionalParam.files[i]); if (files[i].startsWith("http")) {
formData.append(
`file${i}`,
new Blob([file]),
additionalParam.files[i],
);
if (i == 0) {
jsonData.push({ jsonData.push({
"parse_mode": additionalParam.parse_mode,
"caption": additionalParam.caption,
"type": "photo", "type": "photo",
"media": `attach://file${i}`, "media": files[i],
}); });
} else { } else {
jsonData.push({ const file = await Deno.readFile(files[i]);
"type": "photo", formData.append(
"media": `attach://file${i}`, `file${i}`,
}); new Blob([file]),
files[i],
);
if (i == 0) {
jsonData.push({
"parse_mode": additionalParam.parse_mode,
"caption": additionalParam.caption,
"type": "photo",
"media": `attach://file${i}`,
});
} else {
jsonData.push({
"type": "photo",
"media": `attach://file${i}`,
});
}
} }
} }
formData.append("media", JSON.stringify(jsonData)); formData.append("media", JSON.stringify(jsonData));
console.log(formData);
break; break;
} }
case "sendPhoto": { case "sendPhoto": {
const file = await Deno.readFile(additionalParam.file); if (additionalParam.file.startsWith("http")) {
formData.append( formData.append(
"photo", "photo",
new Blob([file]), additionalParam.file,
additionalParam.file, additionalParam.file,
); );
} else {
const file = await Deno.readFile(additionalParam.file);
formData.append(
"photo",
new Blob([file]),
additionalParam.file,
);
}
if (additionalParam.caption) { if (additionalParam.caption) {
formData.append("caption", additionalParam.caption); formData.append("caption", additionalParam.caption);
} }
@ -86,6 +96,7 @@ async function makeTelegramRequest(
const args = parseArgs(Deno.args, { const args = parseArgs(Deno.args, {
string: [ string: [
"bot_token",
"chat_id", "chat_id",
"caption", "caption",
"text", "text",
@ -95,11 +106,12 @@ const args = parseArgs(Deno.args, {
"parse_mode", "parse_mode",
], ],
default: { default: {
bot_token: "",
chat_id: "0", chat_id: "0",
caption: "", caption: "",
text: "", text: "",
file: "", file: "",
files: [], files: "",
message_id: "0", message_id: "0",
parse_mode: "MarkdownV2", parse_mode: "MarkdownV2",
}, },