Stable Diffusion Telegram Bot
A Docker container that allows you to host your own Telegram bot and generate images using the Stable Diffusion API. Can also be run as just a script without Docker.
Features
- Host your own Telegram bot and add it to group chats or DMs with friends.
- Use the
!dreamcommand to generate images from prompts. - Use the
!dreamcommand in an image caption to use both the prompt and image to generate an image. - Reply to
!dreammessages or captions to modify the prompt and generate new variations. - Regenerate images using the original prompt, modified prompts, or existing generated images.
- Upscale generated images to 1024x1024 or 2048x2048 resolution using tile resampling via ControlNet.
Examples
| Generating image from text | Generating image from image | Prompt Modification |
|---|---|---|
![]() |
![]() |
![]() |
| - | - | - |
| Retrying | Generating Variation | Upscaling |
![]() |
![]() |
![]() |
Environment Variables
The bot uses several environment variables that can be set when running the Docker container. These variables include:
SERVER_URL: The URL of the Stable Diffusion API server (default: http://127.0.0.1:7860/).TG_TOKEN: The Telegram API token for your bot (default: None).MODEL: The name of the Stable diffusion 1.5 or XL model to use (default: None).SAMPLER: The sampler name to use for image generation (default: DPM++ 2M).SCHEDULER: The scheduler name to use for image generation (default: Karras).CFG_SCALE: An integer between 6 and 14 that controls the CFG scale (default: 7).DENOISING_STRENGTH: A float between 0 and 1 that controls the denoising strength (default: 0.75).STEPS: An integer between 1 and 150 (default: 25).CLIP_LAYERS: An integer between 1 and 12 (default: 2).SEED: A random Integer with value > 0. Value of -1 will generate a ranom number (default: -1).WIDTH: Width of the generated image (default: 512).HEIGHT: Height of the generated image (default: 512).RESTORE_FACES: Enable or Disable face restoring (default: True)NEGATIVE_PROMPT: The default negative prompt for all generations (default: '').RESIZE_MODE: An integer between 0 and 3 for array: [just resize, crop and resize, resize and fill, just resize (latent upscale)] that controls how images are resized (default: 0).LORAS: A string that provides default additions to prompts such as for LORAs (default: '').CONTROLNET_ENABLE: A boolean that enables or disables using ControlNet for img2img generation (default: False).CONTROLNET_MODULE: The name of the ControlNet module to use for img2img generation (default: 'none').CONTROLNET_MODE: An integer between 0 and 2 for array: [Balanced, My prompt is more important, ControlNet is more important] that controls how ControlNet is used for img2img generation (default: 0).RECURSIVE_UPSCALE: A boolean that enables or disables recursive upscaling (default: False).CONTROLNET_UPSCALE_MODEL: Controlnet model to use for tile resample upscale (default: 'none').
Getting Started
Telegram BotFather
To use this bot, you will need to create a Telegram Bot and obtain a bot API token. To do so, message /newbot to @BotFather on telegram. BotFather will reply with a series of steps for you to follow including choosing the name of your bot as well as it's username. Once the steps have been completed, BotFather will share your bot API token with you. The token will be a string like
4839574812:AAFD39kkdpWt3ywyRZergyOLMaJhac60qc
Stable Diffusion This bot relies on access to a local Stable Diffusion instance. Please follow the README for Automatic1111 for complete Windows installation steps.
For NVIDIA GPUs:
- Install Python 3.10.6, ensuring "Add to PATH" is checked
- Install git
- Clone
https://github.com/AUTOMATIC1111/stable-diffusion-webui.gitwithgit cloneand navigate to the directory. - Run
webui.bat --api --listen
For AMD GPUs:
We'll take advantage of ZLUDA to run Stable Diffusion on an AMD GPU. The Repo by lshqqytiger is an AMD-specific fork of Automatic1111
Note, ZLUDA only works on HIP SDK supported AMD GPUs
- Install Python 3.10.6, ensuring "Add to PATH" is checked
- Install git
- Install HIP SDK for Windows
Ensure you de-select the
Visual Studio Codeoptions during install or you may get an error during install
- Add
%HIP_PATH%binto PATH - Clone
https://github.com/lshqqytiger/stable-diffusion-webui-amdgpu.gitwithgit cloneand navigate to the directory. - Run
webui.bat --use-zluda --api --listen
Note, first image generation will take a very long time and will look like it is doing nothing. I recommend using the webui at 127.0.0.1:7860/ with a basic prompt such as
a person riding a bike. Leave your computer be for quite some time (20+ minutes) and keep an eye on the terminal window for a progress bar.
Bot Usage
Option 1: Python To get started with Python, ensure you have Python 3.10.6 installed and added to your PATH prior to following the steps below:
- Clone this repository with
git cloneand navigate to the directory. - Run
python -m pip install -r requirements.txtto install the required packages on your local machine. - Copy
exmaple.envto.envand replace the values. - Once installed, run
python bot.py. - The Telegram bot should now be running and accessible.
Option 2: Docker
Use the below steps if you would like to make code changes and build the image locally. Otherwise, use the image available on Docker Hub,
vshadbolt/stable-diffusion-telegram-bot:latest, in the compose file.
To get started with Docker, ensure you have Docker Desktop installed or otherwise have Docker running on your machine prior to following the steps below:
- Clone this repository with
git cloneand navigate to the directory. - Run
docker build -t stable-diffusion-telegram-bot .to build the Docker image. - (Optional) Modify the
docker-compose.ymlfile environment variables. - Once built, run
docker compose up.
The Telegram bot should now be running and accessible.
If using Docker Portainer, use the following steps instead:
- Clone this repository and navigate to the directory.
- Run
docker build -t stable-diffusion-telegram-bot .to build the Docker image. - Once built, run
docker save -o stable-diffusion-telegram-bot.tar stable-diffusion-telegram-botto save the built docker image to a .tar file in the repository directory - Import the image to Portainer (guide)
- Create a new Stack by copying the compose file from this repository
Deploy the stack and the Telegram bot should be running and accessible.
Common Errors
-
If the bot does not immediately start, ensure the Telegram Token
TG_TOKENis set in the .env or docker compose file. If left asNonethe bot will not be able to start and will show an error similar to below in the logs:The token 'None' was rejected by the server. -
If the bot is unable to access the stable diffusion API server, it will hang on
Dreaming...before eventually sending a message thatStable diffusion is not currently running, come back later. -
If the bot hangs after
Dreaming...,Dreaming again...,Generating dream variation..., orUpscaling dream..., ensure that the bot is still not actively generating the image or otherwise check for error messages in the API response (can be found in the container logs). Attempting to use a model you do not have installed in Stable Diffusion, using ControlNet without the extension installed, or otherwise having a malformed Environment variable will often lead to API errors.





