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)

Readme
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;
}
};
👆 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.