Skip to content

Commit ebfcdca

Browse files
committed
Added an image generating telegram bot
1 parent a83d9da commit ebfcdca

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-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+
Hi👋 I'm an AI Image generator bot🤖. I can create original images from a single text prompt!
3+
4+
5+
# Screenshot
6+
![Bot Screenshot](botss.jpg)
7+
8+
9+
# Getting Started
10+
Now we are gonna go through how you can create your own image generator AI bot. So exciting 😋. Here are a few prerequisites:-
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/neelshah2409/Bot-Collection`
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+
| Command | Description |
38+
|:--------|------------------------------------------------------------------------:|
39+
| /start | Start a conversation |
40+
| /help | Get a list of all the commands |
41+
| /create `prompt` | Create an image based on the given command |
42+
43+
#
44+
*Created by [Arnav Kohli](https://github.com/THEGAMECHANGER416)*
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
import requests
3+
from dotenv import load_dotenv
4+
import replicate
5+
from telegram import Update
6+
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes, MessageHandler, filters
7+
8+
load_dotenv()
9+
10+
11+
def getUrl(prompt):
12+
a = replicate.run(
13+
"stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
14+
input={"prompt": prompt}
15+
)
16+
print(a)
17+
return a[0]
18+
19+
20+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
21+
await update.message.reply_text(
22+
f"Hello {update.effective_user.first_name} I am BotCollector's own AI Image Generator \n Use /help to know all commands.")
23+
24+
25+
async def help(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
26+
await update.message.reply_text(
27+
'''Here are a list of all commands:-
28+
/start - Start a conversation
29+
/help - Get a list of commands
30+
/create <prompt> - Get an AI generated image'''
31+
)
32+
33+
34+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
35+
await update.message.reply_text("""
36+
Sorry, I did not understand that command.
37+
Type \" /help \" to see all possible commands""")
38+
39+
40+
async def create(update: Update, context: ContextTypes.DEFAULT_TYPE):
41+
arg = str(" ".join(context.args))
42+
url = getUrl(arg)
43+
await update.message.reply_photo(url)
44+
45+
def main():
46+
app = ApplicationBuilder().token(os.environ.get('TELEGRAM_BOT_TOKEN')).build()
47+
app.add_handler(CommandHandler("start", start))
48+
app.add_handler(CommandHandler("help", help))
49+
app.add_handler(CommandHandler("create", create))
50+
app.add_handler(MessageHandler(filters.COMMAND, handle_message))
51+
app.run_polling()
52+
if __name__ == '__main__':
53+
main()
1.16 MB
Loading
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

0 commit comments

Comments
 (0)