Skip to content

Commit a7a0e30

Browse files
Merge pull request #1756 from THEGAMECHANGER416/imagify_bot
Added image generation telegram bot
2 parents f51138e + 21be287 commit a7a0e30

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Telegram AI Image Generator Bot
2+
This is an AI Image generator bot. It can create original images from a single text prompt!
3+
4+
5+
# Screenshot
6+
![Bot Screenshot](telegramBotScreenShot.png)
7+
8+
9+
# Getting Started
10+
This package is simple to use and allows to build an image generation telegram bot
11+
12+
## Prerequisites
13+
14+
- Python
15+
- Telegram Bot API Key
16+
- Replicate Stable Diffusion API Key
17+
18+
## Installation
19+
20+
21+
22+
1. Get a free telegram bot API key from @BotFather telegram bot.
23+
2. Also get a free stable diffusion API key from https://replicate.com/account/api-tokens
24+
![Replicate Screenshot](replicatess.png)
25+
3. Clone the repo
26+
`git clone https://github.com/<your_username>/Amazing-Python-Scripts`
27+
`cd ai_image_generation_telegram_bot`
28+
3. Install the requirements from the requirements.txt file
29+
` pip install -r requirements.txt`
30+
4. Enter the bot token from BotFather in the **TELEGRAM_BOT_TOKEN** in the .env file.
31+
5. Enter the bot token from Replicate in the **REPLICATE_API_TOKEN** in the .env file.
32+
6. Now run the ai_bot.py file and start chatting.
33+
34+
# Bot Commands
35+
You can see a list of commands by typing */help*
36+
37+
| Commands | Description |
38+
|-------------------|-----------------------------------------------|
39+
| /start | Start a conversation |
40+
| /help | Get a list of all the commands |
41+
| /imagine `prompt` | Create an image based on the given command |
42+
43+
#
44+
*Created by [Arnav Kohli](https://github.com/THEGAMECHANGER416)*
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from dotenv import load_dotenv
2+
import os
3+
from telegram import Update
4+
import replicate
5+
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes, MessageHandler, filters
6+
7+
load_dotenv()
8+
9+
10+
def getUrl(prompt):
11+
a = replicate.run(
12+
"stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
13+
input={"prompt": prompt}
14+
)
15+
print(a)
16+
return a[0]
17+
18+
19+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
20+
await update.message.reply_text(
21+
f"Hello {update.effective_user.first_name} I am your personal AI Image Generator \n Use /help to know all commands.")
22+
23+
24+
async def help(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
25+
await update.message.reply_text(
26+
'''Here are a list of all commands:-
27+
/start - Start a conversation
28+
/help - Get a list of commands
29+
/imagine <prompt> - Get an AI generated image'''
30+
)
31+
32+
33+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
34+
await update.message.reply_text("""
35+
Sorry, I did not understand that command.
36+
Type \" /help \" to see all possible commands""")
37+
38+
39+
async def imagine(update: Update, context: ContextTypes.DEFAULT_TYPE):
40+
arg = str(" ".join(context.args))
41+
url = getUrl(arg)
42+
await update.message.reply_photo(url)
43+
44+
45+
app = ApplicationBuilder().token(os.environ.get('TELEGRAM_BOT_TOKEN')).build()
46+
app.add_handler(CommandHandler("start", start))
47+
app.add_handler(CommandHandler("help", help))
48+
app.add_handler(CommandHandler("imagine", imagine))
49+
app.add_handler(MessageHandler(filters.COMMAND, handle_message))
50+
app.run_polling()
18.7 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
requests
2+
python-telegram-bot
3+
python-dotenv
4+
replicate
1.96 MB
Loading

0 commit comments

Comments
 (0)