Readme

Telegram DALLE Bot

A personal telegram bot you can message to create images with OpenAI's DALLE

DALLE: A Macintosh II sitting on a desk, painted by Picasso in his blue period.

Set up yours

  1. fork this val

  2. speak to telegram’s https://t.me/botfather to create a bot and obtain a bot token

  3. set the bot token as a val town secret called telegramDalleBotToken

  4. add a random string as a val town secret called telegramDalleBotWebhookSecret

  5. set up your webhook with telegram like this:

Create val// paste and run this in your workspace on here @vtdocs.telegramSetWebhook(@me.secrets.telegramDalleBotToken, { url: /* your fork's express endpoint (click the three dots on a val) */, allowed_updates: ["message"], secret_token: @me.secrets.telegramDalleBotWebhookSecret, });
  1. message your bot some prompts!

(if you get stuck, you can refer to the telegram echo bot guide from docs.val.town)

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
import { telegramSendPhoto } from "https://esm.town/v/vtdocs/telegramSendPhoto?v=1";
import { textToImageDalle } from "https://esm.town/v/hootz/textToImageDalle";
import { telegramSendMessage } from "https://esm.town/v/vtdocs/telegramSendMessage?v=5";
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;
}
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.