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
import { textToImageDalle } from "https://esm.town/v/hootz/textToImageDalle";
import { telegramSendMessage } from "https://esm.town/v/vtdocs/telegramSendMessage?v=5";
import { telegramSendPhoto } from "https://esm.town/v/vtdocs/telegramSendPhoto?v=1";
import process from "node:process";
export const telegramDalleBot = async (
req: express.Request,
res: express.Response,
) => {
if (
req.get("x-telegram-bot-api-secret-token")
!== process.env.telegramDalleBotWebhookSecret
) {
return res.status(401);
}
const text: string = req.body.message.text;
const chatId: number = req.body.message.chat.id;
// when you start a chat, this event happens
if (text === "/start") {
telegramSendMessage(
process.env.telegramDalleBotToken,
{ chat_id: chatId, text: "send prompt, get an image!" },
);
return;
}
// otherwise, generate an image!
try {
const imageURL = (await textToImageDalle(
process.env.openai,
text,
1,
"1024x1024",
))
.data[0].url;
telegramSendPhoto(
process.env.telegramDalleBotToken,
// caption is limited to 1024 characters
{ chat_id: chatId, photo: imageURL, caption: text.slice(0, 1024) },
);
}
catch (e) {
// report any errors via telegram
telegramSendMessage(
process.env.telegramDalleBotToken,
{ chat_id: chatId, text: `error: ${e}` },
);
// also throw so we can check in https://www.val.town/settings/evaluations
throw e;
}
};