Add support for attaching media with links
parent
3dd4b5f06b
commit
d39f37faa0
72
cli.ts
72
cli.ts
|
@ -1,18 +1,11 @@
|
|||
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(
|
||||
command: string,
|
||||
additionalParam: typeof args,
|
||||
) {
|
||||
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();
|
||||
|
||||
|
@ -25,38 +18,55 @@ async function makeTelegramRequest(
|
|||
break;
|
||||
}
|
||||
case "sendMediaGroup": {
|
||||
const files = additionalParam.files.split(" ");
|
||||
const jsonData = [];
|
||||
for (let i = 0; i < additionalParam.files.length; i++) {
|
||||
const file = await Deno.readFile(additionalParam.files[i]);
|
||||
formData.append(
|
||||
`file${i}`,
|
||||
new Blob([file]),
|
||||
additionalParam.files[i],
|
||||
);
|
||||
if (i == 0) {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
if (files[i].startsWith("http")) {
|
||||
jsonData.push({
|
||||
"parse_mode": additionalParam.parse_mode,
|
||||
"caption": additionalParam.caption,
|
||||
"type": "photo",
|
||||
"media": `attach://file${i}`,
|
||||
"media": files[i],
|
||||
});
|
||||
} else {
|
||||
jsonData.push({
|
||||
"type": "photo",
|
||||
"media": `attach://file${i}`,
|
||||
});
|
||||
const file = await Deno.readFile(files[i]);
|
||||
formData.append(
|
||||
`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));
|
||||
console.log(formData);
|
||||
break;
|
||||
}
|
||||
case "sendPhoto": {
|
||||
const file = await Deno.readFile(additionalParam.file);
|
||||
formData.append(
|
||||
"photo",
|
||||
new Blob([file]),
|
||||
additionalParam.file,
|
||||
);
|
||||
if (additionalParam.file.startsWith("http")) {
|
||||
formData.append(
|
||||
"photo",
|
||||
additionalParam.file,
|
||||
additionalParam.file,
|
||||
);
|
||||
} else {
|
||||
const file = await Deno.readFile(additionalParam.file);
|
||||
formData.append(
|
||||
"photo",
|
||||
new Blob([file]),
|
||||
additionalParam.file,
|
||||
);
|
||||
}
|
||||
if (additionalParam.caption) {
|
||||
formData.append("caption", additionalParam.caption);
|
||||
}
|
||||
|
@ -86,6 +96,7 @@ async function makeTelegramRequest(
|
|||
|
||||
const args = parseArgs(Deno.args, {
|
||||
string: [
|
||||
"bot_token",
|
||||
"chat_id",
|
||||
"caption",
|
||||
"text",
|
||||
|
@ -95,11 +106,12 @@ const args = parseArgs(Deno.args, {
|
|||
"parse_mode",
|
||||
],
|
||||
default: {
|
||||
bot_token: "",
|
||||
chat_id: "0",
|
||||
caption: "",
|
||||
text: "",
|
||||
file: "",
|
||||
files: [],
|
||||
files: "",
|
||||
message_id: "0",
|
||||
parse_mode: "MarkdownV2",
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue