https://github.com/king-tri-ton/doWallAIBot/assets/53092931/c4a93538-438b-43fa-b991-93af583ffeb6
Telegram Bot for Generating Images Based on Text Description Using OpenAI API
This Telegram bot is designed to generate images based on textual descriptions using the image generation model from OpenAI. Users can send textual descriptions, and the bot uses the OpenAI API to generate images, which are then sent back to the user as an image file.
Installation and Setup:
-
Install Dependencies:
Before using, make sure you have all the necessary dependencies installed. You can install them by running the following command:
pip install -r requirements.txt
-
Register Bot on Telegram:
- Register a new bot on Telegram following the instructions here.
- Obtain the bot token from BotFather and save it.
-
Get OpenAI API Key:
- Register on the OpenAI website.
- Obtain the API key by choosing the appropriate pricing plan.
-
Configure Configuration File:
- Create a
config.py
file based on exp.config.py
.
- Fill in the fields
AI_TOKEN
(OpenAI API key), BOT_TOKEN
(Telegram bot token), and ADMIN_ID
(bot administrator’s ID).
-
Run the Bot:
- After configuring all necessary parameters, run the
bot.py
file:
Usage:
-
Command /start:
- Begin interacting with the bot by sending the command
/start
.
- After executing this command, the bot will wait for you to send a textual description to generate an image.
-
Sending Text Description:
- After sending the
/start
command, send the bot a textual description you want to convert into an image.
- The bot will initiate the image generation process based on your description.
-
Command /stats:
- To get usage statistics of the bot, send the command
/stats
.
- This command is only available to the bot administrator.
-
Receiving the Image:
- After generating the image, the bot will send you the result as an image file.
Note:
- Before usage, ensure all necessary libraries are installed, and you have correctly filled in the
config.py
file.
- Please note that using the OpenAI API may incur additional charges depending on your pricing plan.
- This bot is provided “as is” without any warranties or obligations. You use it at your own risk.
Changelog
Version 3.0.0
- `bot.py`
- Added handling for selecting image size and quality for text description.
- Modified text message handler to consider the choice of image size and quality.
- Added new functions: `process_text_description`, `generate_size_keyboard`, `process_size_selection`, `generate_quality_keyboard`, `process_quality_selection`.
- Updated image generation handler to take into account the selected size and quality.
- `functions.py`
- Changed signature of `generate_image_url` function to now accept additional parameters `size` and `quality`.
- Updated `download_and_send_image` function to pass text description along with the image.
Version 2.0.2
- `bot.py`
- Changed import of functions generate_image_url and download_and_send_image from the file functions.py.
- Changed import of db to db_manager from the file db.py.
- Instead of directly calling functions from the db module, now use the create_tables() method of db_manager instance.
- Instead of directly calling the add_user function from the db module, now use the add_user() method of db_manager instance.
- Instead of directly calling the get_total_users function from the db module, now use the get_total_users() method of db_manager instance.
- The generate_image() function was changed to a method with the same name, now using functions from the functions.py file.
- `db.py`
- Added a new class DatabaseManager.
- Database operations are encapsulated within this class.
- Initialized the DatabaseManager object with the default database name.
- Use locking (Lock) to prevent threading issues when accessing the database simultaneously.
- Database operations are performed within the lock context (with self.lock), ensuring safety during parallel access.
- `functions.py`
- The generate_image_url() function now returns the URL of the generated image instead of sending it.
- Added the download_image() function, which downloads the image from a URL and saves it to disk.
- Added the send_image() function, which sends the image to the user and records image data in the database via the db_manager instance.
- Added the download_and_send_image() function, which first downloads the image, then sends it to the user, and records image data in the database via the db_manager instance.
Version 2.0.0
- `exp.config.py`
- Added variable `IMAGE_FOLDER` containing the path to the folder for saving images.
- `bot.py`
- Added validation of the incoming text message length before processing to ensure the text is at least 10 characters long.
- Added exception handling for errors that may occur during image generation using OpenAI. In case of an error, a message is sent to the user requesting to retry the request or contact the developer.
- Changed the logic of image saving: now the image is downloaded and saved in the specified `images` folder with a name in the format `_.png`.
- Changed the method of sending the image to the user: now the image is sent as a file object rather than via URL.
- `db.py`
- Added exception handling in the `add_user(tg_id)` function to prevent potential errors when adding a user to the database.
</details>
---
**License:**
This project is distributed under the [MIT License](https://choosealicense.com/licenses/mit/).
---
If you have any questions or suggestions for improving the project, feel free to contact me at mdolmatov99@gmail.com or via Telegram [@king_triton](https://t.me/king_triton). Thank you for using my bot!