Telegram to DallE Bot

Set up

  1. First you'll need to set yourself up to send and receive messages on Telegram. Follow all 5 steps here: https://www.val.town/v/stevekrouse.telegram

  2. Fork this @telegramBotHandler val below. Make sure you click Run to save it to your account.

  3. On your forked val, click the ⋮ menu > Endpoints > Copy express endpoint

  4. Message @ValTownBot /webhook

  5. Message @ValTownBot the express endpoint you copied

  6. You'll also need an openai key in your secrets for this particular DallE bot to work

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
import { t } from "https://esm.town/v/stevekrouse/t";
import { textToImageDalle } from "https://esm.town/v/stevekrouse/textToImageDalle";
import process from "node:process";
export async function telegramBotHandler(req, res) {
let text = req.body.message.text;
res.status(200).send("{}");
if (text.startsWith("/dalle")) {
await t("Loading...");
try {
let resp = await textToImageDalle(
process.env.openai,
text.replace("/dalle", ""),
1,
"1024x1024",
);
if ("error" in resp)
return t("DallE Error: " + resp.error.message);
else
return t(null, { photo: resp.data[0].url });
}
catch (e) {
// report any errors via telegram
await t("Error: " + e.message);
// also throw so we can check in https://www.val.town/settings/evaluations
throw e;
}
// Forked from @hootz.telegramDalleBot
} else if (text.startsWith("/gpt")) {
await t("hi");
}
else {
await t("Unkown command: " + text);
}
}
👆 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.