import { runVal } from "https://esm.town/v/std/runVal";
import type { TelegramSendMessageOptions } from "https://esm.town/v/stevekrouse/telegramSendMessage";
import type { TelegramSendPhotoOptions } from "https://esm.town/v/stevekrouse/telegramSendPhoto";
export async function telegramText(text: string, options?: TextOptions, authorization?: string) {
return telegramRequest("text", { text, options }, authorization);
}
export async function telegramPhoto(options: PhotoOptions, authorization?: string) {
return telegramRequest("photo", { options }, authorization);
}
async function telegramRequest(path, body, authorization?: string) {
const response = await fetch("https://stevekrouse-telegramValTownAPI.web.val.run/" + path, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${authorization ?? Deno.env.get("telegram")}`,
},
});
if (!response.ok) {
const error = await response.text();
throw new Error(error);
} else {
return "SUCCESS";
}
}
type TextOptions = Omit<TelegramSendMessageOptions, "chat_id">;
type PhotoOptions = Omit<TelegramSendPhotoOptions, "chat_id">;
export async function telegram(secret: string, text: string, options?: MergedOptions) {
return runVal("stevekrouse.telegramValTownBot", secret, text, options);
}
export type MergedOptions = TextOptions & PhotoOptions;