1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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";
/**
* Send a text message to yourself from the ValTownBot Telegram bot
* Message https://t.me/ValTownBot to get started
* @param text
* @param options
* @param authorization
*/
export async function telegramText(text: string, options?: TextOptions, authorization?: string) {
return telegramRequest("text", { text, options }, authorization);
}
/**
* Send a photo to yourself from the ValTownBot Telegram bot
* Message https://t.me/ValTownBot to get started
* @param options
* @param 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">;
/**
* @deprecated since 4/20/2024
*/
export async function telegram(secret: string, text: string, options?: MergedOptions) {
return runVal("stevekrouse.telegramValTownBot", secret, text, options);
}
export type MergedOptions = TextOptions & PhotoOptions;