Skip to content
robdevopsPublic

About

Telegram chat bot for generating AI images

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

151 Commits

Folders and files

Repository files navigation

dalibot

Telegram chat bot for AI image generation, powered by OpenRouter (choose from dozens of image models)

Description

  • Responds to a text prompt with an image
  • Responds to an image with four clearly different variations (each redrawn in its own style and shown from its own viewpoint, both invented by a cheap text model), sent as one album, each picture captioned with its own style and viewpoint. Add a caption to steer them.

Screenshot of chat with Dalibot

Features

  • Auto convert, crop, and resize uploaded images to meet image model requirements
  • Optionally archive results to local storage
  • Runs self-hosted or serverless
  • Minimal dependencies outside the Python Standard Library. There are none if you run it serverless and only use the text prompt feature.

Usage

  • The prompt must be three or more words
  • The prompt must begin with a command, or tag the bot (@botname, anywhere in the message) if it has admin access in the group. Each command uses a different model:
Command Model Notes
/dream openai/gpt-image-2 by default Set OPENROUTER_IMAGE_MODEL to change
/imagine same as /dream An alias of the default command
/gpt openai/gpt-image-2 Cheapest
/flux black-forest-labs/flux-3-image-klein Fastest
/qwen qwen/qwen-image-2.1-turbo
  • The same commands work in a photo's caption to get variations from that model. Replying to any picture (including one the bot made) also gets variations of it, and whatever your reply says is passed along as a note, for example make it winter. Add #strict to get one image that does just what you ask instead (see below). In a group, tag the bot in the reply (@botname make it winter) or start it with a command such as /flux, which also chooses the model; in a private chat any reply works.
  • Editing a message to tag the bot (or to start with a command) works as if you had just sent it. That includes adding the tag to a photo's caption or changing a prompt. Edits that don't tag the bot are ignored.
  • /credits (also /tokens, /usage and /cost) shows what the bot's OpenRouter API key has spent today, this week and this month, using OpenRouter's own totals. If TELEGRAM_ALLOWED_USER_IDS lists user ids, only those users can ask.
  • The caption of every picture the bot sends ends with a hashtag naming the model that made it: #gpt, #flux or #qwen (the tag follows the model, whatever the command is called; a default model that is none of these gets a tag made from its name). Tap it to find that model's pictures in the chat.
  • A text model (OPENROUTER_PLAN_MODEL) first sorts each new prompt into technical (a diagram, cutaway, cross-section, schematic, infographic, or a labelled, how-it-works or educational picture) or artistic (everything else: art, scenes, portraits, logos, memes). An artistic prompt goes to the image model unchanged. For a technical one the model works out the facts first, as up to 12 requirements the picture must get right (what each label says and points to, which way each arrow goes, angles, counts, order, what is perpendicular to or inside what, quantities with their units), then writes the whole prompt: the layout, every label and arrow and how they connect, and a shape that suits it (wide, tall or square). The bot sends the image model a brief that puts accuracy ahead of decoration, that prompt, the requirements as must-haves, and rules for the text (only the words given, spelled exactly, short and legible, no invented numbers, arrows exactly as described). The caption shows what you asked for. A technical picture gets just a 🔄 Retry button, which draws it again from the same prompt and replaces it in place.
  • Each generated image has buttons under it. Every button replaces the image in place:
    • 🔄 Remix generates again with the same model, prompt and style. The image it replaces is kept in the record (as earlier versions of that style), so a gallery still shows it; the buttons then switch to the new one.
    • The style buttons (8-bit, Anime, Bird's eye, Black & white, Cartoon, Comic book, Cubism, Cyberpunk, EGA, Lithograph, Monet, Naive, Photo, Pop art, Rubber stamp, Stencil, Ukiyo-e, Van Gogh, Watercolour and Worm's eye) work like tabs. The first press makes the image. Pressing it again later shows that image straight away, without calling a model. ↩ Original goes back to the image before any style.
    • Before a style is applied, a cheap text model removes words that fight it, so a "photorealistic" prompt loses that word when you press Cubism. Then a short phrase such as "Monet-inspired" or "as a watercolour painting" is added.
    • While an image is being made, the pressed button shows ⏳ in green, and the image and caption stay as they were until the new one replaces them. The other buttons just say "still painting" until it's done. If it fails, the previous buttons come back.
    • Image variations do not get buttons. Each picture of a variations album is recorded on its own, with its style and viewpoint as the caption, in private chats and groups alike.
    • The bot keeps a small record of each image message (the prompt and Telegram file ids, not the images) in SQLite when self-hosted or DynamoDB on Lambda. See the setup guides.
    • To add a picture of your own to the record without generating anything, post it with #storeonly in its caption (as a photo, or as a JPEG file). The bot saves it with the rest of the caption and reacts with 👍. It also works on a picture already in the chat: reply to it with #storeonly (add @botname if the bot does not have admin access in the group), and it is saved under what else your reply says, or else under its own caption. It makes no variations and adds no buttons. Editing the caption afterwards does nothing. A JPEG file must be 20 MB or smaller. It works on an album too: every picture in it is saved, under the album's caption. In a group the bot must be able to see the message, which means it needs admin access there.
    • To bring a small or soft picture up to full size, post it with #upscale (or /upscale) in its caption. The image model redraws it as closely as it can at its full size (1024×1024 for the default models), and the bot posts the result with the rest of the caption and saves it to the record. This is a redraw, not an enlargement: the model invents the added detail and can change small things, so check the result. It costs one image generation. A command in the caption, such as /flux #upscale a red fox, chooses the model. A picture that is not square is cropped to its central square first. The small original is not saved; use #storeonly for that. It also works on a picture already in the chat: reply to it with @botname #upscale. On an album, every picture is redrawn and posted on its own, at one image generation each.
    • To get one image that does just what you ask to a picture, without the different styles and viewpoints that variations add, post it with #strict and your instruction in its caption, for example #strict make it night time. The bot posts the one result with your instruction as its caption and saves it to the record. It costs one image generation. A command in the caption, such as /flux #strict add a hat, chooses the model. A picture that is not square is cropped to its central square first. It also works on a picture already in the chat: reply to it with #strict and your instruction (in a group, add @botname or a command). On an album, every picture gets the same instruction and its own result. If a caption has both #upscale and #strict, #upscale wins. #variation and /variation do the same as #strict (/variation uses the default model). The result (and an #upscale result too) has a 🔄 Retry button, which makes it again from the same picture and instruction and replaces it in place, as editing your message to fix a typo would (the image it replaces stays in the record). The result of a reply (with #strict or #upscale) to one of the bot's pictures is filed in the record under the style that picture shows, for example Rubber stamp, so a gallery groups them together. Only the record changes; the instruction sent to the image model is the same.
    • To see a picture's main colours, reply to it with /palette (or /hex, #palette, #hex), or post a picture with one of them in its caption. The bot replies with a grid of up to 8 main colours, most common first (picked from colours that really appear, favouring vivid ones and small bright accents over several shades of the same dark), each labelled with its hex code, and lists the codes in the caption so you can copy them. A light area such as white fur always gets a tile. Buttons under it show the same colours another way: Vivid (the default: the most vivid colour that is common in each part) or Average (each part's average, which shading makes darker and greyer), and 8 colours, 16 colours or Tones (a row each for the shadows, midtones and highlights, up to 4 colours each). A press redraws the palette in place. It is worked out by the bot itself, so it costs nothing. It is saved to the record with kind and command set to palette, so a gallery can leave palettes out.
    • To remove a picture from the record, reply to it with /delete or #delete. The bot removes that message's record with every style made for it, and reacts with 👍. A gallery reading the record stops showing them. The picture stays in the chat, but if it is one of the bot's own it loses its buttons, so that a press cannot bring it back. Only the users listed by id in TELEGRAM_ALLOWED_USER_IDS can do this and, in a group, that group's owner and admins. If the picture is not one of the bot's own messages (one saved with #storeonly, say), the bot only sees a #delete reply if it has admin access in the group or you tag it (@botname #delete); /delete needs neither, because Telegram always passes commands to the bot.
    • To record pictures the bot made before it kept records, forward them to the bot in a private chat (you must be in TELEGRAM_ALLOWED_USER_IDS). Each one is saved with its caption and the date it was first sent, and the bot reacts with 👍. Nothing is generated, and forwarding the same picture twice does not add it twice. A forwarded picture has no buttons, is recorded under the model its hashtag names (or the default command, for a picture from before the bot added hashtags), and is forgotten after DALIBOT_STORE_DAYS like any other record (forward it again to keep it longer). Forward with the sender and caption shown; a copy sent without them is treated as a new photo. To get variations of one of the bot's pictures, reply to it in the group instead of forwarding it.
  • The bot only responds in group chats, unless you set TELEGRAM_ALLOWED_USER_IDS (user ids, or all)

Requirements

  • A Telegram bot token from BotFather
  • An OpenRouter API key
  • Python 3.10+
  • For self-hosted pull mode (long polling): nothing else
  • For self-hosted push mode (webhook, optional):
    • A web server with a valid domain and certificate
    • Python gevent module
  • If using the image variations feature:
    • Python pillow module

Setup

Security

Protect your API keys and bot tokens

chmod 600 dalibot.env

Preventing Unauthorized use of your bot

  • Because Telegram bots are discoverable by anyone, the bot ignores private messages by default. You can whitelist users with TELEGRAM_ALLOWED_USER_IDS. User IDs can be obtained from the URL bar when messaging a user on https://web.telegram.org/, or from the bots console output when someone messages it. Example config:
TELEGRAM_ALLOWED_USER_IDS="123456789 987654321"

If you have already limited who can reach the bot, for example in BotFather, set TELEGRAM_ALLOWED_USER_IDS=all to let everyone who can reach it message it privately. Only do this if that restriction really is in place, because the bot then answers (and spends your OpenRouter credits for) anyone who can send it a message.

  • It is recommended to message /setjoingroups to BotFather to set your bot group join to Disabled after you have added your bot to any desired groups.

Secure your endpoint

Screenshot of chat with Dalibot

About

Telegram chat bot for generating AI images

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages