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;
if (text === "/start") {
telegramSendMessage(
process.env.telegramDalleBotToken,
{ chat_id: chatId, text: "send prompt, get an image!" },
);
return;
}
try {
const imageURL = (await textToImageDalle(
process.env.openai,
text,
1,
"1024x1024",
))
.data[0].url;
telegramSendPhoto(
process.env.telegramDalleBotToken,
{ chat_id: chatId, photo: imageURL, caption: text.slice(0, 1024) },
);
}
catch (e) {
telegramSendMessage(
process.env.telegramDalleBotToken,
{ chat_id: chatId, text: `error: ${e}` },
);
throw e;
}
};