Public
Script
v1
October 23, 2023
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
const telegramDalleBot = async (
req: express.Request,
res: express.Response,
) => {
if (
req.get("x-telegram-bot-api-secret-token") !==
me.secrets.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") {
vtdocs.telegramSendMessage(
me.secrets.telegramDalleBotToken,
{ chat_id: chatId, text: "send prompt, get an image!" },
);
return;
}
// otherwise, generate an image!
try {
const imageURL =
(await hootz.textToImageDalle(
me.secrets.openai,
text,
1,
"1024x1024",
))
.data[0].url;
vtdocs.telegramSendPhoto(
me.secrets.telegramDalleBotToken,
// caption is limited to 1024 characters
{ chat_id: chatId, photo: imageURL, caption: text.slice(0, 1024) },
);
}
catch (e) {
// report any errors via telegram
vtdocs.telegramSendMessage(
me.secrets.telegramDalleBotToken,
{ chat_id: chatId, text: `error: ${e}` },
);
// also throw so we can check in https://www.val.town/settings/evaluations
throw e;
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v1
October 23, 2023