DEV Community

Rahul K
Rahul K

Posted on

I built a Telegram bot that generates AI images using Stable Diffusion β€” here's how

πŸ”— Live portfolio page

AI Image Generation Telegram Bot - Rahul Khunte

Full-stack AI-as-a-Service bot with GPU inference, payment integration, and production deployment

rahulkhunte.github.io

A few months ago I wanted AI image generation accessible
directly in Telegram β€” no web UI, no waiting, just type
a prompt and get an image back in the chat. So I built it.

128 developers cloned the repo in the last 2 weeks with
zero promotion. Apparently others wanted the same thing.

Here's exactly how it works.

What it does

  • Send any text prompt to the bot
  • Bot generates an image using SDXL / Juggernaut XL
  • Returns image directly in Telegram chat
  • Supports custom models via ComfyUI workflows
  • Runs on any GPU server (I use free Kaggle/Colab and a VPS with GPU)

The Stack

  • python-telegram-bot β€” handles all Telegram API
  • ComfyUI β€” the image generation backend
  • SDXL / Juggernaut XL β€” the actual models
  • websockets β€” connects bot to ComfyUI API
  • Oracle Cloud ARM β€” hosts the bot 24/7 for free

How it works

ComfyUI exposes a REST API. The bot sends your prompt
to ComfyUI via websocket, waits for the image, then
sends it back to Telegram. That's the whole loop.

async def generate_image(prompt: str) -> bytes:
    # Build ComfyUI workflow with your prompt
    workflow = build_workflow(prompt)

    # Send to ComfyUI API
    async with websockets.connect(COMFYUI_WS) as ws:
        await queue_prompt(workflow, ws)
        image_data = await wait_for_output(ws)

    return image_data

async def handle_message(update, context):
    prompt = update.message.text
    await update.message.reply_text("Generating... ")

    image = await generate_image(prompt)
    await update.message.reply_photo(image)
Enter fullscreen mode Exit fullscreen mode

The tricky parts

1. ComfyUI workflow as JSON
ComfyUI workflows are JSON graphs. Export any workflow
from the UI, save as a template, then inject the user
prompt at runtime. This lets you swap models without
changing bot code.

2. Queue management
Multiple users sending prompts simultaneously = queue
needed. I use asyncio queues to process one generation
at a time, with the user getting a "position X in queue"
message.

3. GPU access without paying
For development: Kaggle notebooks (30hrs GPU/week free),
Google Colab free tier.
For production hosting the bot itself (not the GPU),
you need almost nothing β€” it's just a Python process
making API calls.

Good free/cheap options:

  • Railway.app β€” free tier, deploy straight from GitHub, zero config
  • Render.com β€” free tier, auto-deploys on git push
  • Fly.io β€” free allowance, great for bots
  • Any $4/month VPS β€” DigitalOcean, Hetzner, Vultr

The bot uses ~80MB RAM idle. A $4 Hetzner ARM server
handles it perfectly.

Results

The bot handles ~20-30 image requests per day on a
single deployment. Average generation time: 8-15 seconds
on a mid-range GPU.

Users can also send style keywords:

  • "realistic portrait of a cat --style photorealistic"
  • "cyberpunk city at night --style anime"

What's next

Currently adding:

  • Image-to-image (send a photo + prompt = transformed image)
  • Multiple model selection via /model command
  • DALL-E 3 fallback when GPU is unavailable

Full source code

Everything is open source:
πŸ‘‰ Github

πŸ€– Jenerator Bot - AI Image Generation

Production-ready Telegram bot with GPU-accelerated image generation using Stable Diffusion XL Turbo. Generate high-quality images in under 4 seconds with natural language prompts.

Status GPU Python

πŸ“„ View Full Project Portfolio

⚠️ Note: Demo currently paused for infrastructure optimization. Full codebase and deployment guide available.

✨ Features

Core Capabilities

  • ⚑ Lightning-fast generation - Sub-4 second image creation (L40 GPU)
  • 🎨 High-quality outputs - SDXL Turbo model for photorealistic results
  • πŸ”§ ComfyUI backend - Professional workflow management
  • πŸ’¬ Telegram integration - Intuitive bot interface with inline buttons
  • πŸ’° Payment system - Cryptocurrency payment integration
  • πŸ“Š Usage tracking - Credit system with transaction history
  • πŸ–ΌοΈ Gallery system - Browse and regenerate previous images

Technical Highlights

  • GPU-optimized inference pipeline
  • Asynchronous request handling
  • Docker containerization
  • Production-grade error handling
  • Automatic queue management
  • Real-time generation status updates

🎯 Why This Project Matters

This bot demonstrates:

  • Production ML deployment - Not a toy…

πŸ‘‰ πŸ”— Live portfolio page

AI Image Generation Telegram Bot - Rahul Khunte

Full-stack AI-as-a-Service bot with GPU inference, payment integration, and production deployment

rahulkhunte.github.io

Includes the full bot, ComfyUI workflow templates,
deployment guide on Cloud, and systemd service
config for 24/7 running.

If you build something with it or have questions,
drop a comment β€” happy to help.

Top comments (0)